pitch perfect movement
This commit is contained in:
0
semestralka1/src/game/animation.cpp
Normal file
0
semestralka1/src/game/animation.cpp
Normal file
0
semestralka1/src/game/animation.h
Normal file
0
semestralka1/src/game/animation.h
Normal file
@@ -13,6 +13,7 @@ private:
|
|||||||
Timer state_timer;
|
Timer state_timer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
// MOVED FROM loop.cpp
|
||||||
// Update state (handles crawl timeout)
|
// Update state (handles crawl timeout)
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "background.h"
|
#include "background.h"
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "../game/state.h"
|
||||||
#include "../assets/character_frames.h"
|
#include "../assets/character_frames.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
@@ -13,20 +14,11 @@ extern DigitalOut led;
|
|||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
constexpr size_t BUFFER_SIZE = 64;
|
constexpr size_t BUFFER_SIZE = 64;
|
||||||
constexpr auto CRAWL_DURATION = 300ms;
|
|
||||||
constexpr auto ANIMATION_TICK = 170ms;
|
constexpr auto ANIMATION_TICK = 170ms;
|
||||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
||||||
constexpr int PLAYER_X = 9;
|
constexpr int PLAYER_X = 9;
|
||||||
constexpr int PLAYER_Y = 6;
|
constexpr int PLAYER_Y = 6;
|
||||||
|
|
||||||
// PlayerState is what user pressed. We are mapping CharacterState to it
|
|
||||||
enum class PlayerState { Walk1, Walk2, Crawl1 };
|
|
||||||
|
|
||||||
struct WalkingState {
|
|
||||||
PlayerState current_state = PlayerState::Walk1;
|
|
||||||
Timer state_timer;
|
|
||||||
};
|
|
||||||
|
|
||||||
static char rx_buffer[BUFFER_SIZE];
|
static char rx_buffer[BUFFER_SIZE];
|
||||||
static char message[BUFFER_SIZE];
|
static char message[BUFFER_SIZE];
|
||||||
static bool message_active = false;
|
static bool message_active = false;
|
||||||
@@ -87,42 +79,6 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static CharacterFrame get_character_frame(PlayerState state) {
|
|
||||||
switch (state) {
|
|
||||||
case PlayerState::Walk1:
|
|
||||||
return CharacterFrame::Walk1;
|
|
||||||
case PlayerState::Walk2:
|
|
||||||
return CharacterFrame::Walk2;
|
|
||||||
case PlayerState::Crawl1:
|
|
||||||
return CharacterFrame::Crawl1;
|
|
||||||
default:
|
|
||||||
return CharacterFrame::Walk1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void update_player_state(WalkingState &state) {
|
|
||||||
// stop crawling
|
|
||||||
if (state.current_state == PlayerState::Crawl1 &&
|
|
||||||
state.state_timer.elapsed_time() >= CRAWL_DURATION) {
|
|
||||||
state.current_state = PlayerState::Walk1;
|
|
||||||
state.state_timer.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void start_crawl(WalkingState &state) {
|
|
||||||
state.current_state = PlayerState::Crawl1;
|
|
||||||
state.state_timer.reset();
|
|
||||||
state.state_timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void render_loop(int speed) {
|
void render_loop(int speed) {
|
||||||
WalkingState player_state;
|
WalkingState player_state;
|
||||||
|
|
||||||
@@ -145,12 +101,12 @@ void render_loop(int speed) {
|
|||||||
|
|
||||||
if (uart_state == 2) {
|
if (uart_state == 2) {
|
||||||
// start crawl frame for x time
|
// start crawl frame for x time
|
||||||
start_crawl(player_state);
|
player_state.start_crawl();
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if crawl duration expired
|
// Check if crawl duration expired
|
||||||
update_player_state(player_state);
|
player_state.update();
|
||||||
|
|
||||||
if (update_animation(anim_timer, shift, speed)) {
|
if (update_animation(anim_timer, shift, speed)) {
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
@@ -158,13 +114,14 @@ void render_loop(int speed) {
|
|||||||
|
|
||||||
if (need_redraw) {
|
if (need_redraw) {
|
||||||
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
||||||
CharacterFrame player_frame = get_character_frame(player_state.current_state);
|
CharacterFrame player_frame = player_state.get_character_frame();
|
||||||
|
|
||||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
||||||
draw_character(draw_pos.x, draw_pos.y, player_frame);
|
draw_character(draw_pos.x, draw_pos.y, player_frame);
|
||||||
|
|
||||||
// alternate between frame 0 and 1 when not crawling
|
// alternate between frame 0 and 1 when not crawling
|
||||||
if (player_state.current_state != PlayerState::Crawl1) {
|
if (player_state.get_state() != PlayerState::Crawl1) {
|
||||||
toggle_walk_frame(player_state);
|
player_state.toggle_walk_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
ThisThread::sleep_for(50ms);
|
ThisThread::sleep_for(50ms);
|
||||||
|
|||||||
Reference in New Issue
Block a user