changes to either walk, run or crawl calculations
This commit is contained in:
@@ -1,8 +1,21 @@
|
|||||||
// 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"
|
||||||
|
#include "../assets/character_run_frames.h"
|
||||||
|
#include "../assets/character_walk_frames.h"
|
||||||
|
#include "../assets/character_crawl_frames.h"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
int compute_frame_index(int frame_count, float player_speed, int tick_counter) {
|
||||||
|
float phase_speed = player_speed * 0.1f;
|
||||||
|
float phase = fmodf(tick_counter * phase_speed, 1.0f);
|
||||||
|
int frame =
|
||||||
|
static_cast<int>((1.0f - fabsf(sinf(phase * M_PI))) * (frame_count - 1));
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
void WalkingState::update() {
|
void WalkingState::update() {
|
||||||
// stop crawling after duration
|
// stop crawling after duration
|
||||||
@@ -19,22 +32,44 @@ void WalkingState::start_crawl() {
|
|||||||
state_timer.start();
|
state_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO THIS NEEDS REDESIGN ACCORDING TO SPEED
|
// switch walk/run based on relative speed
|
||||||
void WalkingState::toggle_walk_frame(float player_speed, int tick_counter) {
|
void WalkingState::set_motion_state_for_speed(int player_speed,
|
||||||
constexpr int FRAME_COUNT = 8;
|
int ground_speed) {
|
||||||
float phase_speed = player_speed * 0.1f; // tune this scaling for animation tempo
|
int relative = player_speed - ground_speed;
|
||||||
|
if (relative > 1)
|
||||||
float phase = fmodf(tick_counter * phase_speed, 1.0f);
|
current_state = PlayerState::Run;
|
||||||
int frame = static_cast<int>((1.0f - fabsf(sinf(phase * M_PI))) * (FRAME_COUNT - 1));
|
else
|
||||||
|
current_state = PlayerState::Walk;
|
||||||
run_index = frame;
|
}
|
||||||
|
|
||||||
|
// TODO THIS NEEDS REDESIGN FOR WALK/RUN
|
||||||
|
void WalkingState::toggle_walk_frame(float player_speed, int tick_counter) {
|
||||||
|
switch (current_state) {
|
||||||
|
case PlayerState::Run:
|
||||||
|
run_index = compute_frame_index(CHARACTER_RUN_FRAME_COUNT, player_speed,
|
||||||
|
tick_counter);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlayerState::Walk:
|
||||||
|
walk_index = compute_frame_index(CHARACTER_WALK_FRAME_COUNT, player_speed,
|
||||||
|
tick_counter);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlayerState::Crawl:
|
||||||
|
crawl_index = compute_frame_index(CHARACTER_CRAWL_FRAME_COUNT, player_speed,
|
||||||
|
tick_counter);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PlayerState::Crawl2:
|
||||||
|
crawl_index = compute_frame_index(CHARACTER_CRAWL2_FRAME_COUNT, player_speed,
|
||||||
|
tick_counter);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// void RunningState::toggle_run_frame() {
|
|
||||||
// if (++run_index >= 7) run_index = 0;
|
|
||||||
// }
|
|
||||||
|
|
||||||
FrameSelection WalkingState::get_frame_selection() const {
|
FrameSelection WalkingState::get_frame_selection() const {
|
||||||
FrameSelection f{MovementType::Walk, 0};
|
FrameSelection f{MovementType::Walk, 0};
|
||||||
|
|
||||||
switch (current_state) {
|
switch (current_state) {
|
||||||
case PlayerState::Walk:
|
case PlayerState::Walk:
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ struct FrameSelection {
|
|||||||
|
|
||||||
class WalkingState {
|
class WalkingState {
|
||||||
private:
|
private:
|
||||||
PlayerState current_state = PlayerState::Run;
|
PlayerState current_state = PlayerState::Walk;
|
||||||
Timer state_timer;
|
Timer state_timer;
|
||||||
int walk_index = 0; // cycles 0‑6
|
int walk_index = 0; // cycles 0‑6
|
||||||
int run_index = 0; // cycles 0‑6
|
int run_index = 0; // cycles 0‑6
|
||||||
@@ -23,6 +23,9 @@ private:
|
|||||||
public:
|
public:
|
||||||
void update();
|
void update();
|
||||||
void start_crawl();
|
void start_crawl();
|
||||||
|
|
||||||
|
void set_motion_state_for_speed(int player_speed, int ground_speed);
|
||||||
|
|
||||||
void toggle_walk_frame(float player_speed, int tick_counter);
|
void toggle_walk_frame(float player_speed, int tick_counter);
|
||||||
|
|
||||||
FrameSelection get_frame_selection() const;
|
FrameSelection get_frame_selection() const;
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ void render_loop(int speed) {
|
|||||||
|
|
||||||
|
|
||||||
if (player_state.get_state() != PlayerState::Crawl) {
|
if (player_state.get_state() != PlayerState::Crawl) {
|
||||||
|
player_state.set_motion_state_for_speed(player_speed, timing.get_ground_speed());
|
||||||
player_state.toggle_walk_frame(player_speed, tick_counter);
|
player_state.toggle_walk_frame(player_speed, tick_counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user