Compare commits
2 Commits
42d07e9715
...
17c53ace99
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17c53ace99 | ||
|
|
50f523b52d |
@@ -23,6 +23,7 @@ struct GameState {
|
|||||||
|
|
||||||
int background_shift;
|
int background_shift;
|
||||||
|
|
||||||
|
int current_speed;
|
||||||
ObstacleRenderData obstacles[MAX_RENDER_OBSTACLES];
|
ObstacleRenderData obstacles[MAX_RENDER_OBSTACLES];
|
||||||
|
|
||||||
bool game_over;
|
bool game_over;
|
||||||
|
|||||||
@@ -4,14 +4,12 @@
|
|||||||
#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];
|
||||||
|
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -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,5 @@ 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);
|
render_loop(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,35 +27,49 @@ constexpr int STACK_SIZE = 4096;
|
|||||||
|
|
||||||
Thread logic_thread(osPriorityNormal, STACK_SIZE);
|
Thread logic_thread(osPriorityNormal, STACK_SIZE);
|
||||||
Thread render_thread(osPriorityNormal, STACK_SIZE);
|
Thread render_thread(osPriorityNormal, STACK_SIZE);
|
||||||
void logic_loop() {
|
void logic_loop(void *arg) {
|
||||||
|
int speed = *(int *)arg;
|
||||||
MovementState player_state;
|
MovementState player_state;
|
||||||
AnimationController animation;
|
AnimationController animation;
|
||||||
ButtonHandler button;
|
ButtonHandler button;
|
||||||
MovementController mover(PLAYER_X, VIEW_WIDTH);
|
MovementController mover(PLAYER_X, VIEW_WIDTH);
|
||||||
SpeedController timing;
|
SpeedController timing;
|
||||||
timing.set_ground_speed(6);
|
timing.set_ground_speed(speed);
|
||||||
|
|
||||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||||
player_state.set_state(PlayerState::Walk);
|
|
||||||
|
|
||||||
Timer spawn_timer;
|
Timer spawn_timer;
|
||||||
spawn_timer.start();
|
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;
|
||||||
bool game_over = false;
|
bool game_over = false;
|
||||||
|
|
||||||
// Spawn first obstacle
|
// Spawn first obstacle
|
||||||
|
if (speed >= 5)
|
||||||
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH + 10);
|
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH + 10);
|
||||||
|
|
||||||
while (!game_over) {
|
while (!game_over) {
|
||||||
tick_counter++;
|
tick_counter++;
|
||||||
|
|
||||||
mover.update_position(6, timing.get_ground_speed());
|
if (speed_timer.elapsed_time() >= 6s) {
|
||||||
|
speed++;
|
||||||
|
timing.set_ground_speed(speed);
|
||||||
|
speed_timer.reset();
|
||||||
|
|
||||||
|
// update player's 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();
|
||||||
|
|
||||||
// Spawn periodically
|
// Spawn periodically
|
||||||
if (spawn_timer.elapsed_time() > 2s) {
|
if (speed >= 5 && spawn_timer.elapsed_time() > 6s) {
|
||||||
spawn_timer.reset();
|
spawn_timer.reset();
|
||||||
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH);
|
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH);
|
||||||
}
|
}
|
||||||
@@ -71,7 +85,7 @@ void logic_loop() {
|
|||||||
|
|
||||||
player_state.update();
|
player_state.update();
|
||||||
|
|
||||||
bool anim_tick = animation.tick(6);
|
bool anim_tick = animation.tick(speed);
|
||||||
if (anim_tick) {
|
if (anim_tick) {
|
||||||
anim_tick_counter++;
|
anim_tick_counter++;
|
||||||
// move obstacles
|
// move obstacles
|
||||||
@@ -84,10 +98,9 @@ void logic_loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
player_state.toggle_walk_frame(6, anim_tick_counter);
|
player_state.toggle_walk_frame(speed, anim_tick_counter);
|
||||||
FrameSelection frame = player_state.get_frame_selection();
|
FrameSelection frame = player_state.get_frame_selection();
|
||||||
CharacterPosition draw_pos =
|
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||||
get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
|
||||||
|
|
||||||
// check collision
|
// check collision
|
||||||
bool collision = false;
|
bool collision = false;
|
||||||
@@ -102,6 +115,7 @@ void logic_loop() {
|
|||||||
g_state_mutex.lock();
|
g_state_mutex.lock();
|
||||||
|
|
||||||
g_state.player_pos = draw_pos;
|
g_state.player_pos = draw_pos;
|
||||||
|
g_state.current_speed = speed;
|
||||||
g_state.movement = frame.movement;
|
g_state.movement = frame.movement;
|
||||||
g_state.frame_index = frame.frame_index;
|
g_state.frame_index = frame.frame_index;
|
||||||
g_state.background_shift = animation.get_shift();
|
g_state.background_shift = animation.get_shift();
|
||||||
@@ -149,12 +163,14 @@ void render_loop_thread() {
|
|||||||
draw_mask("background", local.background_shift);
|
draw_mask("background", local.background_shift);
|
||||||
draw_character(local.player_pos.x, local.player_pos.y, local.movement,
|
draw_character(local.player_pos.x, local.player_pos.y, local.movement,
|
||||||
local.frame_index);
|
local.frame_index);
|
||||||
|
if (local.current_speed >= 5) {
|
||||||
for (int i = 0; i < MAX_RENDER_OBSTACLES; i++) {
|
for (int i = 0; i < MAX_RENDER_OBSTACLES; i++) {
|
||||||
if (local.obstacles[i].active)
|
if (local.obstacles[i].active)
|
||||||
draw_obstacle(local.obstacles[i].x, local.obstacles[i].y,
|
draw_obstacle(local.obstacles[i].x, local.obstacles[i].y,
|
||||||
local.obstacles[i].type);
|
local.obstacles[i].type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ThisThread::sleep_for(50ms);
|
ThisThread::sleep_for(50ms);
|
||||||
}
|
}
|
||||||
@@ -166,9 +182,13 @@ void render_loop(int speed) {
|
|||||||
g_state.game_over = false;
|
g_state.game_over = false;
|
||||||
g_state_mutex.unlock();
|
g_state_mutex.unlock();
|
||||||
|
|
||||||
logic_thread.start(logic_loop);
|
int *speed_ptr = new int(speed);
|
||||||
|
|
||||||
|
logic_thread.start(callback(logic_loop, (void *)speed_ptr));
|
||||||
render_thread.start(render_loop_thread);
|
render_thread.start(render_loop_thread);
|
||||||
|
|
||||||
logic_thread.join();
|
logic_thread.join();
|
||||||
render_thread.join();
|
render_thread.join();
|
||||||
|
|
||||||
|
delete speed_ptr;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user