speed bug fixed now properly well
This commit is contained in:
@@ -23,6 +23,7 @@ struct GameState {
|
||||
|
||||
int background_shift;
|
||||
|
||||
int current_speed;
|
||||
ObstacleRenderData obstacles[MAX_RENDER_OBSTACLES];
|
||||
|
||||
bool game_over;
|
||||
|
||||
@@ -120,16 +120,6 @@ void MovementState::set_state(PlayerState 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;
|
||||
}
|
||||
|
||||
void MovementState::toggle_walk_frame(float player_speed, int tick_counter) {
|
||||
switch (current_state) {
|
||||
case PlayerState::Run:
|
||||
|
||||
@@ -27,7 +27,6 @@ public:
|
||||
void update();
|
||||
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);
|
||||
|
||||
FrameSelection get_frame_selection() const;
|
||||
|
||||
@@ -21,5 +21,5 @@ int main(void) {
|
||||
serial_port.set_format(8, BufferedSerial::None, 1);
|
||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||
|
||||
render_loop(2);
|
||||
render_loop(1);
|
||||
}
|
||||
|
||||
@@ -37,29 +37,39 @@ void logic_loop(void *arg) {
|
||||
timing.set_ground_speed(speed);
|
||||
|
||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||
if (speed < 3)
|
||||
player_state.set_state(PlayerState::Run);
|
||||
else
|
||||
player_state.set_state(PlayerState::Walk);
|
||||
|
||||
Timer spawn_timer;
|
||||
spawn_timer.start();
|
||||
Timer speed_timer;
|
||||
speed_timer.start();
|
||||
|
||||
int anim_tick_counter = 0;
|
||||
int tick_counter = 0;
|
||||
bool game_over = false;
|
||||
|
||||
// Spawn first obstacle
|
||||
if (speed >= 5)
|
||||
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH + 10);
|
||||
|
||||
while (!game_over) {
|
||||
tick_counter++;
|
||||
|
||||
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();
|
||||
|
||||
// Spawn periodically
|
||||
if (spawn_timer.elapsed_time() > 5s) {
|
||||
if (speed >= 5 && spawn_timer.elapsed_time() > 6s) {
|
||||
spawn_timer.reset();
|
||||
spawn_obstacle(CrawlObstacleType::Crawl1, VIEW_WIDTH);
|
||||
}
|
||||
@@ -90,8 +100,7 @@ void logic_loop(void *arg) {
|
||||
|
||||
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);
|
||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||
|
||||
// check collision
|
||||
bool collision = false;
|
||||
@@ -106,6 +115,7 @@ void logic_loop(void *arg) {
|
||||
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();
|
||||
@@ -153,12 +163,14 @@ void render_loop_thread() {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user