another movement done
This commit is contained in:
@@ -6,85 +6,23 @@
|
||||
#include "mbed.h"
|
||||
#include "player.h"
|
||||
#include "../game/state.h"
|
||||
#include "../game/animation.h"
|
||||
#include "../hardware/uart.h"
|
||||
#include "../assets/character_frames.h"
|
||||
#include <cstring>
|
||||
|
||||
extern BufferedSerial serial_port;
|
||||
extern DigitalOut led;
|
||||
|
||||
// Constants
|
||||
constexpr size_t BUFFER_SIZE = 64;
|
||||
constexpr auto ANIMATION_TICK = 170ms;
|
||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
||||
constexpr int PLAYER_X = 9;
|
||||
constexpr int PLAYER_Y = 6;
|
||||
|
||||
static char rx_buffer[BUFFER_SIZE];
|
||||
static char message[BUFFER_SIZE];
|
||||
static bool message_active = false;
|
||||
|
||||
void draw_mask(const char *unused_filename, int shift, const char *text);
|
||||
|
||||
// Returns:
|
||||
// 0 = no change - walk1
|
||||
// 1 = normal redraw - walk2
|
||||
// 2 = button triggered - crawl1
|
||||
static int read_uart() {
|
||||
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) {
|
||||
led = !led; // toogle ledky
|
||||
strncpy(message, rx_buffer, sizeof(message) - 1);
|
||||
message_active = true;
|
||||
changed = true;
|
||||
triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// casovac na 1s zobrazenia spravy
|
||||
static Timer msg_timer;
|
||||
static bool timer_started = false;
|
||||
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 2; // LED toggled press
|
||||
if (changed)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
||||
if (anim_timer.elapsed_time() >= ANIMATION_TICK) {
|
||||
// speed determines scroll steps per tick
|
||||
shift += speed;
|
||||
anim_timer.reset();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void render_loop(int speed) {
|
||||
WalkingState player_state;
|
||||
|
||||
Timer anim_timer;
|
||||
anim_timer.start();
|
||||
int shift = 0;
|
||||
AnimationController animation;
|
||||
UartReader uart(serial_port, led);
|
||||
|
||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||
|
||||
@@ -93,13 +31,13 @@ void render_loop(int speed) {
|
||||
|
||||
while (true) {
|
||||
need_redraw = false;
|
||||
int uart_state = read_uart(); // returns 0/1/2
|
||||
UartEvent uart_event = uart.poll();
|
||||
|
||||
if (uart_state == 1) {
|
||||
if (uart_event == UartEvent::MessageUpdate) {
|
||||
need_redraw = true;
|
||||
}
|
||||
|
||||
if (uart_state == 2) {
|
||||
if (uart_event == UartEvent::Triggered) {
|
||||
// start crawl frame for x time
|
||||
player_state.start_crawl();
|
||||
need_redraw = true;
|
||||
@@ -108,12 +46,12 @@ void render_loop(int speed) {
|
||||
// Check if crawl duration expired
|
||||
player_state.update();
|
||||
|
||||
if (update_animation(anim_timer, shift, speed)) {
|
||||
if (animation.tick(speed)) {
|
||||
need_redraw = true;
|
||||
}
|
||||
|
||||
if (need_redraw) {
|
||||
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
||||
CharacterFrame player_frame = player_state.get_character_frame();
|
||||
|
||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
||||
|
||||
Reference in New Issue
Block a user