diff --git a/include/Engine/Sound/EPlayAnnouncerVoice.h b/include/Engine/Sound/EPlayAnnouncerVoice.h new file mode 100644 index 00000000..18d1a953 --- /dev/null +++ b/include/Engine/Sound/EPlayAnnouncerVoice.h @@ -0,0 +1,17 @@ +#ifndef Events_PlayAnnouncerVoice_h__ +#define Events_PlayAnnouncerVoice_h__ + +#include +#include "../Engine/Core/EventBroker.h" + +namespace Events +{ + +struct PlayAnonuncerVoice : public Event +{ + std::string FilePath = ""; +}; + +} + +#endif diff --git a/include/Engine/Sound/ESetAnnouncerGain.h b/include/Engine/Sound/ESetAnnouncerGain.h new file mode 100644 index 00000000..2149be80 --- /dev/null +++ b/include/Engine/Sound/ESetAnnouncerGain.h @@ -0,0 +1,16 @@ +#ifndef Events_SetAnnouncerGain_h__ +#define Events_SetAnnouncerGain_h__ + +#include "../Engine/Core/EventBroker.h" + +namespace Events +{ + +struct SetAnnouncerGain : public Event +{ + float Gain = 1; +}; + +} + +#endif // diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index cad197a5..9a4d68f2 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -19,11 +19,13 @@ #include "../Engine/Sound/EPlaySoundOnEntity.h" #include "../Engine/Sound/EPlaySoundOnPosition.h" #include "../Engine/Sound/EPlayBackgroundMusic.h" +#include "../Engine/Sound/EPlayAnnouncerVoice.h" #include "../Engine/Sound/EPauseSound.h" #include "../Engine/Sound/EContinueSound.h" #include "../Engine/Sound/EStopSound.h" #include "../Engine/Sound/ESetBGMGain.h" #include "../Engine/Sound/ESetSFXGain.h" +#include "../Engine/Sound/ESetAnnouncerGain.h" #include "../Engine/Sound/EChangeBGM.h" #include "../Engine/Core/EPause.h" #include "../Engine/Core/EComponentAttached.h" @@ -34,7 +36,8 @@ typedef std::pair> QueuedBuffers; enum class SoundType { SFX, - BGM + BGM, + Announcer }; struct Source @@ -98,6 +101,7 @@ private: float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; + float m_AnnouncerVolumeChannel = 1.0f; EntityWrapper m_LocalPlayer = EntityWrapper(); // Events @@ -107,6 +111,8 @@ private: bool OnPlaySoundOnPosition(const Events::PlaySoundOnPosition &e); EventRelay m_EPlayBackgroundMusic; bool OnPlayBackgroundMusic(const Events::PlayBackgroundMusic &e); + EventRelay m_EPlayAnnouncerVoice; + bool OnPlayAnnouncerVoice(const Events::PlayAnonuncerVoice& e); EventRelay m_EPauseSound; bool OnPauseSound(const Events::PauseSound &e); EventRelay m_EStopSound; @@ -117,6 +123,8 @@ private: bool OnSetBGMGain(const Events::SetBGMGain &e); EventRelay m_ESetSFXGain; bool OnSetSFXGain(const Events::SetSFXGain &e); + EventRelay m_ESetAnnouncerGain; + bool OnSetAnnouncerGain(const Events::SetAnnouncerGain& e); EventRelay m_EComponentAttached; bool OnComponentAttached(const Events::ComponentAttached &e); EventRelay m_EPause; diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index 962eb085..37e78d32 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -20,6 +20,7 @@ #include "../Engine/Collision/ETrigger.h" #include "../Engine/Sound/EPlaySoundOnEntity.h" #include "../Engine/Sound/EPlayBackgroundMusic.h" +#include "../Engine/Sound/EPlayAnnouncerVoice.h" #include "../Game/Events/EDoubleJump.h" #include "../Game/Events/EDashAbility.h" diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index 60ee4823..c118070b 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -36,4 +36,5 @@ ResourceLoading=true [Sound] BGMVolume=1.0 SFXVolume=1.0 +AnnouncerVolume=1.0 Announcer=female \ No newline at end of file diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 31861ad0..7b6fef79 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -7,6 +7,7 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker) m_World = world; m_BGMVolumeChannel = config->Get("Sound.BGMVolume", 1.f); m_SFXVolumeChannel = config->Get("Sound.SFXVolume", 1.f); + m_AnnouncerVolumeChannel = config->Get("Sound.AnnouncerVolume", 1.f); initOpenAL(); alSpeedOfSound(340.29f); @@ -19,16 +20,19 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker) EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnEntity, &SoundManager::OnPlaySoundOnEntity); EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnPosition, &SoundManager::OnPlaySoundOnPosition); EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundManager::OnPlayBackgroundMusic); + EVENT_SUBSCRIBE_MEMBER(m_EPlayAnnouncerVoice, &SoundManager::OnPlayAnnouncerVoice); EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundManager::OnStopSound); EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundManager::OnPauseSound); EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundManager::OnContinueSound); EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundManager::OnSetBGMGain); EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundManager::OnSetSFXGain); + EVENT_SUBSCRIBE_MEMBER(m_ESetAnnouncerGain, &SoundManager::OnSetAnnouncerGain); EVENT_SUBSCRIBE_MEMBER(m_EPause, &SoundManager::OnPause); EVENT_SUBSCRIBE_MEMBER(m_EResume, &SoundManager::OnResume); EVENT_SUBSCRIBE_MEMBER(m_EComponentAttached, &SoundManager::OnComponentAttached); EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_EPlayQueueOnEntity, &SoundManager::OnPlayQueueOnEntity); + EVENT_SUBSCRIBE_MEMBER(m_EChangeBGM, &SoundManager::OnChangeBGM); } SoundManager::~SoundManager() @@ -272,6 +276,28 @@ bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) return true; } + +bool SoundManager::OnPlayAnnouncerVoice(const Events::PlayAnonuncerVoice& e) +{ + auto listenerComponents = m_World->GetComponents("Listener"); + for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { + if ((*it).EntityID != m_LocalPlayer.ID) { + break; + } + auto emitterChild = m_World->CreateEntity((*it).EntityID); + auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter"); + (bool&)emitter["Loop"] = false; + (std::string&)emitter["FilePath"] = e.FilePath; + m_World->AttachComponent(emitterChild, "Transform"); + Source* source = createSource(e.FilePath); + source->Type = SoundType::Announcer; + setSoundProperties(source, &emitter); + m_Sources[emitterChild] = source; + playSound(source); + } + return true; +} + bool SoundManager::OnSetBGMGain(const Events::SetBGMGain & e) { m_BGMVolumeChannel = e.Gain; @@ -284,6 +310,13 @@ bool SoundManager::OnSetSFXGain(const Events::SetSFXGain & e) return true; } + +bool SoundManager::OnSetAnnouncerGain(const Events::SetAnnouncerGain& e) +{ + m_AnnouncerVolumeChannel = e.Gain; + return true; +} + bool SoundManager::OnComponentAttached(const Events::ComponentAttached & e) { if (e.Component.Info.Name == "SoundEmitter") { @@ -361,7 +394,13 @@ void SoundManager::setGain(Source * source, float gain) void SoundManager::setSoundProperties(Source* source, ComponentWrapper* soundComponent) { - float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel; + float gain; + switch (source->Type) { + case SoundType::SFX: gain = m_SFXVolumeChannel; break; + case SoundType::BGM: gain = m_BGMVolumeChannel; break; + case SoundType::Announcer: gain = m_AnnouncerVolumeChannel; break; + default: gain = 1.f; break; + } alSourcef(source->ALsource, AL_GAIN, (float)(double)(*soundComponent)["Gain"] * gain); alSourcef(source->ALsource, AL_PITCH, (float)(double)(*soundComponent)["Pitch"]); alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index aed70584..8135eb56 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -36,8 +36,7 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) { if (e.PlayerID == -1) { // Local player m_World->AttachComponent(e.Player.ID, "Listener"); - Events::PlaySoundOnEntity go; - go.EmitterID = LocalPlayer.ID; + Events::PlayAnonuncerVoice go; go.FilePath = "Audio/announcer/" + m_Announcer + "/go.wav"; m_EventBroker->Publish(go); // TEMP: starts bgm @@ -92,13 +91,12 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) { int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "Team")["Team"]; int team = (int)m_World->GetComponent(LocalPlayer.ID, "Team")["Team"]; - Events::PlaySoundOnEntity ev; + Events::PlayAnonuncerVoice ev; if (team == homeTeam) { ev.FilePath = "Audio/announcer/" + m_Announcer + "/objective_achieved.wav"; } else { ev.FilePath = "Audio/announcer/" + m_Announcer + "/objective_failed.wav"; // have not been tested } - ev.EmitterID = LocalPlayer.ID; m_EventBroker->Publish(ev); // Temp for play test. m_DrumsIsPlaying = false;