now the movement of the player is working properly well and being calculated based on the speed of the player

This commit is contained in:
Filipriec
2025-11-15 22:40:23 +01:00
parent de6facff03
commit 8cb6892d30
4 changed files with 42 additions and 7 deletions

View File

@@ -1,17 +1,15 @@
// src/render/background.cpp // src/render/background.cpp
#include "background.h"
#include "../assets/background_frame.h" #include "../assets/background_frame.h"
#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, const char *text) {
const int view_width = 90;
const int view_height = 16;
// Terminal clear + home // Terminal clear + home
printf("\033[2J\033[H"); 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]; const char *row = BACKGROUND_MASK[i];
int width = strlen(row); int width = strlen(row);
if (width == 0) { if (width == 0) {
@@ -21,7 +19,7 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
int start = shift % width; 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("%c", row[(start + j) % width]);
} }
printf("\r\n"); printf("\r\n");

View File

@@ -1,6 +1,7 @@
// src/render/background.h // src/render/background.h
#pragma once #pragma once
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, const char *text = nullptr);

View File

@@ -10,12 +10,14 @@
#include "../hardware/uart.h" #include "../hardware/uart.h"
#include "../render/player.h" #include "../render/player.h"
#include "../timing/speed_controller.h" #include "../timing/speed_controller.h"
#include "../timing/movement_controller.h"
extern BufferedSerial serial_port; extern BufferedSerial serial_port;
extern DigitalOut led; extern DigitalOut led;
// Constants // Constants
constexpr int PLAYER_X = 9; // constexpr int PLAYER_X = 9;
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 draw_mask(const char *unused_filename, int shift, const char *text);
@@ -24,6 +26,7 @@ void render_loop(int speed) {
WalkingState player_state; WalkingState player_state;
AnimationController animation; AnimationController animation;
UartReader uart(serial_port); UartReader uart(serial_port);
MovementController mover(PLAYER_X, VIEW_WIDTH);
SpeedController timing; SpeedController timing;
timing.set_ground_speed(speed); timing.set_ground_speed(speed);
@@ -33,10 +36,14 @@ void render_loop(int speed) {
bool need_redraw = false; bool need_redraw = false;
int tick_counter = 0; int tick_counter = 0;
int player_speed = 4; int player_speed = 5;
while (true) { while (true) {
tick_counter++; tick_counter++;
mover.update_position(player_speed, timing.get_ground_speed());
pos.x = mover.get_position();
need_redraw = false; need_redraw = false;
UartEvent uart_event = uart.poll(); UartEvent uart_event = uart.poll();

View File

@@ -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<float>(start_x)), view_width(width) {}
void reset(int start_x) { x_pos = static_cast<float>(start_x); }
void update_position(int player_speed, int ground_speed) {
int delta = player_speed - ground_speed;
x_pos += static_cast<float>(delta) * motion_scale;
if (x_pos < 0) x_pos = 0;
if (x_pos > (VIEW_WIDTH - 10)) x_pos = static_cast<float>(VIEW_WIDTH - 10);
}
int get_position() const { return static_cast<int>(x_pos); }
};