Compare commits
20 Commits
09454e3ab4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b10d13695 | ||
|
|
74dca27c06 | ||
|
|
786ead2ffa | ||
|
|
e5091a33e7 | ||
|
|
3cef68142d | ||
|
|
17c53ace99 | ||
|
|
50f523b52d | ||
|
|
42d07e9715 | ||
|
|
8382d1caec | ||
|
|
165540588f | ||
|
|
0083fff73a | ||
|
|
ca7eac70e4 | ||
|
|
3e47f855ed | ||
|
|
dd410c6f6c | ||
|
|
967b377e76 | ||
|
|
cf30946c0d | ||
|
|
2af725bfe7 | ||
|
|
c56dac3b6a | ||
|
|
ae2b8b91aa | ||
|
|
34c9d76389 |
@@ -40,3 +40,18 @@ static const int CHARACTER_CRAWL2_FRAME_COUNT = sizeof(CHARACTER_CRAWL2_FRAMES)
|
|||||||
// Height (rows per crawl frame)
|
// Height (rows per crawl frame)
|
||||||
static const int CHARACTER_CRAWL1_FRAME_HEIGHT = sizeof(CRAWL1_FRAME_1) / sizeof(CRAWL1_FRAME_1[0]);
|
static const int CHARACTER_CRAWL1_FRAME_HEIGHT = sizeof(CRAWL1_FRAME_1) / sizeof(CRAWL1_FRAME_1[0]);
|
||||||
static const int CHARACTER_CRAWL2_FRAME_HEIGHT = sizeof(CRAWL2_FRAME_1) / sizeof(CRAWL2_FRAME_1[0]);
|
static const int CHARACTER_CRAWL2_FRAME_HEIGHT = sizeof(CRAWL2_FRAME_1) / sizeof(CRAWL2_FRAME_1[0]);
|
||||||
|
|
||||||
|
|
||||||
|
// Crawl1 collision box
|
||||||
|
static const int CHARACTER_CRAWL1_FRAME_WIDTH = 8;
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_RIGHT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_WIDTH = CHARACTER_CRAWL1_FRAME_WIDTH - (CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET + CHARACTER_CRAWL1_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_HEIGHT = CHARACTER_CRAWL1_FRAME_HEIGHT - 2;
|
||||||
|
|
||||||
|
// Crawl2 collision box (lower posture - smaller collision height)
|
||||||
|
static const int CHARACTER_CRAWL2_FRAME_WIDTH = 8;
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_RIGHT_OFFSET = 0;
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_WIDTH = CHARACTER_CRAWL2_FRAME_WIDTH - (CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET + CHARACTER_CRAWL2_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_HEIGHT = CHARACTER_CRAWL2_FRAME_HEIGHT - 4;
|
||||||
|
|||||||
@@ -96,3 +96,11 @@ static const int CHARACTER_RUN_FRAME_COUNT = sizeof(CHARACTER_RUN_FRAMES) / size
|
|||||||
|
|
||||||
// Height (rows per run frame)
|
// Height (rows per run frame)
|
||||||
static const int CHARACTER_RUN_FRAME_HEIGHT = sizeof(RUN_FRAME_1) / sizeof(RUN_FRAME_1[0]);
|
static const int CHARACTER_RUN_FRAME_HEIGHT = sizeof(RUN_FRAME_1) / sizeof(RUN_FRAME_1[0]);
|
||||||
|
|
||||||
|
static const int CHARACTER_RUN_COLLISION_HEIGHT = CHARACTER_RUN_FRAME_HEIGHT;
|
||||||
|
|
||||||
|
static const int CHARACTER_RUN_FRAME_WIDTH = 8;
|
||||||
|
static const int CHARACTER_RUN_COLLISION_LEFT_OFFSET = 2;
|
||||||
|
static const int CHARACTER_RUN_COLLISION_RIGHT_OFFSET = 2;
|
||||||
|
// 8 je width a 2 zlava a 2 zprava
|
||||||
|
static const int CHARACTER_RUN_COLLISION_WIDTH = CHARACTER_RUN_FRAME_WIDTH - (CHARACTER_RUN_COLLISION_LEFT_OFFSET + CHARACTER_RUN_COLLISION_RIGHT_OFFSET);
|
||||||
|
|||||||
@@ -96,3 +96,9 @@ static const int CHARACTER_WALK_FRAME_COUNT = sizeof(CHARACTER_WALK_FRAMES) / si
|
|||||||
|
|
||||||
// Height (rows per walk frame)
|
// Height (rows per walk frame)
|
||||||
static const int CHARACTER_WALK_FRAME_HEIGHT = sizeof(WALK_FRAME_1) / sizeof(WALK_FRAME_1[0]);
|
static const int CHARACTER_WALK_FRAME_HEIGHT = sizeof(WALK_FRAME_1) / sizeof(WALK_FRAME_1[0]);
|
||||||
|
|
||||||
|
static const int CHARACTER_WALK_FRAME_WIDTH = 6;
|
||||||
|
static const int CHARACTER_WALK_COLLISION_LEFT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_WALK_COLLISION_RIGHT_OFFSET = 1;
|
||||||
|
// 8 je width a 2 zlava a 2 zprava
|
||||||
|
static const int CHARACTER_WALK_COLLISION_WIDTH = CHARACTER_WALK_FRAME_WIDTH - (CHARACTER_WALK_COLLISION_LEFT_OFFSET + CHARACTER_WALK_COLLISION_RIGHT_OFFSET);
|
||||||
|
|||||||
47
semestralka1/src/assets/obstacle_crawl_frames.h
Normal file
47
semestralka1/src/assets/obstacle_crawl_frames.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// src/assets/obstacle_crawl_frames.h
|
||||||
|
#pragma once
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// Crawl obstacle for CRAWL1 (2 lines tall)
|
||||||
|
static const char *OBSTACLE_CRAWL1_FRAME[] = {
|
||||||
|
"#########",
|
||||||
|
".######",
|
||||||
|
"..#####",
|
||||||
|
"..####",
|
||||||
|
"...###",
|
||||||
|
"...###"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Crawl obstacle for CRAWL2 (4 lines tall)
|
||||||
|
static const char *OBSTACLE_CRAWL2_FRAME[] = {
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###"
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class CrawlObstacleType { Crawl1 = 0, Crawl2, COUNT };
|
||||||
|
|
||||||
|
static const char **OBSTACLE_CRAWL_FRAMES[] = { OBSTACLE_CRAWL1_FRAME, OBSTACLE_CRAWL2_FRAME };
|
||||||
|
|
||||||
|
// Crawl1 dimensions and collision
|
||||||
|
static const int OBSTACLE_CRAWL1_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL1_FRAME) / sizeof(OBSTACLE_CRAWL1_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL1_FRAME_WIDTH = std::strlen(OBSTACLE_CRAWL1_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET = 3;
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_RIGHT_OFFSET = 3;
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_WIDTH = OBSTACLE_CRAWL1_FRAME_WIDTH - (OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET + OBSTACLE_CRAWL1_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET = 4; // first 4 rows are just empty ('.')
|
||||||
|
// collision box height = bottom 2 rows
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_HEIGHT = 2;
|
||||||
|
|
||||||
|
// Crawl2 dimensions and collision
|
||||||
|
static const int OBSTACLE_CRAWL2_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL2_FRAME) / sizeof(OBSTACLE_CRAWL2_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL2_FRAME_WIDTH = std::strlen(OBSTACLE_CRAWL2_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_LEFT_OFFSET = 0;
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_RIGHT_OFFSET = 0;
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_WIDTH = OBSTACLE_CRAWL2_FRAME_WIDTH - (OBSTACLE_CRAWL2_COLLISION_LEFT_OFFSET + OBSTACLE_CRAWL2_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_HEIGHT = OBSTACLE_CRAWL2_FRAME_HEIGHT;
|
||||||
@@ -14,7 +14,5 @@ public:
|
|||||||
|
|
||||||
// Update animation, returns true if redraw needed
|
// Update animation, returns true if redraw needed
|
||||||
bool tick(int speed);
|
bool tick(int speed);
|
||||||
|
|
||||||
// Getters
|
|
||||||
int get_shift() const { return shift; }
|
int get_shift() const { return shift; }
|
||||||
};
|
};
|
||||||
|
|||||||
64
semestralka1/src/game/collision.h
Normal file
64
semestralka1/src/game/collision.h
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
// src/game/collision.h
|
||||||
|
#pragma once
|
||||||
|
#include "../render/player_positioning.h"
|
||||||
|
#include "../assets/character_walk_frames.h"
|
||||||
|
#include "../assets/character_run_frames.h"
|
||||||
|
#include "../assets/character_crawl_frames.h"
|
||||||
|
|
||||||
|
struct Obstacle {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline bool check_collision(const CharacterPosition& player,
|
||||||
|
MovementType movement,
|
||||||
|
const Obstacle& obs) {
|
||||||
|
|
||||||
|
// Player is given in bottom-based world coordinates (0 = bottom line)
|
||||||
|
const int player_x = player.x;
|
||||||
|
const int player_bottom = player.y;
|
||||||
|
int box_x = 0;
|
||||||
|
int box_w = 0;
|
||||||
|
int box_h = 0;
|
||||||
|
|
||||||
|
switch (movement) {
|
||||||
|
case MovementType::Walk:
|
||||||
|
box_x = player_x + CHARACTER_WALK_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_WALK_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_WALK_FRAME_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Run:
|
||||||
|
box_x = player_x + CHARACTER_RUN_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_RUN_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_RUN_COLLISION_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Crawl1:
|
||||||
|
box_x = player_x + CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_CRAWL1_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_CRAWL1_COLLISION_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Crawl2:
|
||||||
|
box_x = player_x + CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_CRAWL2_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_CRAWL2_COLLISION_HEIGHT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player bounding box
|
||||||
|
const int player_left = box_x;
|
||||||
|
const int player_right = player_left + box_w;
|
||||||
|
const int player_top = player_bottom + box_h;
|
||||||
|
|
||||||
|
// Obstacle bounding box
|
||||||
|
const int obs_left = obs.x;
|
||||||
|
const int obs_right = obs.x + obs.width;
|
||||||
|
const int obs_bottom = obs.y;
|
||||||
|
const int obs_top = obs_bottom + obs.height;
|
||||||
|
|
||||||
|
// Overlap check
|
||||||
|
bool horizontal = player_left < obs_right && player_right > obs_left;
|
||||||
|
bool vertical = player_bottom < obs_top && player_top > obs_bottom;
|
||||||
|
return horizontal && vertical;
|
||||||
|
}
|
||||||
48
semestralka1/src/game/game_over.cpp
Normal file
48
semestralka1/src/game/game_over.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// src/game/game_over.cpp
|
||||||
|
#include "game_over.h"
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "../render/background.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
|
#include "../hardware/button.h"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
void show_game_over_screen(int seconds, ButtonHandler& button) {
|
||||||
|
printf("\033[2J\033[H");
|
||||||
|
|
||||||
|
// 34 znakov vo vypise
|
||||||
|
const int box_width = 34;
|
||||||
|
const int start_col = (VIEW_WIDTH - box_width) / 2;
|
||||||
|
|
||||||
|
const int box_height = 6;
|
||||||
|
const int top_padding = (VIEW_HEIGHT - box_height) / 2;
|
||||||
|
|
||||||
|
// Empty rows
|
||||||
|
for (int i = 0; i < top_padding; i++) {
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
|
printf("==================================\r\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
|
printf(" G A M E O V E R \r\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
|
printf("==================================\r\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
|
printf(" Doba hry %d sekund. \r\n", seconds);
|
||||||
|
|
||||||
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
|
printf("==================================\r\n");
|
||||||
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
|
printf("Stlacte 2x po sebe tlacidlo pre restart\r\n");
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
while (button.poll() != ButtonEvent::Pressed)
|
||||||
|
ThisThread::sleep_for(25ms);
|
||||||
|
|
||||||
|
printf("\033[2J\033[H");
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
8
semestralka1/src/game/game_over.h
Normal file
8
semestralka1/src/game/game_over.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// src/game/game_over.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "../hardware/button.h"
|
||||||
|
|
||||||
|
// Displays GAME OVER message with elapsed time.
|
||||||
|
// Takes a Timer that was measuring game duration.
|
||||||
|
void show_game_over_screen(int seconds, ButtonHandler& button);
|
||||||
6
semestralka1/src/game/game_state.cpp
Normal file
6
semestralka1/src/game/game_state.cpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
// src/game/game_state.cpp
|
||||||
|
#include "game_state.h"
|
||||||
|
|
||||||
|
GameState g_state;
|
||||||
|
Mutex g_state_mutex;
|
||||||
36
semestralka1/src/game/game_state.h
Normal file
36
semestralka1/src/game/game_state.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// src/game/game_state.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "../render/player.h"
|
||||||
|
#include "../render/player_positioning.h"
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
|
||||||
|
// POZOR JE Z OBSTACLE MANAGERA
|
||||||
|
constexpr int MAX_RENDER_OBSTACLES = 6;
|
||||||
|
|
||||||
|
struct ObstacleRenderData {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
CrawlObstacleType type;
|
||||||
|
bool active;
|
||||||
|
};
|
||||||
|
|
||||||
|
// All data needed to render one frame
|
||||||
|
struct GameState {
|
||||||
|
CharacterPosition player_pos;
|
||||||
|
MovementType movement;
|
||||||
|
int frame_index;
|
||||||
|
|
||||||
|
int background_shift;
|
||||||
|
|
||||||
|
int current_speed;
|
||||||
|
ObstacleRenderData obstacles[MAX_RENDER_OBSTACLES];
|
||||||
|
|
||||||
|
int elapsed_seconds;
|
||||||
|
bool game_over;
|
||||||
|
bool need_redraw;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Global shared
|
||||||
|
extern GameState g_state;
|
||||||
|
extern Mutex g_state_mutex;
|
||||||
54
semestralka1/src/game/obstacle_manager.h
Normal file
54
semestralka1/src/game/obstacle_manager.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// src/game/obstacle_manager.h
|
||||||
|
#pragma once
|
||||||
|
#include "../game/collision.h"
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
|
|
||||||
|
struct MovingObstacle {
|
||||||
|
Obstacle data;
|
||||||
|
CrawlObstacleType type;
|
||||||
|
bool active;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr int MAX_OBSTACLES = 6;
|
||||||
|
static MovingObstacle obstacle_pool[MAX_OBSTACLES];
|
||||||
|
|
||||||
|
// Create/reset obstacle
|
||||||
|
inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (!obstacle_pool[i].active) {
|
||||||
|
obstacle_pool[i].active = true;
|
||||||
|
obstacle_pool[i].type = type;
|
||||||
|
obstacle_pool[i].data.x = x_start;
|
||||||
|
|
||||||
|
obstacle_pool[i].data.width = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_COLLISION_WIDTH : OBSTACLE_CRAWL2_COLLISION_WIDTH;
|
||||||
|
obstacle_pool[i].data.height = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_COLLISION_HEIGHT : OBSTACLE_CRAWL2_COLLISION_HEIGHT;
|
||||||
|
|
||||||
|
// Default Y so that the character touches the top of the screen.
|
||||||
|
// We store Y as the bottom of the COLLISION BOX in bottom-based world coords.
|
||||||
|
int top_offset = 0;
|
||||||
|
if (type == CrawlObstacleType::Crawl1) {
|
||||||
|
top_offset = OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET;
|
||||||
|
} else {
|
||||||
|
top_offset = 0;
|
||||||
|
}
|
||||||
|
const int i_bottom_of_collision = top_offset + obstacle_pool[i].data.height - 1;
|
||||||
|
// Top of screen in world coords is VIEW_HEIGHT - 1
|
||||||
|
const int collision_bottom_world_y = (VIEW_HEIGHT - 1) - i_bottom_of_collision;
|
||||||
|
obstacle_pool[i].data.y = collision_bottom_world_y;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update X positions, mark inactive if offscreen
|
||||||
|
inline void update_obstacles(int shift_speed) {
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (!obstacle_pool[i].active) continue;
|
||||||
|
obstacle_pool[i].data.x -= shift_speed;
|
||||||
|
if (obstacle_pool[i].data.x + obstacle_pool[i].data.width < 0)
|
||||||
|
obstacle_pool[i].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
71
semestralka1/src/game/obstacle_system.h
Normal file
71
semestralka1/src/game/obstacle_system.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
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) {}
|
||||||
|
|
||||||
|
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 n 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);
|
||||||
|
|
||||||
|
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*/) {
|
||||||
|
draw_obstacle(obs.data.x, obs.data.y, obs.type);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -91,19 +91,20 @@ static inline int compute_frame_index(int frame_count,
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void WalkingState::update() {
|
void MovementState::update() {
|
||||||
// stop crawling after duration
|
// stop crawling after duration
|
||||||
if ((current_state == PlayerState::Crawl1 ||
|
if ((current_state == PlayerState::Crawl1 ||
|
||||||
current_state == PlayerState::Crawl2) &&
|
current_state == PlayerState::Crawl2) &&
|
||||||
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
||||||
current_state = PlayerState::Walk;
|
current_state = previous_state;
|
||||||
state_timer.stop();
|
state_timer.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WalkingState::start_crawl(PlayerState crawl_type) {
|
void MovementState::start_crawl(PlayerState crawl_type) {
|
||||||
if (crawl_type != PlayerState::Crawl1 && crawl_type != PlayerState::Crawl2)
|
if (current_state != PlayerState::Crawl1 && current_state != PlayerState::Crawl2) {
|
||||||
return;
|
previous_state = current_state;
|
||||||
|
}
|
||||||
|
|
||||||
current_state = crawl_type;
|
current_state = crawl_type;
|
||||||
state_timer.stop();
|
state_timer.stop();
|
||||||
@@ -111,7 +112,7 @@ void WalkingState::start_crawl(PlayerState crawl_type) {
|
|||||||
state_timer.start();
|
state_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WalkingState::set_state(PlayerState s) {
|
void MovementState::set_state(PlayerState s) {
|
||||||
if (current_state != s) {
|
if (current_state != s) {
|
||||||
walk_index = run_index = crawl1_index = crawl2_index = 0;
|
walk_index = run_index = crawl1_index = crawl2_index = 0;
|
||||||
phase = 0.0f;
|
phase = 0.0f;
|
||||||
@@ -119,18 +120,7 @@ void WalkingState::set_state(PlayerState s) {
|
|||||||
current_state = s;
|
current_state = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WalkingState::set_motion_state_for_speed(int player_speed, int ground_speed) {
|
void MovementState::toggle_walk_frame(float player_speed, int tick_counter) {
|
||||||
int relative = player_speed - ground_speed;
|
|
||||||
PlayerState new_state = (relative > 1) ? PlayerState::Run : PlayerState::Walk;
|
|
||||||
if (new_state != current_state) {
|
|
||||||
run_index = 0;
|
|
||||||
walk_index = 0;
|
|
||||||
}
|
|
||||||
current_state = new_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO THIS NEEDS REDESIGN FOR WALK/RUN
|
|
||||||
void WalkingState::toggle_walk_frame(float player_speed, int tick_counter) {
|
|
||||||
switch (current_state) {
|
switch (current_state) {
|
||||||
case PlayerState::Run:
|
case PlayerState::Run:
|
||||||
run_index = compute_frame_index(CHARACTER_RUN_FRAME_COUNT, player_speed, tick_counter);
|
run_index = compute_frame_index(CHARACTER_RUN_FRAME_COUNT, player_speed, tick_counter);
|
||||||
@@ -150,7 +140,7 @@ void WalkingState::toggle_walk_frame(float player_speed, int tick_counter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameSelection WalkingState::get_frame_selection() const {
|
FrameSelection MovementState::get_frame_selection() const {
|
||||||
FrameSelection f{};
|
FrameSelection f{};
|
||||||
switch (current_state) {
|
switch (current_state) {
|
||||||
case PlayerState::Walk:
|
case PlayerState::Walk:
|
||||||
|
|||||||
@@ -12,9 +12,10 @@ struct FrameSelection {
|
|||||||
int frame_index;
|
int frame_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WalkingState {
|
class MovementState {
|
||||||
private:
|
private:
|
||||||
PlayerState current_state = PlayerState::Walk;
|
PlayerState current_state = PlayerState::Walk;
|
||||||
|
PlayerState previous_state = PlayerState::Walk;
|
||||||
Timer state_timer;
|
Timer state_timer;
|
||||||
int walk_index = 0; // cycles 0‑6
|
int walk_index = 0; // cycles 0‑6
|
||||||
int run_index = 0; // cycles 0‑6
|
int run_index = 0; // cycles 0‑6
|
||||||
@@ -26,7 +27,6 @@ public:
|
|||||||
void update();
|
void update();
|
||||||
void start_crawl(PlayerState crawl_type);
|
void start_crawl(PlayerState crawl_type);
|
||||||
|
|
||||||
void set_motion_state_for_speed(int player_speed, int ground_speed);
|
|
||||||
void toggle_walk_frame(float player_speed, int tick_counter);
|
void toggle_walk_frame(float player_speed, int tick_counter);
|
||||||
|
|
||||||
FrameSelection get_frame_selection() const;
|
FrameSelection get_frame_selection() const;
|
||||||
|
|||||||
33
semestralka1/src/hardware/button.cpp
Normal file
33
semestralka1/src/hardware/button.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// src/hardware/button.cpp
|
||||||
|
#include "button.h"
|
||||||
|
|
||||||
|
ButtonHandler::ButtonHandler(PinName pin, PinName led_pin) : button(pin), led(led_pin, 0) {
|
||||||
|
// ISR for button press (falling edge)
|
||||||
|
button.fall(callback(this, &ButtonHandler::on_pressed));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ButtonHandler::on_pressed() {
|
||||||
|
pressed_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ButtonEvent ButtonHandler::poll() {
|
||||||
|
static Timer blink_timer;
|
||||||
|
static bool blinking = false;
|
||||||
|
|
||||||
|
if (pressed_flag) {
|
||||||
|
pressed_flag = false;
|
||||||
|
led = 1;
|
||||||
|
blink_timer.reset();
|
||||||
|
blink_timer.start();
|
||||||
|
blinking = true;
|
||||||
|
return ButtonEvent::Pressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blinking && blink_timer.elapsed_time() >= 500ms) {
|
||||||
|
led = 0;
|
||||||
|
blink_timer.stop();
|
||||||
|
blinking = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ButtonEvent::None;
|
||||||
|
}
|
||||||
23
semestralka1/src/hardware/button.h
Normal file
23
semestralka1/src/hardware/button.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// src/hardware/button.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
|
||||||
|
enum class ButtonEvent {
|
||||||
|
None = 0,
|
||||||
|
Pressed = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
class ButtonHandler {
|
||||||
|
private:
|
||||||
|
InterruptIn button;
|
||||||
|
DigitalOut led;
|
||||||
|
volatile bool pressed_flag = false;
|
||||||
|
volatile bool blink_flag = false;
|
||||||
|
|
||||||
|
void on_pressed();
|
||||||
|
|
||||||
|
public:
|
||||||
|
ButtonHandler(PinName pin = BUTTON1, PinName led_pin = LED1);
|
||||||
|
|
||||||
|
ButtonEvent poll();
|
||||||
|
};
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
// src/hardware/uart.cpp
|
|
||||||
#include "uart.h"
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
UartReader::UartReader(BufferedSerial &serial) : serial_port(serial) {
|
|
||||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
UartEvent UartReader::poll() {
|
|
||||||
bool changed = false;
|
|
||||||
bool triggered = false;
|
|
||||||
|
|
||||||
// cita spravu z uartu
|
|
||||||
if (serial_port.readable()) {
|
|
||||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
||||||
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
|
||||||
if (num > 0) {
|
|
||||||
strncpy(message, rx_buffer, sizeof(message) - 1);
|
|
||||||
message_active = true;
|
|
||||||
changed = true;
|
|
||||||
triggered = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// casovac na 1s zobrazenia spravy
|
|
||||||
if (!timer_started) {
|
|
||||||
msg_timer.start();
|
|
||||||
timer_started = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// po jednu sekundu sa sprava zobrazi
|
|
||||||
if (message_active && msg_timer.elapsed_time() > MESSAGE_DISPLAY_DURATION) {
|
|
||||||
message_active = false;
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
msg_timer.reset();
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (triggered)
|
|
||||||
return UartEvent::Triggered;
|
|
||||||
if (changed)
|
|
||||||
return UartEvent::MessageUpdate;
|
|
||||||
return UartEvent::NoChange;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* UartReader::get_message() const {
|
|
||||||
return message_active ? message : nullptr;
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
// src/hardware/uart.h
|
|
||||||
#pragma once
|
|
||||||
#include "mbed.h"
|
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
constexpr size_t UART_BUFFER_SIZE = 64;
|
|
||||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
|
||||||
|
|
||||||
enum class UartEvent {
|
|
||||||
NoChange = 0, // No new data
|
|
||||||
MessageUpdate = 1, // Message changed (received or expired)
|
|
||||||
Triggered = 2 // New message received (trigger action)
|
|
||||||
};
|
|
||||||
|
|
||||||
class UartReader {
|
|
||||||
private:
|
|
||||||
BufferedSerial &serial_port;
|
|
||||||
|
|
||||||
char rx_buffer[UART_BUFFER_SIZE];
|
|
||||||
char message[UART_BUFFER_SIZE];
|
|
||||||
bool message_active = false;
|
|
||||||
|
|
||||||
Timer msg_timer;
|
|
||||||
bool timer_started = false;
|
|
||||||
|
|
||||||
public:
|
|
||||||
UartReader(BufferedSerial &serial);
|
|
||||||
|
|
||||||
// Poll for UART events
|
|
||||||
UartEvent poll();
|
|
||||||
|
|
||||||
// Get current message (nullptr if no active message)
|
|
||||||
const char* get_message() const;
|
|
||||||
bool has_message() const { return message_active; }
|
|
||||||
};
|
|
||||||
@@ -21,6 +21,7 @@ int main(void) {
|
|||||||
serial_port.set_format(8, BufferedSerial::None, 1);
|
serial_port.set_format(8, BufferedSerial::None, 1);
|
||||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||||
|
|
||||||
// Just call into render feature
|
while (true) {
|
||||||
render_loop(6);
|
render_loop(4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text) {
|
void draw_mask(const char *unused_filename, int shift) {
|
||||||
// Terminal clear + home
|
// Terminal clear + home
|
||||||
printf("\033[2J\033[H");
|
printf("\033[2J\033[H");
|
||||||
|
|
||||||
@@ -25,9 +25,5 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
|
|||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text && text[0] != '\0') {
|
|
||||||
printf("\r\n[RX] %s\r\n", text);
|
|
||||||
}
|
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,4 @@
|
|||||||
constexpr int VIEW_WIDTH = 90;
|
constexpr int VIEW_WIDTH = 90;
|
||||||
|
|
||||||
// Draws the ASCII art background with optional message
|
// Draws the ASCII art background with optional message
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text = nullptr);
|
void draw_mask(const char *unused_filename, int shift);
|
||||||
|
|||||||
@@ -5,88 +5,216 @@
|
|||||||
#include "background.h"
|
#include "background.h"
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "background.h"
|
||||||
|
#include "../game/game_over.h"
|
||||||
|
#include "../game/game_state.h"
|
||||||
#include "../game/state.h"
|
#include "../game/state.h"
|
||||||
#include "../game/animation.h"
|
#include "../game/animation.h"
|
||||||
#include "../hardware/uart.h"
|
#include "../game/collision.h"
|
||||||
|
#include "../hardware/button.h"
|
||||||
#include "../render/player.h"
|
#include "../render/player.h"
|
||||||
|
#include "../render/obstacle.h"
|
||||||
#include "../timing/speed_controller.h"
|
#include "../timing/speed_controller.h"
|
||||||
#include "../timing/movement_controller.h"
|
#include "../timing/movement_controller.h"
|
||||||
|
#include "../game/obstacle_system.h"
|
||||||
|
|
||||||
extern BufferedSerial serial_port;
|
|
||||||
extern DigitalOut led;
|
extern DigitalOut led;
|
||||||
|
ButtonHandler button(BUTTON1, LED1);
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
// constexpr int PLAYER_X = 9;
|
// constexpr int PLAYER_X = 9;
|
||||||
constexpr int PLAYER_X = 29;
|
constexpr int PLAYER_X = 29;
|
||||||
constexpr int PLAYER_Y = 6;
|
constexpr int PLAYER_Y = 6;
|
||||||
|
constexpr int STACK_SIZE = 4096;
|
||||||
|
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text);
|
void logic_loop(void *arg) {
|
||||||
|
int speed = *(int *)arg;
|
||||||
void render_loop(int speed) {
|
MovementState player_state;
|
||||||
WalkingState player_state;
|
|
||||||
AnimationController animation;
|
AnimationController animation;
|
||||||
UartReader uart(serial_port);
|
|
||||||
MovementController mover(PLAYER_X, VIEW_WIDTH);
|
MovementController mover(PLAYER_X, VIEW_WIDTH);
|
||||||
SpeedController timing;
|
SpeedController timing;
|
||||||
timing.set_ground_speed(speed);
|
timing.set_ground_speed(speed);
|
||||||
|
|
||||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||||
|
|
||||||
const char *bg_file = "background_dark_inverted.txt";
|
Timer spawn_timer;
|
||||||
bool need_redraw = false;
|
spawn_timer.start();
|
||||||
|
Timer speed_timer;
|
||||||
|
speed_timer.start();
|
||||||
|
|
||||||
int anim_tick_counter = 0;
|
int anim_tick_counter = 0;
|
||||||
int tick_counter = 0;
|
int tick_counter = 0;
|
||||||
int player_speed = 6;
|
bool game_over = false;
|
||||||
|
Timer game_timer;
|
||||||
|
game_timer.start();
|
||||||
|
|
||||||
player_state.set_state(PlayerState::Run);
|
// Spawn first obstacle
|
||||||
while (true) {
|
if (speed >= 5)
|
||||||
|
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH + 10);
|
||||||
|
|
||||||
|
while (!game_over) {
|
||||||
tick_counter++;
|
tick_counter++;
|
||||||
|
|
||||||
mover.update_position(player_speed, timing.get_ground_speed());
|
if (speed_timer.elapsed_time() >= 6s) {
|
||||||
|
speed++;
|
||||||
|
timing.set_ground_speed(speed);
|
||||||
|
speed_timer.reset();
|
||||||
|
|
||||||
|
// update players animation mode
|
||||||
|
if (speed < 4)
|
||||||
|
player_state.set_state(PlayerState::Walk);
|
||||||
|
else
|
||||||
|
player_state.set_state(PlayerState::Run);
|
||||||
|
}
|
||||||
|
mover.update_position(speed, timing.get_ground_speed());
|
||||||
pos.x = mover.get_position();
|
pos.x = mover.get_position();
|
||||||
|
|
||||||
need_redraw = false;
|
// Spawn periodically
|
||||||
UartEvent uart_event = uart.poll();
|
if (speed >= 5 && spawn_timer.elapsed_time() > 6s) {
|
||||||
|
spawn_timer.reset();
|
||||||
if (uart_event == UartEvent::MessageUpdate) {
|
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH);
|
||||||
need_redraw = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uart_event == UartEvent::Triggered) {
|
// Handle input
|
||||||
|
if (button.poll() == ButtonEvent::Pressed) {
|
||||||
PlayerState current = player_state.get_state();
|
PlayerState current = player_state.get_state();
|
||||||
|
if (current == PlayerState::Walk || current == PlayerState::Run)
|
||||||
// pressing UART trigger while walking/running -> Crawl1
|
|
||||||
if (current == PlayerState::Walk || current == PlayerState::Run) {
|
|
||||||
player_state.start_crawl(PlayerState::Crawl1);
|
player_state.start_crawl(PlayerState::Crawl1);
|
||||||
}
|
else if (current == PlayerState::Crawl1)
|
||||||
// pressing again while crawling -> Crawl2
|
|
||||||
else if (current == PlayerState::Crawl1) {
|
|
||||||
player_state.start_crawl(PlayerState::Crawl2);
|
player_state.start_crawl(PlayerState::Crawl2);
|
||||||
}
|
}
|
||||||
|
|
||||||
need_redraw = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if crawl duration expired
|
|
||||||
player_state.update();
|
player_state.update();
|
||||||
|
|
||||||
if (animation.tick(speed)) {
|
bool anim_tick = animation.tick(speed);
|
||||||
|
if (anim_tick) {
|
||||||
anim_tick_counter++;
|
anim_tick_counter++;
|
||||||
need_redraw = true;
|
player_state.toggle_walk_frame(speed, anim_tick_counter);
|
||||||
|
FrameSelection frame = player_state.get_frame_selection();
|
||||||
|
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||||
|
|
||||||
|
int ground_speed = timing.get_ground_speed();
|
||||||
|
bool collision = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (!obstacle_pool[i].active)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Keep original position
|
||||||
|
int old_x = obstacle_pool[i].data.x;
|
||||||
|
int new_x = old_x - ground_speed;
|
||||||
|
|
||||||
|
// Remove if fully offscreen after movement
|
||||||
|
if (new_x + obstacle_pool[i].data.width < 0) {
|
||||||
|
obstacle_pool[i].active = false;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (need_redraw) {
|
// Swept for collision detections at speeds > 1
|
||||||
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
int left_swept = (new_x < old_x) ? new_x : old_x;
|
||||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
int right_swept = ((new_x + obstacle_pool[i].data.width) >
|
||||||
FrameSelection frame = player_state.get_frame_selection();
|
(old_x + obstacle_pool[i].data.width))
|
||||||
|
? (new_x + obstacle_pool[i].data.width)
|
||||||
|
: (old_x + obstacle_pool[i].data.width);
|
||||||
|
|
||||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
// Temporarily create a synthetic obstacle covering the swept range
|
||||||
draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index);
|
Obstacle swept_obs{};
|
||||||
|
swept_obs.x = left_swept;
|
||||||
|
swept_obs.y = obstacle_pool[i].data.y;
|
||||||
|
swept_obs.width = right_swept - left_swept;
|
||||||
|
swept_obs.height = obstacle_pool[i].data.height;
|
||||||
|
|
||||||
ThisThread::sleep_for(50ms);
|
if (check_collision(pos, frame.movement, swept_obs)) {
|
||||||
|
collision = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
obstacle_pool[i].data.x = new_x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
g_state_mutex.lock();
|
||||||
|
|
||||||
|
g_state.player_pos = draw_pos;
|
||||||
|
g_state.current_speed = speed;
|
||||||
|
g_state.movement = frame.movement;
|
||||||
|
g_state.frame_index = frame.frame_index;
|
||||||
|
g_state.background_shift = animation.get_shift();
|
||||||
|
g_state.need_redraw = true;
|
||||||
|
g_state.game_over = collision;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
g_state.obstacles[i].x = obstacle_pool[i].data.x;
|
||||||
|
g_state.obstacles[i].y = obstacle_pool[i].data.y;
|
||||||
|
g_state.obstacles[i].type = obstacle_pool[i].type;
|
||||||
|
g_state.obstacles[i].active = obstacle_pool[i].active;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_state_mutex.unlock();
|
||||||
|
|
||||||
|
if (collision)
|
||||||
|
game_over = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThisThread::sleep_for(25ms);
|
ThisThread::sleep_for(25ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
game_timer.stop();
|
||||||
|
int seconds = static_cast<int>(game_timer.elapsed_time().count() / 1000000);
|
||||||
|
|
||||||
|
g_state_mutex.lock();
|
||||||
|
g_state.elapsed_seconds = seconds;
|
||||||
|
g_state.game_over = true;
|
||||||
|
g_state_mutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_loop_thread() {
|
||||||
|
while (true) {
|
||||||
|
g_state_mutex.lock();
|
||||||
|
bool need = g_state.need_redraw;
|
||||||
|
bool over = g_state.game_over;
|
||||||
|
GameState local = g_state;
|
||||||
|
g_state.need_redraw = false;
|
||||||
|
g_state_mutex.unlock();
|
||||||
|
|
||||||
|
if (over) {
|
||||||
|
show_game_over_screen(local.elapsed_seconds, button);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (need) {
|
||||||
|
draw_mask("background", local.background_shift);
|
||||||
|
draw_character(local.player_pos.x, local.player_pos.y, local.movement,
|
||||||
|
local.frame_index);
|
||||||
|
if (local.current_speed >= 5) {
|
||||||
|
for (int i = 0; i < MAX_RENDER_OBSTACLES; i++) {
|
||||||
|
if (local.obstacles[i].active)
|
||||||
|
draw_obstacle(local.obstacles[i].x, local.obstacles[i].y,
|
||||||
|
local.obstacles[i].type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ThisThread::sleep_for(50ms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_loop(int speed) {
|
||||||
|
g_state_mutex.lock();
|
||||||
|
g_state.need_redraw = false;
|
||||||
|
g_state.game_over = false;
|
||||||
|
g_state_mutex.unlock();
|
||||||
|
|
||||||
|
int *speed_ptr = new int(speed);
|
||||||
|
|
||||||
|
Thread logic_thread(osPriorityNormal, STACK_SIZE);
|
||||||
|
Thread render_thread(osPriorityNormal, STACK_SIZE);
|
||||||
|
|
||||||
|
logic_thread.start(callback(logic_loop, (void *)speed_ptr));
|
||||||
|
render_thread.start(render_loop_thread);
|
||||||
|
|
||||||
|
logic_thread.join();
|
||||||
|
render_thread.join();
|
||||||
|
|
||||||
|
delete speed_ptr;
|
||||||
}
|
}
|
||||||
|
|||||||
51
semestralka1/src/render/obstacle.cpp
Normal file
51
semestralka1/src/render/obstacle.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
// src/render/obstacle.cpp
|
||||||
|
#include "obstacle.h"
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
// Draws an obstacle using bottom‑based world coordinates.
|
||||||
|
void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
||||||
|
const char **obstacle_frame = nullptr;
|
||||||
|
int frame_total_height = 0;
|
||||||
|
|
||||||
|
int collision_top_offset = 0;
|
||||||
|
int collision_height = 0;
|
||||||
|
int collision_left_offset = 0;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case CrawlObstacleType::Crawl1:
|
||||||
|
obstacle_frame = OBSTACLE_CRAWL1_FRAME;
|
||||||
|
frame_total_height = OBSTACLE_CRAWL1_FRAME_HEIGHT;
|
||||||
|
collision_top_offset = OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET;
|
||||||
|
collision_height = OBSTACLE_CRAWL1_COLLISION_HEIGHT;
|
||||||
|
collision_left_offset = OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CrawlObstacleType::Crawl2:
|
||||||
|
obstacle_frame = OBSTACLE_CRAWL2_FRAME;
|
||||||
|
frame_total_height = OBSTACLE_CRAWL2_FRAME_HEIGHT;
|
||||||
|
collision_top_offset = 0;
|
||||||
|
collision_height = OBSTACLE_CRAWL2_COLLISION_HEIGHT;
|
||||||
|
collision_left_offset = OBSTACLE_CRAWL2_COLLISION_LEFT_OFFSET;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return; // no rendering for invalid type
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert bottom‑based coordinates to screen coordinates.
|
||||||
|
const int frame_row_index_bottom = collision_top_offset + collision_height - 1;
|
||||||
|
const int frame_top_world_y = y + frame_row_index_bottom;
|
||||||
|
const int frame_top_screen_row = VIEW_HEIGHT - frame_top_world_y;
|
||||||
|
|
||||||
|
const int frame_screen_x = x - collision_left_offset;
|
||||||
|
|
||||||
|
for (int i = 0; i < frame_total_height; i++) {
|
||||||
|
const int screen_row = frame_top_screen_row + i;
|
||||||
|
if (screen_row < 1 || screen_row > VIEW_HEIGHT) continue;
|
||||||
|
printf("\033[%d;%dH%s", screen_row, frame_screen_x + 1, obstacle_frame[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
7
semestralka1/src/render/obstacle.h
Normal file
7
semestralka1/src/render/obstacle.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// src/render/obstacle.h
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
|
||||||
|
// Draw the obstacle ASCII block starting at given (x, y)
|
||||||
|
void draw_obstacle(int x, int y, CrawlObstacleType type);
|
||||||
@@ -13,7 +13,5 @@ public:
|
|||||||
int get_ground_speed() const { return ground_speed; }
|
int get_ground_speed() const { return ground_speed; }
|
||||||
|
|
||||||
// Calculates how many frames to advance for current tick.
|
// Calculates how many frames to advance for current tick.
|
||||||
// Takes object’s current speed and a global tick counter
|
|
||||||
// to automatically handle slower "wait" behavior.
|
|
||||||
int frame_advance_for(int object_speed, int tick_counter) const;
|
int frame_advance_for(int object_speed, int tick_counter) const;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user