From 714af1b173d56f20200da0f2fef86e0e824e8962 Mon Sep 17 00:00:00 2001 From: dandovlatov Date: Tue, 24 Nov 2026 03:00:20 +0400 Subject: [PATCH] FBC: NIGHT SHIFT - cross-faded agent animations and smoothed the locomotion blending --- src/render/models.cpp | 50 ++++++++++++++++++++++++++++++++++++---- src/render/models.hpp | 9 +++++++- src/render/presenter.cpp | 34 +++++++++++++++++++++++---- src/render/presenter.hpp | 4 ++++ 4 files changed, 87 insertions(+), 10 deletions(-) diff --git a/src/render/models.cpp b/src/render/models.cpp index 4d6ff11..a81f65c 100644 --- a/src/render/models.cpp +++ b/src/render/models.cpp @@ -8,6 +8,7 @@ #define DrawCylinder(...) do { PROF_DRAW_CALL(); DrawCylinder(__VA_ARGS__); } while(0) #define DrawSphere(...) do { PROF_DRAW_CALL(); DrawSphere(__VA_ARGS__); } while(0) +#include #include #include #include @@ -144,6 +145,11 @@ void ModelBank::shutdown() { e.animations = nullptr; e.animation_count = 0; e.current_animation = -1; + e.animation_clock = 0.0f; + e.blend_from = -1; + e.blend_from_clock = 0.0f; + e.blend_time = 0.0f; + e.blend_duration = 0.0f; if (e.loaded) UnloadModel(e.model); e.loaded = false; } @@ -220,7 +226,8 @@ void ModelBank::draw(ActorModel id, Vector3 pos, float yaw, float scale, Vector3{scale, scale, scale}, tint); } -void ModelBank::update_animation(ActorModel id, const char* name, float dt) { +void ModelBank::update_animation(ActorModel id, const char* name, float dt, + float blend_seconds) { const int index = static_cast(id); if (index < 0 || index >= static_cast(ActorModel::Count)) return; Entry& e = entries_[index]; @@ -231,14 +238,49 @@ void ModelBank::update_animation(ActorModel id, const char* name, float dt) { if (std::strcmp(e.animations[a].name, name) == 0) { found = a; break; } } if (found < 0) return; + if (found != e.current_animation) { + if (e.blend_duration > 0.0f) { + // Retargeted mid-fade: carry the dominant pose forward instead of + // snapping to frame zero of the new clip. + if (e.blend_time / e.blend_duration >= 0.5f) { + e.blend_from = e.current_animation; + e.blend_from_clock = e.animation_clock; + } + } else { + e.blend_from = e.current_animation; + e.blend_from_clock = e.animation_clock; + } e.current_animation = found; e.animation_clock = 0.0f; + e.blend_time = 0.0f; + e.blend_duration = (e.blend_from >= 0 && blend_seconds > 0.0f) + ? blend_seconds : 0.0f; } e.animation_clock += dt; - const float frame = std::fmod(e.animation_clock * 60.0f, - static_cast(e.animations[found].keyframeCount)); - UpdateModelAnimation(e.model, e.animations[found], frame); + + const ModelAnimation& to = e.animations[found]; + const float to_frame = std::fmod(e.animation_clock * 60.0f, + static_cast(to.keyframeCount)); + + if (e.blend_duration <= 0.0f) { + UpdateModelAnimation(e.model, to, to_frame); + return; + } + + e.blend_time += dt; + const float w = std::min(1.0f, e.blend_time / e.blend_duration); + const float eased = w * w * (3.0f - 2.0f * w); + e.blend_from_clock += dt; + const ModelAnimation& from = e.animations[e.blend_from]; + const float from_frame = std::fmod(e.blend_from_clock * 60.0f, + static_cast(from.keyframeCount)); + UpdateModelAnimationEx(e.model, from, from_frame, to, to_frame, eased); + + if (w >= 1.0f) { + e.blend_from = -1; + e.blend_duration = 0.0f; + } } float ModelBank::height(ActorModel id) { diff --git a/src/render/models.hpp b/src/render/models.hpp index 4724223..71ef095 100644 --- a/src/render/models.hpp +++ b/src/render/models.hpp @@ -30,7 +30,10 @@ public: float stagger = 0.0f, float phase = 0.0f, float hiss = 0.0f); // Plays a named GLB action on a skinned model (dt in seconds, 60 fps clips). - void update_animation(ActorModel id, const char* name, float dt); + // blend_seconds > 0 cross-fades from the outgoing clip into the new one via + // raylib's two-animation blend; 0 keeps the old hard cut. + void update_animation(ActorModel id, const char* name, float dt, + float blend_seconds = 0.0f); // Per-model visual height, used for health bars / floating labels. static float height(ActorModel id); @@ -42,6 +45,10 @@ private: int animation_count = 0; int current_animation = -1; float animation_clock = 0.0f; + int blend_from = -1; // outgoing clip during a cross-fade + float blend_from_clock = 0.0f; + float blend_time = 0.0f; + float blend_duration = 0.0f; // 0 => no cross-fade in progress bool loaded = false; }; diff --git a/src/render/presenter.cpp b/src/render/presenter.cpp index beef3f8..7b270b4 100644 --- a/src/render/presenter.cpp +++ b/src/render/presenter.cpp @@ -491,6 +491,10 @@ void Presenter::reset() { debris_.clear(); concrete_debris_.clear(); scorch_.clear(); + player_walk_ = 0.0f; + dash_pose_ = 0.0f; + dash_prev_ = 0.0f; + player_running_ = false; } void Presenter::shutdown() { @@ -1248,17 +1252,37 @@ void Presenter::draw_scene(Game& game) { const Vec2 moved = p - last_player_pos_; if (length_sq(moved) > 1e-5f) player_yaw_ = std::atan2(moved.x, moved.y); last_player_pos_ = p; - const float player_walk = std::min(1.0f, length(moved) * 14.0f); + + // Speed from distance / time rather than raw per-frame delta: stable + // when the fixed-step sim runs zero or several ticks in one render. + const float raw_speed = frame_dt > 1e-5f ? length(moved) / frame_dt : 0.0f; + const float ref_speed = std::max(0.01f, game.player.stats.move_speed); + const float target_walk = std::clamp(raw_speed / ref_speed, 0.0f, 1.0f); + const float walk_k = target_walk > player_walk_ ? 12.0f : 7.0f; + player_walk_ += (target_walk - player_walk_) * + (1.0f - std::exp(-walk_k * frame_dt)); + + // Hysteresis keeps a player scraping a wall from flapping Idle <-> Run + // every frame, which was the main source of the visible stutter. + if (!player_running_ && player_walk_ > 0.28f) player_running_ = true; + else if (player_running_ && player_walk_ < 0.12f) player_running_ = false; + + // The dash burst is only 0.16 s; hold its lean a little longer so the + // pose reads, then cross-fade back into locomotion. + if (game.player.dash_timer > 0.0f && dash_prev_ <= 0.0f) dash_pose_ = 0.42f; + dash_prev_ = game.player.dash_timer; + dash_pose_ = std::max(0.0f, dash_pose_ - frame_dt); + const float player_flash = actor_flash(game.player.hit_flash, 4.0f); // The Blender agent carries its own root bob; the old sine bob would // double up on top of the skeletal Run/Idle clips. const char* agent_clip = "Idle"; - if (game.player.dash_timer > 0.0f) agent_clip = "Dash"; + if (dash_pose_ > 0.0f) agent_clip = "Dash"; else if (player_flash > 0.05f) agent_clip = "Stagger"; - else if (player_walk > 0.08f) agent_clip = "Run"; - models_.update_animation(ActorModel::Guard, agent_clip, GetFrameTime()); + else if (player_running_) agent_clip = "Run"; + models_.update_animation(ActorModel::Guard, agent_clip, frame_dt, 0.16f); models_.draw(ActorModel::Guard, {p.x, 0.0f, p.y}, player_yaw_, 1.0f, - WHITE, player_flash, true, player_walk, player_flash, + WHITE, player_flash, true, player_walk_, player_flash, anim_time_); // Enemies face the player; Hiss units add a nervous jitter. diff --git a/src/render/presenter.hpp b/src/render/presenter.hpp index 070c6be..870379c 100644 --- a/src/render/presenter.hpp +++ b/src/render/presenter.hpp @@ -82,6 +82,10 @@ private: float damage_flash_ = 0.0f; float anim_time_ = 0.0f; float player_yaw_ = 0.0f; + float player_walk_ = 0.0f; // smoothed locomotion weight for the agent clips + float dash_pose_ = 0.0f; // presentation-only hold for the Dash pose + float dash_prev_ = 0.0f; // previous dash timer, for rising-edge detection + bool player_running_ = false; // hysteresis latch for Idle <-> Run bool hiss_test_ = false; bool floor_mode_ = false; bool floor_overview_ = false;