sinus speed is displaying frames of the animation now

This commit is contained in:
Filipriec
2025-11-15 22:07:15 +01:00
parent 2753aed573
commit de6facff03
4 changed files with 13 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
// src/game/state.cpp // src/game/state.cpp
#include "state.h" #include "state.h"
#include <cmath>
#include "../render/player.h" #include "../render/player.h"
#include "../timing/speed_controller.h" #include "../timing/speed_controller.h"
@@ -19,8 +20,14 @@ void WalkingState::start_crawl() {
} }
// TODO THIS NEEDS REDESIGN ACCORDING TO SPEED // TODO THIS NEEDS REDESIGN ACCORDING TO SPEED
void WalkingState::toggle_walk_frame(int step) { void WalkingState::toggle_walk_frame(float player_speed, int tick_counter) {
run_index = (run_index + step) % 7; 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() { // void RunningState::toggle_run_frame() {
// if (++run_index >= 7) run_index = 0; // if (++run_index >= 7) run_index = 0;

View File

@@ -23,7 +23,7 @@ private:
public: public:
void update(); void update();
void start_crawl(); void start_crawl();
void toggle_walk_frame(int step); void toggle_walk_frame(float player_speed, int tick_counter);
FrameSelection get_frame_selection() const; FrameSelection get_frame_selection() const;
PlayerState get_state() const { return current_state; } PlayerState get_state() const { return current_state; }

View File

@@ -22,5 +22,5 @@ int main(void) {
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE); printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
// Just call into render feature // Just call into render feature
render_loop(1); render_loop(6);
} }

View File

@@ -33,7 +33,7 @@ void render_loop(int speed) {
bool need_redraw = false; bool need_redraw = false;
int tick_counter = 0; int tick_counter = 0;
int player_speed = 3; int player_speed = 4;
while (true) { while (true) {
tick_counter++; tick_counter++;
@@ -66,9 +66,7 @@ void render_loop(int speed) {
if (player_state.get_state() != PlayerState::Crawl) { if (player_state.get_state() != PlayerState::Crawl) {
int step = timing.frame_advance_for(player_speed, tick_counter); player_state.toggle_walk_frame(player_speed, tick_counter);
if (step > 0)
player_state.toggle_walk_frame(step);
} }
ThisThread::sleep_for(50ms); ThisThread::sleep_for(50ms);