obstacles finally spawning
This commit is contained in:
97
semestralka1/src/game/obstacle_system.h
Normal file
97
semestralka1/src/game/obstacle_system.h
Normal file
@@ -0,0 +1,97 @@
|
||||
// src/game/obstacle_system.h
|
||||
#pragma once
|
||||
#include "obstacle_manager.h"
|
||||
#include "../render/player_positioning.h"
|
||||
#include "../assets/obstacle_crawl_frames.h"
|
||||
#include "collision.h"
|
||||
#include "mbed.h"
|
||||
|
||||
// Small helper for minimal mbed-safe clamp
|
||||
inline int clamp_min(int a, int b) { return (a > b) ? a : b; }
|
||||
inline int clamp_max(int a, int b) { return (a < b) ? a : b; }
|
||||
|
||||
// Handles spawning, movement, rendering, and collision of obstacles
|
||||
class ObstacleSystem {
|
||||
public:
|
||||
ObstacleSystem() : tick_counter_(0), spawn_index_(0) {}
|
||||
|
||||
// Called once per frame
|
||||
bool update_and_draw(const CharacterPosition &player_pos,
|
||||
MovementType player_movement) {
|
||||
// Update timers, move existing obstacles
|
||||
tick_counter_++;
|
||||
if (tick_counter_ % UPDATE_INTERVAL == 0) {
|
||||
update_obstacles(1);
|
||||
}
|
||||
|
||||
// Periodically spawn new obstacle
|
||||
if (tick_counter_ % SPAWN_EVERY_TICKS == 0) {
|
||||
auto type = (spawn_index_++ % 2 == 0)
|
||||
? CrawlObstacleType::Crawl1
|
||||
: CrawlObstacleType::Crawl2;
|
||||
const int w = (type == CrawlObstacleType::Crawl1)
|
||||
? OBSTACLE_CRAWL1_FRAME_WIDTH
|
||||
: OBSTACLE_CRAWL2_FRAME_WIDTH;
|
||||
spawn_obstacle(type, VIEW_WIDTH - w - 1);
|
||||
}
|
||||
|
||||
// Draw & collision check
|
||||
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||
if (!obstacle_pool[i].active)
|
||||
continue;
|
||||
|
||||
const char **frame = nullptr;
|
||||
int height = 0;
|
||||
if (obstacle_pool[i].type == CrawlObstacleType::Crawl1) {
|
||||
frame = OBSTACLE_CRAWL1_FRAME;
|
||||
height = OBSTACLE_CRAWL1_FRAME_HEIGHT;
|
||||
} else {
|
||||
frame = OBSTACLE_CRAWL2_FRAME;
|
||||
height = OBSTACLE_CRAWL2_FRAME_HEIGHT;
|
||||
}
|
||||
|
||||
draw_clipped_obstacle(obstacle_pool[i], frame, height);
|
||||
|
||||
// Collision check
|
||||
if (check_collision(player_pos, player_movement,
|
||||
obstacle_pool[i].data)) {
|
||||
printf("\033[2J\033[H");
|
||||
printf("GAME OVER\r\n");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr int SPAWN_EVERY_TICKS = 35;
|
||||
static constexpr int UPDATE_INTERVAL = 1; // move obstacles each frame
|
||||
int tick_counter_;
|
||||
int spawn_index_;
|
||||
|
||||
static void draw_clipped_obstacle(const MovingObstacle &obs,
|
||||
const char **frame,
|
||||
int frame_height) {
|
||||
int x = obs.data.x;
|
||||
int y = obs.data.y;
|
||||
int w = obs.data.width;
|
||||
|
||||
for (int r = 0; r < frame_height; ++r) {
|
||||
int left = x;
|
||||
int right = x + w;
|
||||
if (right <= 0 || left >= VIEW_WIDTH)
|
||||
continue;
|
||||
|
||||
int vis_left = clamp_min(left, 0);
|
||||
int vis_right = clamp_max(right, VIEW_WIDTH);
|
||||
int start = vis_left - left;
|
||||
int count = vis_right - vis_left;
|
||||
if (count <= 0)
|
||||
continue;
|
||||
|
||||
const char *row = frame[r] + start;
|
||||
printf("\033[%d;%dH%.*s", y + r + 1, vis_left + 1, count, row);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user