From 25ef8f39341724550c3af19873e7f324a5a57251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 2 Jul 2023 12:47:18 +0200 Subject: [PATCH] Fix local animations not resetting Converts `LocalPlayerAnimation` to a scoped enum to prevent such bugs in the future --- src/client/content_cao.cpp | 17 +++++++++-------- src/client/localplayer.h | 4 ++-- src/network/clientpackethandler.cpp | 3 ++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index 17ff4d150..f80b72a32 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -1032,7 +1032,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env) rot_translator.val_current = m_rotation; if (m_is_visible) { - int old_anim = player->last_animation; + LocalPlayerAnimation old_anim = player->last_animation; float old_anim_speed = player->last_animation_speed; m_velocity = v3f(0,0,0); m_acceleration = v3f(0,0,0); @@ -1062,13 +1062,13 @@ void GenericCAO::step(float dtime, ClientEnvironment *env) if (walking && (controls.dig || controls.place)) { new_anim = player->local_animations[3]; - player->last_animation = WD_ANIM; + player->last_animation = LocalPlayerAnimation::WD_ANIM; } else if (walking) { new_anim = player->local_animations[1]; - player->last_animation = WALK_ANIM; + player->last_animation = LocalPlayerAnimation::WALK_ANIM; } else if (controls.dig || controls.place) { new_anim = player->local_animations[2]; - player->last_animation = DIG_ANIM; + player->last_animation = LocalPlayerAnimation::DIG_ANIM; } // Apply animations if input detected and not attached @@ -1079,9 +1079,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env) m_animation_speed = new_speed; player->last_animation_speed = m_animation_speed; } else { - player->last_animation = NO_ANIM; + player->last_animation = LocalPlayerAnimation::NO_ANIM; - if (old_anim != NO_ANIM) { + if (old_anim != LocalPlayerAnimation::NO_ANIM) { m_animation_range = player->local_animations[0]; updateAnimation(); } @@ -1090,7 +1090,8 @@ void GenericCAO::step(float dtime, ClientEnvironment *env) // Update local player animations if ((player->last_animation != old_anim || m_animation_speed != old_anim_speed) && - player->last_animation != NO_ANIM && allow_update) + player->last_animation != LocalPlayerAnimation::NO_ANIM && + allow_update) updateAnimation(); } @@ -1801,7 +1802,7 @@ void GenericCAO::processMessage(const std::string &data) updateAnimation(); } else { LocalPlayer *player = m_env->getLocalPlayer(); - if(player->last_animation == NO_ANIM) + if(player->last_animation == LocalPlayerAnimation::NO_ANIM) { m_animation_range = v2s32((s32)range.X, (s32)range.Y); m_animation_speed = readF32(is); diff --git a/src/client/localplayer.h b/src/client/localplayer.h index 42c4b073a..f33aab4b3 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -34,7 +34,7 @@ class ClientEnvironment; class IGameDef; struct collisionMoveResult; -enum LocalPlayerAnimations +enum class LocalPlayerAnimation { NO_ANIM, WALK_ANIM, @@ -90,7 +90,7 @@ public: bool makes_footstep_sound = true; - int last_animation = NO_ANIM; + LocalPlayerAnimation last_animation = LocalPlayerAnimation::NO_ANIM; float last_animation_speed = 0.0f; std::string hotbar_image = ""; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 33d470e9f..d75a4e68b 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/strfnd.h" #include "client/clientevent.h" #include "client/sound.h" +#include "client/localplayer.h" #include "network/clientopcodes.h" #include "network/connection.h" #include "network/networkpacket.h" @@ -1508,7 +1509,7 @@ void Client::handleCommand_LocalPlayerAnimations(NetworkPacket* pkt) *pkt >> player->local_animations[3]; *pkt >> player->local_animation_speed; - player->last_animation = -1; + player->last_animation = LocalPlayerAnimation::NO_ANIM; } void Client::handleCommand_EyeOffset(NetworkPacket* pkt)