refactoring

This commit is contained in:
Priec
2025-11-14 15:15:20 +01:00
parent 38b8bb0657
commit 9c93179bf0
3 changed files with 61 additions and 553 deletions

View File

@@ -12,6 +12,14 @@ extern DigitalOut led;
#define BUFFER_SIZE 64
enum class PlayerState { Walk1, Walk2, Crawl1 };
struct WalkingState {
PlayerState current_state = PlayerState::Walk1;
Timer state_timer;
bool state_timer_active = false;
};
static char rx_buffer[BUFFER_SIZE];
static char message[BUFFER_SIZE];
static bool message_active = false;
@@ -72,58 +80,79 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
return false;
}
static int get_player_frame(PlayerState state) {
switch (state) {
case PlayerState::Walk1:
return 0;
case PlayerState::Walk2:
return 1;
case PlayerState::Crawl1:
return 2;
default:
return 0;
}
}
static void update_player_state(WalkingState &state) {
const auto CRAWL_DURATION = 300ms;
// stop crawling
if (state.current_state == PlayerState::Crawl1 && state.state_timer_active &&
state.state_timer.elapsed_time() >= CRAWL_DURATION) {
state.current_state = PlayerState::Walk1;
state.state_timer.stop();
state.state_timer_active = false;
}
}
static void toggle_walk_frame(WalkingState &state) {
if (state.current_state == PlayerState::Walk1) {
state.current_state = PlayerState::Walk2;
} else if (state.current_state == PlayerState::Walk2) {
state.current_state = PlayerState::Walk1;
}
}
void render_loop(int speed) {
Timer msg_timer;
WalkingState player_state;
Timer anim_timer;
Timer crawl_timer;
msg_timer.start();
anim_timer.start();
int shift = 0;
const char *bg_file = "background_dark_inverted.txt";
bool need_redraw = false;
bool crawling = false; // are we in 1s crawl state?
bool crawl_timer_started = false;
const auto CRAWL_DURATION = 300ms;
while (true) {
need_redraw = false;
int uart_state = read_uart(); // returns 0/1/2
if (uart_state == 1)
need_redraw = true;
if (uart_state == 2) {
// start crawl frame for 1 second
crawling = true;
crawl_timer.reset();
crawl_timer.start();
crawl_timer_started = true;
// start crawl frame for x time
player_state.current_state = PlayerState::Crawl1;
player_state.state_timer.reset();
player_state.state_timer.start();
player_state.state_timer_active = true;
need_redraw = true;
}
// stop crawling after 1 second
if (crawling && crawl_timer_started &&
crawl_timer.elapsed_time() >= CRAWL_DURATION) {
crawling = false;
crawl_timer.stop();
crawl_timer_started = false;
}
// Check if crawl duration expired
update_player_state(player_state);
if (update_animation(anim_timer, shift, speed))
need_redraw = true;
if (need_redraw || crawling) {
if (need_redraw) {
draw_mask(bg_file, shift, message_active ? message : nullptr);
int player_frame = get_player_frame(player_state.current_state);
draw_player(18, 13, player_frame);
static int frame = 0;
if (crawling) {
// Display crawl frame during 1s crawl state
draw_player(18, 13, 2);
} else {
draw_player(18, 13, frame);
frame = (frame + 1) % 2; // alternate between frame 0 and 1
// alternate between frame 0 and 1 when not crawling
if (player_state.current_state != PlayerState::Crawl1) {
toggle_walk_frame(player_state);
}
ThisThread::sleep_for(50ms);