diff --git a/include/Engine/Sound/EPlaySoundOnEntity.h b/include/Engine/Sound/EPlaySoundOnEntity.h index 39dea432..12be68b9 100644 --- a/include/Engine/Sound/EPlaySoundOnEntity.h +++ b/include/Engine/Sound/EPlaySoundOnEntity.h @@ -12,7 +12,8 @@ namespace Events // Sound behavior is thereby specified in the SoundEmitter component. struct PlaySoundOnEntity : public Event { - EntityID EmitterID = 0; + EntityWrapper Emitter = EntityWrapper::Invalid; + float Gain = 1.f; std::string FilePath = ""; }; diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 57b710cf..ae1c79ce 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -222,10 +222,12 @@ bool SoundManager::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); source->Type = SoundType::SFX; - EntityID child = m_World->CreateEntity(e.EmitterID); + EntityID child = m_World->CreateEntity(e.Emitter.ID); m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); + auto cEmitter = m_World->AttachComponent(child, "SoundEmitter"); + (double&)(float)cEmitter["Gain"] = e.Gain; m_Sources[child] = source; + setGain(source, cEmitter["Gain"]); playSound(source); return false; } diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index 72008e05..2a973a51 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -287,7 +287,7 @@ void PlayerMovementSystem::playerStep(double dt) // Player moved a step's distance // Create footstep sound Events::PlaySoundOnEntity e; - e.EmitterID = m_LocalPlayer.ID; + e.Emitter = m_LocalPlayer; e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav"; m_LeftFoot = !m_LeftFoot; m_EventBroker->Publish(e); diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 93ab4469..de221f36 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -60,7 +60,7 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) auto model = m_World->AttachComponent(entity.ID, "Model"); (std::string&)model["Resource"] = "Models/Core/UnitCube.mesh"; Events::PlaySoundOnEntity ev; - ev.EmitterID = entity.ID; + ev.Emitter = entity; ev.FilePath = "Audio/crosscounter.wav"; m_EventBroker->Publish(ev); } @@ -77,7 +77,7 @@ void SoundSystem::playerJumps() bool grounded = (bool)m_World->GetComponent(LocalPlayer.ID, "Physics")["IsOnGround"]; if (grounded) { Events::PlaySoundOnEntity e; - e.EmitterID = LocalPlayer.ID; + e.Emitter = LocalPlayer; e.FilePath = "Audio/Jump/Jump1.wav"; m_EventBroker->Publish(e); } @@ -106,7 +106,7 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) if (!IsClient) { // Only play for clients return false; } - if (LocalPlayer.ID = e.Victim.ID) { // You're local player was the one who took dmg + if(!e.Victim.Valid()) { return false; } std::uniform_int_distribution dist(1, 12); @@ -115,7 +115,7 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) paths.push_back("Audio/Hurt/Hurt" + std::to_string(rand) + ".wav"); Events::PlayQueueOnEntity ev; - ev.Emitter = LocalPlayer; + ev.Emitter = e.Victim; ev.FilePaths = paths; m_EventBroker->Publish(ev); return false; @@ -123,7 +123,7 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) { - if (e.Player.ID != LocalPlayer.ID) { + if (!e.Player.Valid()) { return false; } if (!IsClient) { @@ -132,7 +132,8 @@ bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) // The local player is dead. The local player might be invalid? // Play the sound from the listener. // TODO: We might want to hear other players die. - Events::PlayBackgroundMusic ev; + Events::PlaySoundOnEntity ev; + ev.Emitter = e.Player; ev.FilePath = "Audio/Die/Die2.wav"; m_EventBroker->Publish(ev); return false; @@ -141,7 +142,7 @@ bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) { Events::PlaySoundOnEntity ev; - ev.EmitterID = LocalPlayer.ID; + ev.Emitter = LocalPlayer; ev.FilePath = "Audio/Pickup/Pickup2.wav"; m_EventBroker->Publish(ev); return false; @@ -156,7 +157,7 @@ bool SoundSystem::OnDoubleJump(const Events::DoubleJump & e) return false; } Events::PlaySoundOnEntity ev; - ev.EmitterID = e.entityID; + ev.Emitter = EntityWrapper(m_World, e.entityID); ev.FilePath = "Audio/Jump/Jump2.wav"; m_EventBroker->Publish(ev); return false; diff --git a/src/Game/Systems/Weapon/DefenderWeaponBehaviour.cpp b/src/Game/Systems/Weapon/DefenderWeaponBehaviour.cpp index f148f6de..08678177 100644 --- a/src/Game/Systems/Weapon/DefenderWeaponBehaviour.cpp +++ b/src/Game/Systems/Weapon/DefenderWeaponBehaviour.cpp @@ -87,9 +87,12 @@ void DefenderWeaponBehaviour::fireShell(WeaponInfo& wi) if (weaponModelEntity.Valid()) { EntityWrapper spawner = weaponModelEntity.FirstChildByName("WeaponMuzzle"); Events::PlaySoundOnEntity e; - e.EmitterID = weaponModelEntity.ID; + e.Emitter = weaponModelEntity; e.FilePath = "Audio/weapon/Shotgun/ShotgunFire.wav"; - m_EventBroker->Publish(e); + e.Gain = 1.f; + if (IsClient) { + m_EventBroker->Publish(e); + } for (auto& angles : pelletAngles) { glm::vec3 direction = Transform::AbsoluteOrientation(spawner) * glm::quat(glm::vec3(angles, 0.f)) * glm::vec3(0, 0, -1); float distance = traceRayDistance(Transform::AbsolutePosition(spawner), direction);