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);