button scuffed
This commit is contained in:
@@ -3,9 +3,10 @@
|
|||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "../render/background.h"
|
#include "../render/background.h"
|
||||||
#include "../assets/background_frame.h"
|
#include "../assets/background_frame.h"
|
||||||
|
#include "../hardware/button.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
void show_game_over_screen(int seconds) {
|
void show_game_over_screen(int seconds, ButtonHandler& button) {
|
||||||
printf("\033[2J\033[H");
|
printf("\033[2J\033[H");
|
||||||
|
|
||||||
// 34 znakov vo vypise
|
// 34 znakov vo vypise
|
||||||
@@ -34,20 +35,13 @@ void show_game_over_screen(int seconds) {
|
|||||||
|
|
||||||
for (int i = 0; i < start_col; i++) printf(" ");
|
for (int i = 0; i < start_col; i++) printf(" ");
|
||||||
printf("==================================\r\n");
|
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);
|
fflush(stdout);
|
||||||
|
|
||||||
DigitalIn restart_button(BUTTON1);
|
while (button.poll() != ButtonEvent::Pressed)
|
||||||
|
ThisThread::sleep_for(25ms);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\033[2J\033[H");
|
printf("\033[2J\033[H");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
// src/game/game_over.h
|
// src/game/game_over.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
|
#include "../hardware/button.h"
|
||||||
|
|
||||||
// Displays game over message with elapsed time.
|
// Displays game over message with elapsed time.
|
||||||
// Takes a Timer that was measuring game duration.
|
// Takes a Timer that was measuring game duration.
|
||||||
void show_game_over_screen(int seconds);
|
void show_game_over_screen(int seconds, ButtonHandler& button);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#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;
|
||||||
@@ -30,7 +31,6 @@ void logic_loop(void *arg) {
|
|||||||
int speed = *(int *)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);
|
||||||
@@ -89,30 +89,52 @@ void logic_loop(void *arg) {
|
|||||||
bool anim_tick = animation.tick(speed);
|
bool anim_tick = animation.tick(speed);
|
||||||
if (anim_tick) {
|
if (anim_tick) {
|
||||||
anim_tick_counter++;
|
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);
|
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 = 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
|
int ground_speed = timing.get_ground_speed();
|
||||||
bool collision = false;
|
bool collision = false;
|
||||||
|
|
||||||
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)
|
||||||
check_collision(pos, frame.movement, obstacle_pool[i].data)) {
|
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;
|
collision = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply the actual movement after check
|
||||||
|
obstacle_pool[i].data.x = new_x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
g_state_mutex.lock();
|
g_state_mutex.lock();
|
||||||
|
|
||||||
g_state.player_pos = draw_pos;
|
g_state.player_pos = draw_pos;
|
||||||
@@ -158,7 +180,7 @@ void render_loop_thread() {
|
|||||||
g_state_mutex.unlock();
|
g_state_mutex.unlock();
|
||||||
|
|
||||||
if (over) {
|
if (over) {
|
||||||
show_game_over_screen(local.elapsed_seconds);
|
show_game_over_screen(local.elapsed_seconds, button);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user