From cd2d28a83c61ecde803593e73e37533a3a0d943e Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Tue, 7 Jul 2026 18:12:25 +0800 Subject: [PATCH] feat(gameplay): add walking sound for players and adjust underwater bubble interval - Move walk/run sound interval constants from anonymous namespace to ClientPlayer header - Add moving_time field to PlayerInfo to track continuous movement - Implement play_walk_sound callback triggered by WALK and RUN gaits - Increase underwater bubble timer from 1.0s to 1.5s --- include/Cubed/gameplay/client_player.hpp | 2 ++ include/Cubed/gameplay/client_world.hpp | 1 + src/gameplay/client_player.cpp | 5 +--- src/gameplay/client_world.cpp | 32 +++++++++++++++++++++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/Cubed/gameplay/client_player.hpp b/include/Cubed/gameplay/client_player.hpp index 30e2a1d..cdaf56e 100644 --- a/include/Cubed/gameplay/client_player.hpp +++ b/include/Cubed/gameplay/client_player.hpp @@ -17,6 +17,8 @@ namespace Cubed { class ClientWorld; class ClientPlayer { public: + static constexpr float WALK_SOUND_INTERVAL = 0.45f; + static constexpr float RUN_SOUND_INTERVAL = 0.3f; using ChunkPosSet = absl::flat_hash_set; ClientPlayer(ClientWorld& world); ~ClientPlayer(); diff --git a/include/Cubed/gameplay/client_world.hpp b/include/Cubed/gameplay/client_world.hpp index db42f56..cb5a22f 100644 --- a/include/Cubed/gameplay/client_world.hpp +++ b/include/Cubed/gameplay/client_world.hpp @@ -28,6 +28,7 @@ struct PlayerInfo { Gait gait; float angle = 0.0f; float walk_time = 0.0f; + float moving_time = 0.0f; }; struct PlayerRenderData { diff --git a/src/gameplay/client_player.cpp b/src/gameplay/client_player.cpp index 222e437..f1a0ccb 100644 --- a/src/gameplay/client_player.cpp +++ b/src/gameplay/client_player.cpp @@ -5,10 +5,7 @@ #include "Cubed/debug_collector.hpp" #include "Cubed/gameplay/client_world.hpp" -namespace { -constexpr float WALK_SOUND_INTERVAL = 0.45f; -constexpr float RUN_SOUND_INTERVAL = 0.3f; -} // namespace +namespace {} // namespace namespace Cubed { ClientPlayer::ClientPlayer(ClientWorld& world) : m_world(world) {} diff --git a/src/gameplay/client_world.cpp b/src/gameplay/client_world.cpp index 2105fae..b31b571 100644 --- a/src/gameplay/client_world.cpp +++ b/src/gameplay/client_world.cpp @@ -398,7 +398,7 @@ void ClientWorld::init(std::string_view player_name, } }); - m_timers.try_emplace("under water bubble", 1.0f, [this]() { + m_timers.try_emplace("under water bubble", 1.5f, [this]() { if (m_player.is_underwater()) { auto ans = m_random.random_int(1, 2); std::string sound = @@ -794,6 +794,36 @@ void ClientWorld::update(float delta_time) { player.angle = glm::mix(player.angle, 0.0f, t); } + // walking sound + if (player.gait == Gait::STOP) { + player.moving_time = 0.0f; + } else { + player.moving_time += delta_time; + } + auto play_walk_sound = [&]() { + glm::ivec3 block = glm::floor(player.render_pos); + block.y -= 1; + BlockType id = get_block_tpye(block); + if (id == 0) { + return; + } + std::string name = BlockManager::name_form_id(id); + std::string sound = "block/" + name + "/walk.ogg"; + m_audio.play_3d(sound, player.render_pos); + }; + if (player.gait == Gait::WALK) { + if (player.moving_time >= ClientPlayer::WALK_SOUND_INTERVAL) { + player.moving_time = 0.0f; + play_walk_sound(); + } + } + if (player.gait == Gait::RUN) { + if (player.moving_time >= ClientPlayer::RUN_SOUND_INTERVAL) { + player.moving_time = 0.0f; + play_walk_sound(); + } + } + m_render_player_data.emplace_back( player.name, player.uuid, player.render_pos, player.render_yaw, player.render_pitch, player.gait, player.angle);