feat(audio): add ambient pig sounds

Add pig call audio with randomized timing, triggered by proximity to the player as 3D positional sound.
This commit is contained in:
2026-08-07 08:41:56 +08:00
parent 9745ccc7d6
commit 6e7bef8626
4 changed files with 39 additions and 3 deletions

Binary file not shown.

View File

@@ -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<std::string_view, CreateFunc> m_factories;
tbb::concurrent_queue<TaskPair> m_tasks;
Random m_random;
void handle_task(float dt);
void handle_entity_destory(EntityID id);
// not thread safe

View File

@@ -11,4 +11,9 @@ struct BaseClientCreature {
ModelID model;
};
struct SoundTime {
float next_call_time;
};
} // namespace Cubed

View File

@@ -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<EntityInfo, BaseClientCreature, SoundTime>();
for (auto e : view) {
const auto [info, creature] =
view.get<EntityInfo, BaseClientCreature>(e);
if (Math::distance2(player_pos, creature.transform.position.value) >
10 * 10) {
continue;
}
auto& sound_time = view.get<SoundTime>(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