diff --git a/semestralka1/src/game/game_over.cpp b/semestralka1/src/game/game_over.cpp index e48b39f..80d7430 100644 --- a/semestralka1/src/game/game_over.cpp +++ b/semestralka1/src/game/game_over.cpp @@ -3,9 +3,10 @@ #include "mbed.h" #include "../render/background.h" #include "../assets/background_frame.h" +#include "../hardware/button.h" #include -void show_game_over_screen(int seconds) { +void show_game_over_screen(int seconds, ButtonHandler& button) { printf("\033[2J\033[H"); // 34 znakov vo vypise @@ -34,20 +35,13 @@ void show_game_over_screen(int seconds) { for (int i = 0; i < start_col; i++) printf(" "); printf("==================================\r\n"); - printf(" Stlacte tlacidlo pre restart. \r\n"); + for (int i = 0; i < start_col; i++) printf(" "); + printf("Stlacte 2x po sebe tlacidlo pre restart\r\n"); fflush(stdout); - DigitalIn restart_button(BUTTON1); - - int initial_state = restart_button.read(); - while (restart_button.read() == initial_state) { - ThisThread::sleep_for(50ms); - } - int new_state = restart_button.read(); - while (restart_button.read() == new_state) { - ThisThread::sleep_for(50ms); - } + while (button.poll() != ButtonEvent::Pressed) + ThisThread::sleep_for(25ms); printf("\033[2J\033[H"); fflush(stdout); diff --git a/semestralka1/src/game/game_over.h b/semestralka1/src/game/game_over.h index d4b0db6..f1cd0a9 100644 --- a/semestralka1/src/game/game_over.h +++ b/semestralka1/src/game/game_over.h @@ -1,7 +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); +void show_game_over_screen(int seconds, ButtonHandler& button); diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index d03079d..864200d 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -19,6 +19,7 @@ #include "../game/obstacle_system.h" extern DigitalOut led; +ButtonHandler button(BUTTON1, LED1); // Constants // constexpr int PLAYER_X = 9; @@ -30,7 +31,6 @@ void logic_loop(void *arg) { int speed = *(int *)arg; MovementState player_state; AnimationController animation; - ButtonHandler button; MovementController mover(PLAYER_X, VIEW_WIDTH); SpeedController timing; timing.set_ground_speed(speed); @@ -89,30 +89,52 @@ void logic_loop(void *arg) { bool anim_tick = animation.tick(speed); if (anim_tick) { anim_tick_counter++; - // move obstacles - int ground_speed = timing.get_ground_speed(); - for (int i = 0; i < MAX_OBSTACLES; i++) { - if (obstacle_pool[i].active) { - obstacle_pool[i].data.x -= ground_speed; - if (obstacle_pool[i].data.x + obstacle_pool[i].data.width < 0) - obstacle_pool[i].active = false; - } - } - 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); - // check collision + int ground_speed = timing.get_ground_speed(); bool collision = false; + for (int i = 0; i < MAX_OBSTACLES; i++) { - if (obstacle_pool[i].active && - check_collision(pos, frame.movement, obstacle_pool[i].data)) { + 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; + } + + // Compute swept region = min..max + 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; + + // Perform continuous collision check + if (check_collision(pos, frame.movement, swept_obs)) { collision = true; break; } + + // Apply the actual movement after check + obstacle_pool[i].data.x = new_x; } + g_state_mutex.lock(); g_state.player_pos = draw_pos; @@ -158,7 +180,7 @@ void render_loop_thread() { g_state_mutex.unlock(); if (over) { - show_game_over_screen(local.elapsed_seconds); + show_game_over_screen(local.elapsed_seconds, button); break; }