From 4e40a1825ac43a2ca1a493cf6c1c15c048973c82 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Tue, 17 Nov 2026 19:12:53 +0600 Subject: [PATCH] Speed up weapon draw --- server/tests/LocationGameStateTests.cpp | 77 +++++++++++++++++++++++++ src/CharacterState.cpp | 10 +++- src/CharacterState.h | 3 + 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/server/tests/LocationGameStateTests.cpp b/server/tests/LocationGameStateTests.cpp index 3fef3f9..14b6931 100644 --- a/server/tests/LocationGameStateTests.cpp +++ b/server/tests/LocationGameStateTests.cpp @@ -1,6 +1,7 @@ #include "LocationGameState.h" #include "game_server/InternalGameServer.h" #include +#include #include #include #include @@ -338,6 +339,82 @@ int main() return fail("Enabled player acquired an autonomous attack target"); } + FRG::CharacterState drawAnimation; + drawAnimation.configureAsPlayer(); + drawAnimation.enabled = true; + drawAnimation.setAnimationMetadata( + FRG::AnimationState::STAND_TO_ACTION, 0, 100); + drawAnimation.setAnimationMetadata( + FRG::AnimationState::ACTION_IDLE, 0, 100); + FRG::CharacterObservation attackTarget{ + point(0.f, 0.f, 1.f), 100.f}; + drawAnimation.update(100, {{"player1", &attackTarget}, nullptr}); + if (drawAnimation.currentState != FRG::AnimationState::STAND_TO_ACTION || + std::abs(drawAnimation.animationFrame - 8.333333f) > 0.001f || + drawAnimation.showWeapon) { + return fail("Weapon draw animation did not advance at 2x speed"); + } + drawAnimation.update(200, {{"player1", &attackTarget}, nullptr}); + if (!drawAnimation.showWeapon) { + return fail("Weapon draw animation did not use its frame visibility threshold"); + } + + FRG::CharacterState sheathAnimation; + sheathAnimation.configureAsPlayer(); + sheathAnimation.enabled = true; + sheathAnimation.currentState = FRG::AnimationState::ACTION_IDLE; + sheathAnimation.setAnimationMetadata( + FRG::AnimationState::ACTION_TO_STAND, 0, 100); + sheathAnimation.update(100, {{"player1", nullptr}, nullptr}); + if (sheathAnimation.currentState != FRG::AnimationState::ACTION_TO_STAND || + std::abs(sheathAnimation.animationFrame - 8.333333f) > 0.001f || + !sheathAnimation.showWeapon) { + return fail("Weapon sheath animation did not advance at 2x speed"); + } + sheathAnimation.update(300, {{"player1", nullptr}, nullptr}); + if (sheathAnimation.showWeapon) { + return fail("Weapon sheath animation did not hide at its frame visibility threshold"); + } + + FRG::CharacterState ordinaryAnimation; + ordinaryAnimation.configureAsPlayer(); + ordinaryAnimation.enabled = true; + ordinaryAnimation.currentState = FRG::AnimationState::STAND; + ordinaryAnimation.setAnimationMetadata( + FRG::AnimationState::STAND, 0, 100); + ordinaryAnimation.update(100, {{"player1", nullptr}, nullptr}); + if (std::abs(ordinaryAnimation.animationFrame - 4.1666665f) > 0.001f) { + return fail("Ordinary animation playback rate changed unexpectedly"); + } + + FRG::CharacterState completedDrawAnimation; + completedDrawAnimation.configureAsPlayer(); + completedDrawAnimation.enabled = true; + completedDrawAnimation.setAnimationMetadata( + FRG::AnimationState::STAND_TO_ACTION, 0, 30); + completedDrawAnimation.setAnimationMetadata( + FRG::AnimationState::ACTION_IDLE, 0, 30); + for (int i = 0; i < 4; ++i) + completedDrawAnimation.update( + 100, {{"player1", &attackTarget}, nullptr}); + if (completedDrawAnimation.currentState != + FRG::AnimationState::ACTION_IDLE) { + return fail("Accelerated weapon draw animation did not complete"); + } + + FRG::CharacterState completedSheathAnimation; + completedSheathAnimation.configureAsPlayer(); + completedSheathAnimation.enabled = true; + completedSheathAnimation.currentState = FRG::AnimationState::ACTION_IDLE; + completedSheathAnimation.setAnimationMetadata( + FRG::AnimationState::ACTION_TO_STAND, 0, 30); + for (int i = 0; i < 4; ++i) + completedSheathAnimation.update( + 100, {{"player1", nullptr}, nullptr}); + if (completedSheathAnimation.currentState != FRG::AnimationState::STAND) { + return fail("Accelerated weapon sheath animation did not complete"); + } + FRG::CharacterState guardedMover; nlohmann::json guardedJson; guardedMover.velocity = { diff --git a/src/CharacterState.cpp b/src/CharacterState.cpp index b99ac44..92fb900 100644 --- a/src/CharacterState.cpp +++ b/src/CharacterState.cpp @@ -40,6 +40,9 @@ void CharacterState::configureAsPlayer() { Eigen::AngleAxisf(-PI * 0.5f, Eigen::Vector3f::UnitX())); canAttack = true; isPlayer = true; + animationPlaybackRates.fill(1.0f); + animationPlaybackRates[static_cast(AnimationState::STAND_TO_ACTION)] = 2.0f; + animationPlaybackRates[static_cast(AnimationState::ACTION_TO_STAND)] = 2.0f; } void CharacterState::configureNavigationRetryCooldowns( @@ -452,7 +455,11 @@ CharacterUpdateEvents CharacterState::update( ? static_cast(animationStartFrames[index]) : 0.f; } const int previousFrame = static_cast(animationFrame); - animationFrame += static_cast(deltaMs) / 24.f; + const auto animationIndex = static_cast(currentState); + const float playbackRate = + animationIndex < animationPlaybackRates.size() + ? animationPlaybackRates[animationIndex] : 1.0f; + animationFrame += static_cast(deltaMs) / 24.f * playbackRate; if (previousFrame < 19 && static_cast(animationFrame) >= 19 && (currentState == AnimationState::ACTION_ATTACK || @@ -482,7 +489,6 @@ CharacterUpdateEvents CharacterState::update( currentState == AnimationState::ACTION_ATTACK || currentState == AnimationState::ACTION_IDLE; - const auto animationIndex = static_cast(currentState); const int frameCount = animationIndex < animationFrameCounts.size() ? animationFrameCounts[animationIndex] : 1; if (frameCount > 1 && static_cast(animationFrame) >= frameCount - 1) { diff --git a/src/CharacterState.h b/src/CharacterState.h index 97ca4c6..14ac04a 100644 --- a/src/CharacterState.h +++ b/src/CharacterState.h @@ -107,6 +107,9 @@ public: float animationFrame = 0.0f; std::array animationStartFrames = {{0, 0, 0, 0, 0, 0, 0, 0, 0}}; std::array animationFrameCounts = {{1, 1, 1, 1, 1, 1, 1, 1, 1}}; + std::array animationPlaybackRates = {{ + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f}}; // --- Movement / visual parameters --- fp walkSpeed = static_cast(3.0f);