From f89f3c85231225f23a5a3a8bbed73b4d85ce38aa Mon Sep 17 00:00:00 2001 From: ugozapad Date: Wed, 11 Mar 2026 23:25:46 +0300 Subject: [PATCH] set anim speed --- data/scripts/weapons/weapon_base.lua | 4 ++++ data/scripts/weapons/weapon_ump.lua | 2 +- src/game/game_object.cpp | 12 ++++++++++++ src/game/game_object.h | 1 + src/render/animation.cpp | 10 +++++++++- src/render/animation.h | 3 +++ src/render/model.cpp | 2 +- 7 files changed, 31 insertions(+), 3 deletions(-) diff --git a/data/scripts/weapons/weapon_base.lua b/data/scripts/weapons/weapon_base.lua index 5f06f88..5dcbc2a 100644 --- a/data/scripts/weapons/weapon_base.lua +++ b/data/scripts/weapons/weapon_base.lua @@ -115,6 +115,10 @@ function weapon_base:on_state_switch(state) if fsm_state.anim_sound then engine.play_sound(fsm_state.anim_sound) end + + if fsm_state.anim_speed then + self:set_animation_speed(fsm_state.anim_speed) + end end function weapon_base:set_relative_position_to_camera( ent ) diff --git a/data/scripts/weapons/weapon_ump.lua b/data/scripts/weapons/weapon_ump.lua index 1334c09..c952299 100644 --- a/data/scripts/weapons/weapon_ump.lua +++ b/data/scripts/weapons/weapon_ump.lua @@ -14,7 +14,7 @@ weapon_ump.m_fsm[WEAPON_FSM_STATE_IDLE] = { weapon_ump.m_fsm[WEAPON_FSM_STATE_ATTACK] = { anim = "shoot1", -- имя анимации anim_playback = ANIM_PLAYBACK_NONE, - anim_speed = 1.0, -- обычная скорость анимации + anim_speed = 6.0, -- обычная скорость анимации anim_sound = "data/sounds/weapons/ump45_shoot.wav" } diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index af60999..26d1a03 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -235,6 +235,7 @@ void Entity::RegisterBaseFunctions() m_luaObject.Register("get_current_animation", *this, &Entity::Lua_GetCurrentAnimation); m_luaObject.Register("get_current_animation_time", *this, &Entity::Lua_GetCurrentAnimationTime); m_luaObject.Register("get_animation_time", *this, &Entity::Lua_GetAnimationTime); + m_luaObject.Register("set_animation_speed", *this, &Entity::Lua_SetAnimationSpeed); //int Lua_FindAnimation(LuaPlus::LuaState * state); //int Lua_PlayAnimation(LuaPlus::LuaState * state); @@ -615,6 +616,17 @@ int Entity::Lua_GetAnimationTime(LuaPlus::LuaState* state) return 1; } +int Entity::Lua_SetAnimationSpeed(LuaPlus::LuaState* state) +{ + LuaPlus::LuaStack stack(state); + float speed = stack[2].GetFloat(); + + if (m_skeleton) + m_skeleton->SetAnimationSpeed(speed); + + return 0; +} + const LuaPlus::LuaObject& Entity::GetLuaObject() { return m_luaObject; diff --git a/src/game/game_object.h b/src/game/game_object.h index 1f47a98..51b818b 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -141,6 +141,7 @@ public: int Lua_GetCurrentAnimation(LuaPlus::LuaState* state); int Lua_GetCurrentAnimationTime(LuaPlus::LuaState* state); int Lua_GetAnimationTime(LuaPlus::LuaState* state); + int Lua_SetAnimationSpeed(LuaPlus::LuaState* state); const LuaPlus::LuaObject& GetLuaObject(); diff --git a/src/render/animation.cpp b/src/render/animation.cpp index c75e7ed..cfa7895 100644 --- a/src/render/animation.cpp +++ b/src/render/animation.cpp @@ -6,6 +6,7 @@ SkeletonInstance::SkeletonInstance() : m_model(nullptr), m_animation(INVALID_ANIMATION_HANDLE), m_time(0.0f), + m_speed(1.0f), m_looped(false) { } @@ -52,7 +53,7 @@ float SkeletonInstance::GetAnimationTime(AnimationId_t id) float frameTime = 1.0f / animation->framerate; float totalDuration = animation->numFrames * frameTime; - return totalDuration; + return totalDuration / m_speed; } void SkeletonInstance::PlayAnimation(AnimationId_t id, bool looped) @@ -60,6 +61,7 @@ void SkeletonInstance::PlayAnimation(AnimationId_t id, bool looped) m_time = 0.0f; m_animation = id; m_looped = looped; + m_speed = 1.0f; } void SkeletonInstance::StopAnimation() @@ -67,6 +69,12 @@ void SkeletonInstance::StopAnimation() m_time = 0.0f; m_animation = INVALID_ANIMATION_HANDLE; m_looped = -1; + m_speed = 1.0f; +} + +void SkeletonInstance::SetAnimationSpeed(float speed) +{ + m_speed = speed; } AnimationId_t SkeletonInstance::GetCurrentAnimation() diff --git a/src/render/animation.h b/src/render/animation.h index 2b6f92d..71e7cc4 100644 --- a/src/render/animation.h +++ b/src/render/animation.h @@ -43,6 +43,7 @@ public: std::vector m_finalMatrices; AnimationId_t m_animation; float m_time; + float m_speed; bool m_looped; public: @@ -60,6 +61,8 @@ public: void PlayAnimation(AnimationId_t id, bool looped); void StopAnimation(); + void SetAnimationSpeed(float speed); + AnimationId_t GetCurrentAnimation(); float GetCurrentTime(); diff --git a/src/render/model.cpp b/src/render/model.cpp index a0e0f42..c231583 100644 --- a/src/render/model.cpp +++ b/src/render/model.cpp @@ -817,7 +817,7 @@ void Model::UpdateSkeletonInstance(SkeletonInstance* instance, float dt) const Animation& animation = m_animations[instance->m_animation]; - instance->m_time += dt; + instance->m_time += dt * instance->m_speed; float frameTime = 1.0f / animation.framerate; float totalDuration = (animation.numFrames - 1) * frameTime;