Compare commits
3 Commits
ca7eac70e4
...
8382d1caec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8382d1caec | ||
|
|
165540588f | ||
|
|
0083fff73a |
@@ -13,7 +13,6 @@ struct Obstacle {
|
|||||||
int height;
|
int height;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Axis-aligned bounding-box collision
|
|
||||||
inline bool check_collision(const CharacterPosition& player,
|
inline bool check_collision(const CharacterPosition& player,
|
||||||
MovementType movement,
|
MovementType movement,
|
||||||
const Obstacle& obs) {
|
const Obstacle& obs) {
|
||||||
|
|||||||
@@ -102,10 +102,10 @@ void MovementState::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MovementState::start_crawl(PlayerState crawl_type) {
|
void MovementState::start_crawl(PlayerState crawl_type) {
|
||||||
if (crawl_type != PlayerState::Crawl1 && crawl_type != PlayerState::Crawl2)
|
if (current_state != PlayerState::Crawl1 && current_state != PlayerState::Crawl2) {
|
||||||
return;
|
|
||||||
|
|
||||||
previous_state = current_state;
|
previous_state = current_state;
|
||||||
|
}
|
||||||
|
|
||||||
current_state = crawl_type;
|
current_state = crawl_type;
|
||||||
state_timer.stop();
|
state_timer.stop();
|
||||||
state_timer.reset();
|
state_timer.reset();
|
||||||
|
|||||||
33
semestralka1/src/hardware/button.cpp
Normal file
33
semestralka1/src/hardware/button.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// src/hardware/button.cpp
|
||||||
|
#include "button.h"
|
||||||
|
|
||||||
|
ButtonHandler::ButtonHandler(PinName pin, PinName led_pin) : button(pin), led(led_pin, 0) {
|
||||||
|
// ISR for button press (falling edge)
|
||||||
|
button.fall(callback(this, &ButtonHandler::on_pressed));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ButtonHandler::on_pressed() {
|
||||||
|
pressed_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ButtonEvent ButtonHandler::poll() {
|
||||||
|
static Timer blink_timer;
|
||||||
|
static bool blinking = false;
|
||||||
|
|
||||||
|
if (pressed_flag) {
|
||||||
|
pressed_flag = false;
|
||||||
|
led = 1;
|
||||||
|
blink_timer.reset();
|
||||||
|
blink_timer.start();
|
||||||
|
blinking = true;
|
||||||
|
return ButtonEvent::Pressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blinking && blink_timer.elapsed_time() >= 500ms) {
|
||||||
|
led = 0;
|
||||||
|
blink_timer.stop();
|
||||||
|
blinking = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ButtonEvent::None;
|
||||||
|
}
|
||||||
23
semestralka1/src/hardware/button.h
Normal file
23
semestralka1/src/hardware/button.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// src/hardware/button.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
|
||||||
|
enum class ButtonEvent {
|
||||||
|
None = 0,
|
||||||
|
Pressed = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
class ButtonHandler {
|
||||||
|
private:
|
||||||
|
InterruptIn button;
|
||||||
|
DigitalOut led;
|
||||||
|
volatile bool pressed_flag = false;
|
||||||
|
volatile bool blink_flag = false;
|
||||||
|
|
||||||
|
void on_pressed();
|
||||||
|
|
||||||
|
public:
|
||||||
|
ButtonHandler(PinName pin = BUTTON1, PinName led_pin = LED1);
|
||||||
|
|
||||||
|
ButtonEvent poll();
|
||||||
|
};
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
// src/hardware/uart.cpp
|
|
||||||
#include "uart.h"
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
UartReader::UartReader(BufferedSerial &serial) : serial_port(serial) {
|
|
||||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
UartEvent UartReader::poll() {
|
|
||||||
bool changed = false;
|
|
||||||
bool triggered = false;
|
|
||||||
|
|
||||||
// cita spravu z uartu
|
|
||||||
if (serial_port.readable()) {
|
|
||||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
||||||
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
|
||||||
if (num > 0) {
|
|
||||||
strncpy(message, rx_buffer, sizeof(message) - 1);
|
|
||||||
message_active = true;
|
|
||||||
changed = true;
|
|
||||||
triggered = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// casovac na 1s zobrazenia spravy
|
|
||||||
if (!timer_started) {
|
|
||||||
msg_timer.start();
|
|
||||||
timer_started = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// po jednu sekundu sa sprava zobrazi
|
|
||||||
if (message_active && msg_timer.elapsed_time() > MESSAGE_DISPLAY_DURATION) {
|
|
||||||
message_active = false;
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
msg_timer.reset();
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (triggered)
|
|
||||||
return UartEvent::Triggered;
|
|
||||||
if (changed)
|
|
||||||
return UartEvent::MessageUpdate;
|
|
||||||
return UartEvent::NoChange;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* UartReader::get_message() const {
|
|
||||||
return message_active ? message : nullptr;
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
// src/hardware/uart.h
|
|
||||||
#pragma once
|
|
||||||
#include "mbed.h"
|
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
constexpr size_t UART_BUFFER_SIZE = 64;
|
|
||||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
|
||||||
|
|
||||||
enum class UartEvent {
|
|
||||||
NoChange = 0, // No new data
|
|
||||||
MessageUpdate = 1, // Message changed (received or expired)
|
|
||||||
Triggered = 2 // New message received (trigger action)
|
|
||||||
};
|
|
||||||
|
|
||||||
class UartReader {
|
|
||||||
private:
|
|
||||||
BufferedSerial &serial_port;
|
|
||||||
|
|
||||||
char rx_buffer[UART_BUFFER_SIZE];
|
|
||||||
char message[UART_BUFFER_SIZE];
|
|
||||||
bool message_active = false;
|
|
||||||
|
|
||||||
Timer msg_timer;
|
|
||||||
bool timer_started = false;
|
|
||||||
|
|
||||||
public:
|
|
||||||
UartReader(BufferedSerial &serial);
|
|
||||||
|
|
||||||
// Poll for UART events
|
|
||||||
UartEvent poll();
|
|
||||||
|
|
||||||
// Get current message (nullptr if no active message)
|
|
||||||
const char* get_message() const;
|
|
||||||
bool has_message() const { return message_active; }
|
|
||||||
};
|
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text) {
|
void draw_mask(const char *unused_filename, int shift) {
|
||||||
// Terminal clear + home
|
// Terminal clear + home
|
||||||
printf("\033[2J\033[H");
|
printf("\033[2J\033[H");
|
||||||
|
|
||||||
@@ -25,9 +25,5 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
|
|||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text && text[0] != '\0') {
|
|
||||||
printf("\r\n[RX] %s\r\n", text);
|
|
||||||
}
|
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,4 @@
|
|||||||
constexpr int VIEW_WIDTH = 90;
|
constexpr int VIEW_WIDTH = 90;
|
||||||
|
|
||||||
// Draws the ASCII art background with optional message
|
// Draws the ASCII art background with optional message
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text = nullptr);
|
void draw_mask(const char *unused_filename, int shift);
|
||||||
|
|||||||
@@ -5,17 +5,17 @@
|
|||||||
#include "background.h"
|
#include "background.h"
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "background.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"
|
||||||
#include "../hardware/uart.h"
|
#include "../hardware/button.h"
|
||||||
#include "../render/player.h"
|
#include "../render/player.h"
|
||||||
#include "../render/obstacle.h"
|
#include "../render/obstacle.h"
|
||||||
#include "../timing/speed_controller.h"
|
#include "../timing/speed_controller.h"
|
||||||
#include "../timing/movement_controller.h"
|
#include "../timing/movement_controller.h"
|
||||||
#include "../game/obstacle_system.h"
|
#include "../game/obstacle_system.h"
|
||||||
|
|
||||||
extern BufferedSerial serial_port;
|
|
||||||
extern DigitalOut led;
|
extern DigitalOut led;
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
@@ -23,12 +23,10 @@ extern DigitalOut led;
|
|||||||
constexpr int PLAYER_X = 29;
|
constexpr int PLAYER_X = 29;
|
||||||
constexpr int PLAYER_Y = 6;
|
constexpr int PLAYER_Y = 6;
|
||||||
|
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text);
|
|
||||||
|
|
||||||
void render_loop(int speed) {
|
void render_loop(int speed) {
|
||||||
MovementState player_state;
|
MovementState player_state;
|
||||||
AnimationController animation;
|
AnimationController animation;
|
||||||
UartReader uart(serial_port);
|
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);
|
||||||
@@ -43,7 +41,7 @@ void render_loop(int speed) {
|
|||||||
int player_speed = 6;
|
int player_speed = 6;
|
||||||
bool game_over = false;
|
bool game_over = false;
|
||||||
|
|
||||||
player_state.set_state(PlayerState::Run);
|
player_state.set_state(PlayerState::Walk);
|
||||||
|
|
||||||
CrawlObstacleType type = CrawlObstacleType::Crawl1;
|
CrawlObstacleType type = CrawlObstacleType::Crawl1;
|
||||||
int start_x = VIEW_WIDTH + 10;
|
int start_x = VIEW_WIDTH + 10;
|
||||||
@@ -69,19 +67,14 @@ void render_loop(int speed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
need_redraw = false;
|
need_redraw = false;
|
||||||
UartEvent uart_event = uart.poll();
|
ButtonEvent button_event = button.poll();
|
||||||
|
|
||||||
if (uart_event == UartEvent::MessageUpdate) {
|
if (button_event == ButtonEvent::Pressed) {
|
||||||
need_redraw = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uart_event == UartEvent::Triggered) {
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +101,7 @@ void render_loop(int speed) {
|
|||||||
|
|
||||||
if (need_redraw) {
|
if (need_redraw) {
|
||||||
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
||||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user