From 8cb6892d304e4978097f70f2c907347ab05c57c0 Mon Sep 17 00:00:00 2001 From: Filipriec Date: Sat, 15 Nov 2025 22:40:23 +0100 Subject: [PATCH] now the movement of the player is working properly well and being calculated based on the speed of the player --- semestralka1/src/render/background.cpp | 8 ++--- semestralka1/src/render/background.h | 1 + semestralka1/src/render/loop.cpp | 11 +++++-- semestralka1/src/timing/movement_controller.h | 29 +++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 semestralka1/src/timing/movement_controller.h diff --git a/semestralka1/src/render/background.cpp b/semestralka1/src/render/background.cpp index c73c55c..3462def 100644 --- a/semestralka1/src/render/background.cpp +++ b/semestralka1/src/render/background.cpp @@ -1,17 +1,15 @@ // src/render/background.cpp +#include "background.h" #include "../assets/background_frame.h" #include #include void draw_mask(const char *unused_filename, int shift, const char *text) { - const int view_width = 90; - const int view_height = 16; - // Terminal clear + home printf("\033[2J\033[H"); - for (int i = 0; i < view_height && i < VIEW_HEIGHT; i++) { + for (int i = 0; i < VIEW_HEIGHT && i < VIEW_HEIGHT; i++) { const char *row = BACKGROUND_MASK[i]; int width = strlen(row); if (width == 0) { @@ -21,7 +19,7 @@ void draw_mask(const char *unused_filename, int shift, const char *text) { int start = shift % width; - for (int j = 0; j < view_width; j++) { + for (int j = 0; j < VIEW_WIDTH; j++) { printf("%c", row[(start + j) % width]); } printf("\r\n"); diff --git a/semestralka1/src/render/background.h b/semestralka1/src/render/background.h index 097655b..d3b642b 100644 --- a/semestralka1/src/render/background.h +++ b/semestralka1/src/render/background.h @@ -1,6 +1,7 @@ // src/render/background.h #pragma once +constexpr int VIEW_WIDTH = 90; // Draws the ASCII art background with optional message void draw_mask(const char *unused_filename, int shift, const char *text = nullptr); diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index 84b3bd3..0ff2ad8 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -10,12 +10,14 @@ #include "../hardware/uart.h" #include "../render/player.h" #include "../timing/speed_controller.h" +#include "../timing/movement_controller.h" extern BufferedSerial serial_port; extern DigitalOut led; // Constants -constexpr int PLAYER_X = 9; +// constexpr int PLAYER_X = 9; +constexpr int PLAYER_X = 29; constexpr int PLAYER_Y = 6; void draw_mask(const char *unused_filename, int shift, const char *text); @@ -24,6 +26,7 @@ void render_loop(int speed) { WalkingState player_state; AnimationController animation; UartReader uart(serial_port); + MovementController mover(PLAYER_X, VIEW_WIDTH); SpeedController timing; timing.set_ground_speed(speed); @@ -33,10 +36,14 @@ void render_loop(int speed) { bool need_redraw = false; int tick_counter = 0; - int player_speed = 4; + int player_speed = 5; while (true) { tick_counter++; + + mover.update_position(player_speed, timing.get_ground_speed()); + pos.x = mover.get_position(); + need_redraw = false; UartEvent uart_event = uart.poll(); diff --git a/semestralka1/src/timing/movement_controller.h b/semestralka1/src/timing/movement_controller.h new file mode 100644 index 0000000..d8c5cfd --- /dev/null +++ b/semestralka1/src/timing/movement_controller.h @@ -0,0 +1,29 @@ +// src/timing/movement_controller.h + +#pragma once +#include "mbed.h" + +// Handles position of the player relative to ground movement +class MovementController { +private: + float x_pos; + const int view_width; + + // Scale factor for translating speed difference to pixel movement + const float motion_scale = 0.2f; + +public: + MovementController(int start_x, int width) : x_pos(static_cast(start_x)), view_width(width) {} + + void reset(int start_x) { x_pos = static_cast(start_x); } + + void update_position(int player_speed, int ground_speed) { + int delta = player_speed - ground_speed; + x_pos += static_cast(delta) * motion_scale; + + if (x_pos < 0) x_pos = 0; + if (x_pos > (VIEW_WIDTH - 10)) x_pos = static_cast(VIEW_WIDTH - 10); + } + + int get_position() const { return static_cast(x_pos); } +};