Compare commits
8 Commits
8382d1caec
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b10d13695 | ||
|
|
74dca27c06 | ||
|
|
786ead2ffa | ||
|
|
e5091a33e7 | ||
|
|
3cef68142d | ||
|
|
17c53ace99 | ||
|
|
50f523b52d | ||
|
|
42d07e9715 |
@@ -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; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "../assets/character_run_frames.h"
|
#include "../assets/character_run_frames.h"
|
||||||
#include "../assets/character_crawl_frames.h"
|
#include "../assets/character_crawl_frames.h"
|
||||||
|
|
||||||
// Obstacle representation
|
|
||||||
struct Obstacle {
|
struct Obstacle {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
|||||||
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;
|
||||||
@@ -4,18 +4,16 @@
|
|||||||
#include "../assets/obstacle_crawl_frames.h"
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
#include "../assets/background_frame.h"
|
#include "../assets/background_frame.h"
|
||||||
|
|
||||||
// Movable obstacle
|
|
||||||
struct MovingObstacle {
|
struct MovingObstacle {
|
||||||
Obstacle data;
|
Obstacle data;
|
||||||
CrawlObstacleType type;
|
CrawlObstacleType type;
|
||||||
bool active;
|
bool active;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fixed obstacle pool
|
|
||||||
constexpr int MAX_OBSTACLES = 6;
|
constexpr int MAX_OBSTACLES = 6;
|
||||||
static MovingObstacle obstacle_pool[MAX_OBSTACLES];
|
static MovingObstacle obstacle_pool[MAX_OBSTACLES];
|
||||||
|
|
||||||
// Create / reset obstacle in one slot
|
// Create/reset obstacle
|
||||||
inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
|
inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
|
||||||
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
if (!obstacle_pool[i].active) {
|
if (!obstacle_pool[i].active) {
|
||||||
@@ -45,7 +43,7 @@ inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update X positions; mark inactive if offscreen
|
// Update X positions, mark inactive if offscreen
|
||||||
inline void update_obstacles(int shift_speed) {
|
inline void update_obstacles(int shift_speed) {
|
||||||
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
if (!obstacle_pool[i].active) continue;
|
if (!obstacle_pool[i].active) continue;
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ class ObstacleSystem {
|
|||||||
public:
|
public:
|
||||||
ObstacleSystem() : tick_counter_(0), spawn_index_(0) {}
|
ObstacleSystem() : tick_counter_(0), spawn_index_(0) {}
|
||||||
|
|
||||||
// Called once per frame
|
|
||||||
bool update_and_draw(const CharacterPosition &player_pos,
|
bool update_and_draw(const CharacterPosition &player_pos,
|
||||||
MovementType player_movement) {
|
MovementType player_movement) {
|
||||||
// Update timers, move existing obstacles
|
// Update timers, move existing obstacles
|
||||||
@@ -30,7 +29,7 @@ public:
|
|||||||
spawn_obstacle(type, VIEW_WIDTH - w - 1);
|
spawn_obstacle(type, VIEW_WIDTH - w - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw & collision check
|
// Draw n collision check
|
||||||
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
if (!obstacle_pool[i].active)
|
if (!obstacle_pool[i].active)
|
||||||
continue;
|
continue;
|
||||||
@@ -47,7 +46,6 @@ public:
|
|||||||
|
|
||||||
draw_clipped_obstacle(obstacle_pool[i], frame, height);
|
draw_clipped_obstacle(obstacle_pool[i], frame, height);
|
||||||
|
|
||||||
// Collision check
|
|
||||||
if (check_collision(player_pos, player_movement,
|
if (check_collision(player_pos, player_movement,
|
||||||
obstacle_pool[i].data)) {
|
obstacle_pool[i].data)) {
|
||||||
printf("\033[2J\033[H");
|
printf("\033[2J\033[H");
|
||||||
|
|||||||
@@ -120,17 +120,6 @@ void MovementState::set_state(PlayerState s) {
|
|||||||
current_state = s;
|
current_state = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MovementState::set_motion_state_for_speed(int player_speed, int ground_speed) {
|
|
||||||
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 MovementState::toggle_walk_frame(float player_speed, int tick_counter) {
|
void MovementState::toggle_walk_frame(float player_speed, int tick_counter) {
|
||||||
switch (current_state) {
|
switch (current_state) {
|
||||||
case PlayerState::Run:
|
case PlayerState::Run:
|
||||||
|
|||||||
@@ -27,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;
|
||||||
|
|||||||
@@ -21,5 +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);
|
||||||
|
|
||||||
render_loop(6);
|
while (true) {
|
||||||
|
render_loop(4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "background.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 "../game/collision.h"
|
#include "../game/collision.h"
|
||||||
@@ -17,117 +19,202 @@
|
|||||||
#include "../game/obstacle_system.h"
|
#include "../game/obstacle_system.h"
|
||||||
|
|
||||||
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 render_loop(int speed) {
|
void logic_loop(void *arg) {
|
||||||
|
int speed = *(int *)arg;
|
||||||
MovementState player_state;
|
MovementState player_state;
|
||||||
AnimationController animation;
|
AnimationController animation;
|
||||||
ButtonHandler button;
|
|
||||||
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;
|
bool game_over = false;
|
||||||
|
Timer game_timer;
|
||||||
|
game_timer.start();
|
||||||
|
|
||||||
player_state.set_state(PlayerState::Walk);
|
// Spawn first obstacle
|
||||||
|
if (speed >= 5)
|
||||||
CrawlObstacleType type = CrawlObstacleType::Crawl1;
|
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH + 10);
|
||||||
int start_x = VIEW_WIDTH + 10;
|
|
||||||
spawn_obstacle(type, start_x);
|
|
||||||
|
|
||||||
Timer spawn_timer;
|
|
||||||
spawn_timer.start();
|
|
||||||
|
|
||||||
while (!game_over) {
|
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();
|
||||||
|
|
||||||
if (spawn_timer.elapsed_time() > 2s) {
|
// Spawn periodically
|
||||||
|
if (speed >= 5 && spawn_timer.elapsed_time() > 6s) {
|
||||||
spawn_timer.reset();
|
spawn_timer.reset();
|
||||||
CrawlObstacleType s_type = CrawlObstacleType::Crawl1;
|
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH);
|
||||||
int spawn_x = VIEW_WIDTH;
|
|
||||||
int idx = spawn_obstacle(s_type, spawn_x);
|
|
||||||
if (idx >= 0) {
|
|
||||||
obstacle_pool[idx].active = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
need_redraw = false;
|
// Handle input
|
||||||
ButtonEvent button_event = button.poll();
|
if (button.poll() == ButtonEvent::Pressed) {
|
||||||
|
|
||||||
if (button_event == ButtonEvent::Pressed) {
|
|
||||||
PlayerState current = player_state.get_state();
|
PlayerState current = player_state.get_state();
|
||||||
|
if (current == PlayerState::Walk || current == PlayerState::Run)
|
||||||
if (current == PlayerState::Walk || current == PlayerState::Run) {
|
|
||||||
player_state.start_crawl(PlayerState::Crawl1);
|
player_state.start_crawl(PlayerState::Crawl1);
|
||||||
} else if (current == PlayerState::Crawl1) {
|
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++;
|
||||||
int ground_speed = timing.get_ground_speed();
|
player_state.toggle_walk_frame(speed, anim_tick_counter);
|
||||||
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
|
||||||
if (obstacle_pool[i].active) {
|
|
||||||
obstacle_pool[i].data.x -= ground_speed;
|
|
||||||
// deactivate when offscreen
|
|
||||||
if (obstacle_pool[i].data.x + obstacle_pool[i].data.width < 0)
|
|
||||||
obstacle_pool[i].active = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
need_redraw = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (need_redraw) {
|
|
||||||
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
|
||||||
draw_mask(bg_file, animation.get_shift());
|
|
||||||
FrameSelection frame = player_state.get_frame_selection();
|
FrameSelection frame = player_state.get_frame_selection();
|
||||||
|
|
||||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||||
draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index);
|
|
||||||
|
|
||||||
// for (int i = 0; i < MAX_OBSTACLES; i++) {
|
int ground_speed = timing.get_ground_speed();
|
||||||
// if (!obstacle_pool[i].active)
|
bool collision = false;
|
||||||
// continue;
|
|
||||||
|
|
||||||
// draw_obstacle(obstacle_pool[i].data.x, obstacle_pool[i].data.y,
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
// obstacle_pool[i].type);
|
if (!obstacle_pool[i].active)
|
||||||
|
continue;
|
||||||
|
|
||||||
// if (check_collision(pos, frame.movement, obstacle_pool[i].data)) {
|
// Keep original position
|
||||||
// printf("\033[2J\033[H");
|
int old_x = obstacle_pool[i].data.x;
|
||||||
// printf("GAME OVER\r\n");
|
int new_x = old_x - ground_speed;
|
||||||
// game_over = true;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (game_over)
|
// Remove if fully offscreen after movement
|
||||||
|
if (new_x + obstacle_pool[i].data.width < 0) {
|
||||||
|
obstacle_pool[i].active = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swept for collision detections at speeds > 1
|
||||||
|
int left_swept = (new_x < old_x) ? new_x : old_x;
|
||||||
|
int right_swept = ((new_x + obstacle_pool[i].data.width) >
|
||||||
|
(old_x + obstacle_pool[i].data.width))
|
||||||
|
? (new_x + obstacle_pool[i].data.width)
|
||||||
|
: (old_x + obstacle_pool[i].data.width);
|
||||||
|
|
||||||
|
// Temporarily create a synthetic obstacle covering the swept range
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (check_collision(pos, frame.movement, swept_obs)) {
|
||||||
|
collision = true;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
ThisThread::sleep_for(50ms);
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,13 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
// Draws an obstacle using bottom‑based world coordinates.
|
// Draws an obstacle using bottom‑based world coordinates.
|
||||||
// x = left edge of the collision box in world space
|
|
||||||
// y = bottom edge of the collision box in world space (0 = ground)
|
|
||||||
void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
||||||
const char **obstacle_frame = nullptr;
|
const char **obstacle_frame = nullptr;
|
||||||
int frame_total_height = 0;
|
int frame_total_height = 0;
|
||||||
|
|
||||||
// Collision‑model parameters for this obstacle
|
int collision_top_offset = 0;
|
||||||
int collision_top_offset = 0; // rows from visual top to top of collision box
|
int collision_height = 0;
|
||||||
int collision_height = 0; // rows tall
|
int collision_left_offset = 0;
|
||||||
int collision_left_offset = 0; // columns from visual left to collision left
|
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case CrawlObstacleType::Crawl1:
|
case CrawlObstacleType::Crawl1:
|
||||||
@@ -37,13 +34,11 @@ void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
|||||||
return; // no rendering for invalid type
|
return; // no rendering for invalid type
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert bottom‑based (world) coordinates to screen coordinates.
|
// Convert bottom‑based coordinates to screen coordinates.
|
||||||
// Y of the collision bottom is given; translate to where to start drawing the visual frame.
|
|
||||||
const int frame_row_index_bottom = collision_top_offset + collision_height - 1;
|
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_world_y = y + frame_row_index_bottom;
|
||||||
const int frame_top_screen_row = VIEW_HEIGHT - frame_top_world_y;
|
const int frame_top_screen_row = VIEW_HEIGHT - frame_top_world_y;
|
||||||
|
|
||||||
// X of the collision box is given; shift left to the visual frame's starting column.
|
|
||||||
const int frame_screen_x = x - collision_left_offset;
|
const int frame_screen_x = x - collision_left_offset;
|
||||||
|
|
||||||
for (int i = 0; i < frame_total_height; i++) {
|
for (int i = 0; i < frame_total_height; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user