diff --git a/assets/sound/creature/pig/call.mp3 b/assets/sound/creature/pig/call.mp3 new file mode 100644 index 0000000..1075145 Binary files /dev/null and b/assets/sound/creature/pig/call.mp3 differ diff --git a/include/Cubed/gameplay/client_entity_manager.hpp b/include/Cubed/gameplay/client_entity_manager.hpp index 427e898..1230edf 100644 --- a/include/Cubed/gameplay/client_entity_manager.hpp +++ b/include/Cubed/gameplay/client_entity_manager.hpp @@ -1,6 +1,7 @@ #pragma once #include "Cubed/gameplay/ecs/entity.hpp" #include "Cubed/gameplay/gait.hpp" +#include "Cubed/tools/cubed_random.hpp" #include "glm/ext/vector_float3.hpp" #include "world/entity.pb.h" @@ -26,6 +27,8 @@ public: const entt::registry& get_registry() const; + void player_sound(float dt); + private: struct EntityCreateElement { EntityID id; @@ -52,7 +55,7 @@ private: EntityMap m_entities; std::unordered_map m_factories; tbb::concurrent_queue m_tasks; - + Random m_random; void handle_task(float dt); void handle_entity_destory(EntityID id); // not thread safe diff --git a/include/Cubed/gameplay/ecs/client_entity.hpp b/include/Cubed/gameplay/ecs/client_entity.hpp index 1b21280..7f0d98a 100644 --- a/include/Cubed/gameplay/ecs/client_entity.hpp +++ b/include/Cubed/gameplay/ecs/client_entity.hpp @@ -11,4 +11,9 @@ struct BaseClientCreature { ModelID model; }; + +struct SoundTime { + float next_call_time; +}; + } // namespace Cubed \ No newline at end of file diff --git a/src/gameplay/client_entity_manager.cpp b/src/gameplay/client_entity_manager.cpp index b8c7d11..7a0a72a 100644 --- a/src/gameplay/client_entity_manager.cpp +++ b/src/gameplay/client_entity_manager.cpp @@ -7,8 +7,8 @@ #include "Cubed/gameplay/ecs/transform.hpp" #include "Cubed/render/model_manager.hpp" #include "Cubed/tools/cubed_assert.hpp" +#include "Cubed/tools/math_tools.hpp" #include "Cubed/tools/net_utils.hpp" - using namespace google::protobuf; namespace Cubed { @@ -25,15 +25,20 @@ void ClientEntityManager::update(float dt) { c.pose.walk_time += dt; } } + + player_sound(dt); } void ClientEntityManager::init() { + m_random.init(std::random_device()()); m_factories.emplace("cubed:pig", [this](EntityID id) { BaseClientCreature c; c.model = ModelManager::instance().get_model_id("cubed:pig"); + float next_call_time = m_random.random_float(8.0f, 25.0f); create_entity_in_registry(id, Entity{id, EntityType::CREATURE}, EntityInfo{"cubed:pig", ""}, std::move(c), - PigTag{}, RenderTransform{}); + PigTag{}, RenderTransform{}, + SoundTime{next_call_time}); }); } // not thread safe @@ -157,4 +162,27 @@ void ClientEntityManager::handle_entity_destory(EntityID id) { const entt::registry& ClientEntityManager::get_registry() const { return m_registry; } + +void ClientEntityManager::player_sound(float dt) { + auto& audio = m_world.get_audio(); + auto player_pos = m_world.player_manager().get_local().get_player_pos(); + auto view = m_registry.view(); + + for (auto e : view) { + const auto [info, creature] = + view.get(e); + if (Math::distance2(player_pos, creature.transform.position.value) > + 10 * 10) { + continue; + } + auto& sound_time = view.get(e); + sound_time.next_call_time -= dt; + if (sound_time.next_call_time <= 0.0f) { + audio.play_3d("creature/pig/call.mp3", + creature.transform.position.value); + sound_time.next_call_time = m_random.random_float(8.0f, 25.0f); + } + } +} + } // namespace Cubed \ No newline at end of file