Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8cb6892d30 | ||
|
|
de6facff03 | ||
|
|
2753aed573 | ||
|
|
0ff2be6564 |
@@ -1,6 +1,8 @@
|
||||
// src/game/state.cpp
|
||||
#include "state.h"
|
||||
#include <cmath>
|
||||
#include "../render/player.h"
|
||||
#include "../timing/speed_controller.h"
|
||||
|
||||
void WalkingState::update() {
|
||||
// stop crawling after duration
|
||||
@@ -18,9 +20,18 @@ void WalkingState::start_crawl() {
|
||||
}
|
||||
|
||||
// TODO THIS NEEDS REDESIGN ACCORDING TO SPEED
|
||||
void WalkingState::toggle_walk_frame() {
|
||||
if (++walk_index >= 7) walk_index = 0;
|
||||
void WalkingState::toggle_walk_frame(float player_speed, int tick_counter) {
|
||||
constexpr int FRAME_COUNT = 8;
|
||||
float phase_speed = player_speed * 0.1f; // tune this scaling for animation tempo
|
||||
|
||||
float phase = fmodf(tick_counter * phase_speed, 1.0f);
|
||||
int frame = static_cast<int>((1.0f - fabsf(sinf(phase * M_PI))) * (FRAME_COUNT - 1));
|
||||
|
||||
run_index = frame;
|
||||
}
|
||||
// void RunningState::toggle_run_frame() {
|
||||
// if (++run_index >= 7) run_index = 0;
|
||||
// }
|
||||
|
||||
FrameSelection WalkingState::get_frame_selection() const {
|
||||
FrameSelection f{MovementType::Walk, 0};
|
||||
|
||||
@@ -14,7 +14,7 @@ struct FrameSelection {
|
||||
|
||||
class WalkingState {
|
||||
private:
|
||||
PlayerState current_state = PlayerState::Walk;
|
||||
PlayerState current_state = PlayerState::Run;
|
||||
Timer state_timer;
|
||||
int walk_index = 0; // cycles 0‑6
|
||||
int run_index = 0; // cycles 0‑6
|
||||
@@ -23,7 +23,7 @@ private:
|
||||
public:
|
||||
void update();
|
||||
void start_crawl();
|
||||
void toggle_walk_frame();
|
||||
void toggle_walk_frame(float player_speed, int tick_counter);
|
||||
|
||||
FrameSelection get_frame_selection() const;
|
||||
PlayerState get_state() const { return current_state; }
|
||||
|
||||
@@ -22,5 +22,5 @@ int main(void) {
|
||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||
|
||||
// Just call into render feature
|
||||
render_loop(1);
|
||||
render_loop(6);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
// src/render/background.cpp
|
||||
|
||||
#include "background.h"
|
||||
#include "../assets/background_frame.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -9,12 +9,15 @@
|
||||
#include "../game/animation.h"
|
||||
#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);
|
||||
@@ -23,13 +26,24 @@ 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);
|
||||
|
||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||
|
||||
const char *bg_file = "background_dark_inverted.txt";
|
||||
bool need_redraw = false;
|
||||
|
||||
int tick_counter = 0;
|
||||
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();
|
||||
|
||||
@@ -57,8 +71,9 @@ void render_loop(int speed) {
|
||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||
draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index);
|
||||
|
||||
|
||||
if (player_state.get_state() != PlayerState::Crawl) {
|
||||
player_state.toggle_walk_frame();
|
||||
player_state.toggle_walk_frame(player_speed, tick_counter);
|
||||
}
|
||||
|
||||
ThisThread::sleep_for(50ms);
|
||||
|
||||
29
semestralka1/src/timing/movement_controller.h
Normal file
29
semestralka1/src/timing/movement_controller.h
Normal 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); }
|
||||
};
|
||||
34
semestralka1/src/timing/speed_controller.cpp
Normal file
34
semestralka1/src/timing/speed_controller.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
// src/timing/speed_controller.cpp
|
||||
#include "speed_controller.h"
|
||||
|
||||
void SpeedController::set_ground_speed(int spd) {
|
||||
if (spd < 0) spd = 0;
|
||||
if (spd > 30) spd = 30;
|
||||
ground_speed = spd;
|
||||
}
|
||||
|
||||
int SpeedController::frame_advance_for(int object_speed, int tick_counter) const {
|
||||
if (object_speed <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ground_speed == 0) {
|
||||
return 1 + object_speed;
|
||||
}
|
||||
|
||||
const int delta = object_speed - ground_speed;
|
||||
|
||||
if (delta > 0) {
|
||||
return 1 + delta;
|
||||
}
|
||||
|
||||
if (delta == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
float ratio_f = static_cast<float>(ground_speed) / object_speed;
|
||||
int ratio = static_cast<int>(ratio_f + 0.3f);
|
||||
if (ratio < 1) ratio = 1;
|
||||
|
||||
return (tick_counter % ratio == 0) ? 1 : 0;
|
||||
}
|
||||
19
semestralka1/src/timing/speed_controller.h
Normal file
19
semestralka1/src/timing/speed_controller.h
Normal file
@@ -0,0 +1,19 @@
|
||||
// src/timing/speed_controller.h
|
||||
#pragma once
|
||||
#include "mbed.h"
|
||||
|
||||
class SpeedController {
|
||||
private:
|
||||
int ground_speed = 1;
|
||||
|
||||
public:
|
||||
SpeedController() = default;
|
||||
|
||||
void set_ground_speed(int spd);
|
||||
int get_ground_speed() const { return ground_speed; }
|
||||
|
||||
// Calculates how many frames to advance for current tick.
|
||||
// Takes object’s current speed and a global tick counter
|
||||
// to automatically handle slower "wait" behavior.
|
||||
int frame_advance_for(int object_speed, int tick_counter) const;
|
||||
};
|
||||
Reference in New Issue
Block a user