From 246f56c82363b455b9de82ad114eb921374a2759 Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 27 Jan 2016 15:40:01 +0100 Subject: [PATCH 01/38] Removed brainless logic. --- src/Engine/Sound/SoundSystem.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index a55c1fee..ba46379e 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -71,7 +71,6 @@ void SoundSystem::deleteInactiveEmitters() alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); m_World->DeleteEntity(it->first); - delete it->second; it = m_Sources.erase(it); } } else { From 950f3f3cf7a02a3950a74eb0445abca0af7e9568 Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 27 Jan 2016 15:43:40 +0100 Subject: [PATCH 02/38] Fixed last commit. Removed the wrong thing. --- src/Engine/Sound/SoundSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index ba46379e..d0887c05 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -70,7 +70,7 @@ void SoundSystem::deleteInactiveEmitters() // Sound has been stopped / finished playing. alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); - m_World->DeleteEntity(it->first); + delete it->second; it = m_Sources.erase(it); } } else { From 25b3c02d29bb8fdc4161867ad42899c4c9229416 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 1 Feb 2016 14:32:01 +0100 Subject: [PATCH 03/38] Adding sounds for: jump, walk, shoot --- assets | 2 +- include/Engine/Sound/SoundSystem.h | 39 ++++- resources/Schema/Entities/aim_rays.xml | 210 +++++++++++++++++++++++++ src/Engine/Sound/SoundSystem.cpp | 129 ++++++++++++++- 4 files changed, 368 insertions(+), 12 deletions(-) create mode 100644 resources/Schema/Entities/aim_rays.xml diff --git a/assets b/assets index e4dc9529..9b4f9ab2 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit e4dc9529f2178d373808ddf487165fa50a641a78 +Subproject commit 9b4f9ab2e67fc09863f35b33bb0a61ce75ef6aa8 diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index b9cc2589..5d57d5b6 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -20,6 +20,10 @@ #include "Sound/EStopSound.h" #include "Sound/ESetBGMGain.h" #include "Sound/ESetSFXGain.h" +#include "Core/EShoot.h" +#include "Core/EPlayerSpawned.h" +#include "Input/EInputCommand.h" +#include "Core/ECaptured.h" enum class SoundType { SFX, @@ -55,18 +59,24 @@ private: // Logic void initOpenAL(); - void updateEmitters(double dt); - void updateListener(double dt); - void deleteInactiveEmitters(); void addNewEmitters(double dt); - Source* createSource(std::string filePath); - void playSound(Source* source); - void stopSound(Source* source); + void updateEmitters(double dt); + void deleteInactiveEmitters(); void stopEmitters(); + void updateListener(double dt); + Source* createSource(std::string filePath); ALenum getSourceState(ALuint source); void setGain(Source* source, float gain); void setSoundProperties(ALuint source, ComponentWrapper* soundComponent); + // Specific logic + void playSound(Source* source); + void stopSound(Source* source); + void playerDamaged(); + void playerShot(); + void playerJumps(); + void playerStep(double dt); + // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; ALCcontext* m_ALCcontext = nullptr; @@ -75,9 +85,14 @@ private: World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; std::unordered_map m_Sources; - float m_BGMVolumeChannel = 1.0f; + float m_BGMVolumeChannel = 1.f; float m_SFXVolumeChannel = 1.f; bool m_EditorEnabled = false; + const double m_PlayerFootstepInterval = 0.5; + double m_TimeSinceLastFootstep = 0; + // TEMP + EntityID m_LocalPlayer = EntityID_Invalid; + bool m_LeftFoot = false; // Events EventRelay m_EPlaySoundOnEntity; @@ -96,6 +111,16 @@ private: bool OnSetBGMGain(const Events::SetBGMGain &e); // Not tested EventRelay m_ESetSFXGain; bool OnSetSFXGain(const Events::SetSFXGain &e); // Not tested + + EventRelay m_EShoot; + bool OnShoot(const Events::Shoot &e); + EventRelay m_EPlayerSpawned; + bool OnPlayerSpawned(const Events::PlayerSpawned &e); + EventRelay m_EInputCommand; + bool OnInputCommand(const Events::InputCommand &e); + EventRelay m_ECaptured; + bool OnCaptured(const Events::Captured &e); + }; #endif \ No newline at end of file diff --git a/resources/Schema/Entities/aim_rays.xml b/resources/Schema/Entities/aim_rays.xml new file mode 100644 index 00000000..c488ca51 --- /dev/null +++ b/resources/Schema/Entities/aim_rays.xml @@ -0,0 +1,210 @@ + + + + + + + + + + + + + + models/core/unitcube.mesh + + + + + + + + + + + + + + + models/core/unitcube.mesh + + + + + + + + + + + + + + + + + + + + + + + + models/core/unitcube.mesh + + + + + + + + + + + + + + + models/core/unitcube.mesh + + + + + + + + + + + + + + + models/core/unitcube.mesh + + + + + + + + + + + + + + Schema/Entities/Player.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Schema/Entities/Player.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + models/core/unitcube.mesh + + + + + + + + + + + diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index d0887c05..dbabb32d 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -20,6 +20,10 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound); EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundSystem::OnSetBGMGain); EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain); + EVENT_SUBSCRIBE_MEMBER(m_EShoot, &SoundSystem::OnShoot); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundSystem::OnInputCommand); + EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); } SoundSystem::~SoundSystem() @@ -48,19 +52,20 @@ void SoundSystem::stopEmitters() } void SoundSystem::Update(double dt) -{ +{ m_EventBroker->Process(); + playerStep(dt); addNewEmitters(dt); // can be optimized with "EEntityCreated" deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" - updateEmitters( dt); - updateListener( dt); + updateEmitters(dt); + updateListener(dt); } void SoundSystem::deleteInactiveEmitters() { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end();) { - if (m_World->ValidEntity(it->first) + if (m_World->ValidEntity(it->first) && m_World->HasComponent(it->first, "SoundEmitter")) { if (getSourceState(it->second->ALsource) != AL_STOPPED) { // Nothing to see here, move along @@ -70,6 +75,7 @@ void SoundSystem::deleteInactiveEmitters() // Sound has been stopped / finished playing. alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); + m_World->DeleteEntity(it->first); delete it->second; it = m_Sources.erase(it); } @@ -174,6 +180,58 @@ void SoundSystem::stopSound(Source* source) alSourceStop(source->ALsource); } +void SoundSystem::playerDamaged() +{ + +} + +void SoundSystem::playerShot() +{ } + +void SoundSystem::playerJumps() +{ + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; + if (vel.y > 1) { + Source* source = createSource("Audio/jump/jump1.wav"); + auto emitterID = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(emitterID, "Transform"); + m_World->AttachComponent(emitterID, "SoundEmitter"); + source->Type = SoundType::SFX; + m_Sources[emitterID] = source; + playSound(source); + } +} + +void SoundSystem::playerStep(double dt) +{ + if (m_LocalPlayer == EntityID_Invalid) { + return; + } + m_TimeSinceLastFootstep += dt; + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; + float playerSpeed = glm::length(vel); + bool isAirborne = vel.y != 0; + if (playerSpeed > 1 && !isAirborne) { + // Player is walking + if (m_TimeSinceLastFootstep > m_PlayerFootstepInterval) { + // Create footstep sound + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + Events::PlaySoundOnEntity e; + e.EmitterID = child; + if (m_LeftFoot) { + e.FilePath = "Audio/footstep/footstep2.wav"; + } else { + e.FilePath = "Audio/footstep/footstep3.wav"; + } + m_LeftFoot = !m_LeftFoot; + m_EventBroker->Publish(e); + m_TimeSinceLastFootstep = 0; + } + } +} + bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); @@ -251,6 +309,69 @@ bool SoundSystem::OnSetSFXGain(const Events::SetSFXGain & e) return true; } +bool SoundSystem::OnShoot(const Events::Shoot & e) +{ + Source* source = createSource("Audio/laser/laser1.wav"); + auto emitterID = m_World->CreateEntity(e.Player.ID); + m_World->AttachComponent(emitterID, "Transform"); + auto emitter = m_World->AttachComponent(emitterID, "SoundEmitter"); + source->Type = SoundType::SFX; + m_Sources[emitterID] = source; + playSound(source); + return true; +} + +bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned & e) +{ + if (e.PlayerID == -1) { // Local player + m_World->AttachComponent(e.Player.ID, "Listener"); + m_LocalPlayer = e.Player.ID; + EntityID child = m_World->CreateEntity(e.Player.ID); + m_World->AttachComponent(child, "SoundEmitter"); // Temp + m_World->AttachComponent(child, "Transform"); // Temp + Events::PlaySoundOnEntity event; + event.EmitterID = child; + event.FilePath = "Audio/announcer/go.wav"; + m_EventBroker->Publish(event); + } + return true; +} + +bool SoundSystem::OnInputCommand(const Events::InputCommand & e) +{ + if (e.Player.ID == EntityID_Invalid) { + //return false; + } + if (e.Command == "Jump" && e.Value > 0) { + if (e.PlayerID == -1) { // local player + //bool airBorne = ((glm::vec3)m_World->GetComponent(e.Player.ID, "Physics")["Velocity"]).y != 0; + //if (!airBorne) { + playerJumps(); + //} + return true; + } + } + return false; +} + +bool SoundSystem::OnCaptured(const Events::Captured & e) +{ + int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "CapturePoint")["HomePointForTeam"]; + int team = (int)m_World->GetComponent(m_LocalPlayer, "Team")["Team"]; + Events::PlaySoundOnEntity ev; + if (team == homeTeam) { + ev.FilePath = "Audio/announcer/objective_achieved.wav"; + } else { + ev.FilePath = "Audio/announcer/objective_failed.wav"; + } + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + ev.EmitterID = child; + m_EventBroker->Publish(ev); + return false; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. From 4ccda5ee43950a251449769dbdece96e50c02a88 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 1 Feb 2016 17:03:41 +0100 Subject: [PATCH 04/38] WIP --- include/Engine/Sound/SoundSystem.h | 11 ++++++++ src/Engine/Sound/SoundSystem.cpp | 43 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 5d57d5b6..4ceed60f 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -24,6 +24,9 @@ #include "Core/EPlayerSpawned.h" #include "Input/EInputCommand.h" #include "Core/ECaptured.h" +#include "Core/EPlayerDamage.h" +#include "Core/EPlayerDeath.h" +#include "Core/EPlayerHealthPickup.h" enum class SoundType { SFX, @@ -120,6 +123,14 @@ private: bool OnInputCommand(const Events::InputCommand &e); EventRelay m_ECaptured; bool OnCaptured(const Events::Captured &e); + EventRelay m_EPlayerDamage; + bool OnPlayerDamage(const Events::PlayerDamage &e); + EventRelay m_EPlayerDeath; + bool OnPlayerDeath(const Events::PlayerDeath &e); + EventRelay m_EPlayerHealthPickup; + bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); + + }; diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index dbabb32d..b4de9d31 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -122,6 +122,7 @@ void SoundSystem::updateEmitters(double dt) setSourceVel(it->second->ALsource, velocity); float gain; (bool)(it->second->Type) ? gain = m_SFXVolumeChannel : gain = m_BGMVolumeChannel; + auto emitter = m_World->GetComponent(it->first, "SoundEmitter"); setSoundProperties(it->second->ALsource, &emitter); @@ -372,6 +373,48 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) return false; } +bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) +{ + if (e.Player.ID == m_LocalPlayer) { + Events::PlaySoundOnEntity ev; + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + ev.EmitterID = child; + ev.FilePath = "Audio/hurt/hurt3.wav"; // random between a bunch + m_EventBroker->Publish(ev); + } + return false; +} + +bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) +{ + if (e.PlayerID == m_LocalPlayer) { + Events::PlaySoundOnEntity ev; + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + ev.EmitterID = child; + ev.FilePath = "Audio/die/die2.wav"; // random between a bunch + m_EventBroker->Publish(ev); + } + return false; +} + +bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) +{ + if (e.PlayerHealedID == m_LocalPlayer) { + Events::PlaySoundOnEntity ev; + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + ev.EmitterID = child; + ev.FilePath = "Audio/pickup/pickup2.wav"; + m_EventBroker->Publish(ev); + } + return false; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. From 4dcf5a8fa089a19e220667c193f0903704139a18 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 1 Feb 2016 17:05:15 +0100 Subject: [PATCH 05/38] #68 fixed warning. --- src/Engine/Sound/SoundSystem.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index b4de9d31..89798516 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -121,7 +121,11 @@ void SoundSystem::updateEmitters(double dt) setSourcePos(it->second->ALsource, nextPos); setSourceVel(it->second->ALsource, velocity); float gain; - (bool)(it->second->Type) ? gain = m_SFXVolumeChannel : gain = m_BGMVolumeChannel; + if (it->second->Type == SoundType::SFX) { + gain = m_SFXVolumeChannel; + } else if (it->second->Type == SoundType::BGM) { + gain = m_BGMVolumeChannel; + } auto emitter = m_World->GetComponent(it->first, "SoundEmitter"); setSoundProperties(it->second->ALsource, &emitter); From cb0feb0b84acd312f5ea8a145819e7de6ef702c7 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 2 Feb 2016 13:25:25 +0100 Subject: [PATCH 06/38] Tested hurt event. Randomizes between several different ones. --- assets | 2 +- include/Engine/Sound/SoundSystem.h | 9 +++-- src/Engine/Sound/SoundSystem.cpp | 55 +++++++++++++++++++----------- src/Game/Game.cpp | 3 +- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/assets b/assets index 9b4f9ab2..5bf03ccd 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 9b4f9ab2e67fc09863f35b33bb0a61ce75ef6aa8 +Subproject commit 5bf03ccd54bed358d525840635573f931adf584e diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 4ceed60f..e161af77 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -2,6 +2,7 @@ #define SoundSystem_h__ #include +#include #include "glm/common.hpp" #include "glm/gtx/rotate_vector.hpp" // Calculate Up vector @@ -27,6 +28,7 @@ #include "Core/EPlayerDamage.h" #include "Core/EPlayerDeath.h" #include "Core/EPlayerHealthPickup.h" +#include "Core/EComponentAttached.h" enum class SoundType { SFX, @@ -70,7 +72,7 @@ private: Source* createSource(std::string filePath); ALenum getSourceState(ALuint source); void setGain(Source* source, float gain); - void setSoundProperties(ALuint source, ComponentWrapper* soundComponent); + void setSoundProperties(Source* source, ComponentWrapper* soundComponent); // Specific logic void playSound(Source* source); @@ -91,11 +93,12 @@ private: float m_BGMVolumeChannel = 1.f; float m_SFXVolumeChannel = 1.f; bool m_EditorEnabled = false; - const double m_PlayerFootstepInterval = 0.5; + const double m_PlayerFootstepInterval = 1.0; double m_TimeSinceLastFootstep = 0; // TEMP EntityID m_LocalPlayer = EntityID_Invalid; bool m_LeftFoot = false; + std::default_random_engine generator; // Events EventRelay m_EPlaySoundOnEntity; @@ -129,6 +132,8 @@ private: bool OnPlayerDeath(const Events::PlayerDeath &e); EventRelay m_EPlayerHealthPickup; bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); + EventRelay m_EComponentAttached; + bool OnComponentAttached(const Events::ComponentAttached &e); diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 89798516..d08232ec 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -22,6 +22,7 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain); EVENT_SUBSCRIBE_MEMBER(m_EShoot, &SoundSystem::OnShoot); EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundSystem::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); } @@ -120,15 +121,9 @@ void SoundSystem::updateEmitters(double dt) glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt; setSourcePos(it->second->ALsource, nextPos); setSourceVel(it->second->ALsource, velocity); - float gain; - if (it->second->Type == SoundType::SFX) { - gain = m_SFXVolumeChannel; - } else if (it->second->Type == SoundType::BGM) { - gain = m_BGMVolumeChannel; - } auto emitter = m_World->GetComponent(it->first, "SoundEmitter"); - setSoundProperties(it->second->ALsource, &emitter); + setSoundProperties(it->second, &emitter); // To make an emitter play when spawned in editor mode if (m_EditorEnabled) { @@ -196,7 +191,7 @@ void SoundSystem::playerShot() void SoundSystem::playerJumps() { glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; - if (vel.y > 1) { + if (vel.y == 0) { Source* source = createSource("Audio/jump/jump1.wav"); auto emitterID = m_World->CreateEntity(m_LocalPlayer); m_World->AttachComponent(emitterID, "Transform"); @@ -205,6 +200,7 @@ void SoundSystem::playerJumps() m_Sources[emitterID] = source; playSound(source); } + } void SoundSystem::playerStep(double dt) @@ -218,7 +214,7 @@ void SoundSystem::playerStep(double dt) bool isAirborne = vel.y != 0; if (playerSpeed > 1 && !isAirborne) { // Player is walking - if (m_TimeSinceLastFootstep > m_PlayerFootstepInterval) { + if (m_TimeSinceLastFootstep * playerSpeed > m_PlayerFootstepInterval) { // Create footstep sound EntityID child = m_World->CreateEntity(m_LocalPlayer); m_World->AttachComponent(child, "Transform"); @@ -351,11 +347,19 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) if (e.PlayerID == -1) { // local player //bool airBorne = ((glm::vec3)m_World->GetComponent(e.Player.ID, "Physics")["Velocity"]).y != 0; //if (!airBorne) { - playerJumps(); + playerJumps(); //} return true; } } + if (e.Command == "TakeDamage" && e.Value > 0) { + if (e.PlayerID == -1) { //Local Player + Events::PlayerDamage ePlayerDamage; + ePlayerDamage.Damage = 1; + ePlayerDamage.Player = EntityWrapper(m_World, e.Player.ID); + m_EventBroker->Publish(ePlayerDamage); + } + } return false; } @@ -379,15 +383,17 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) { - if (e.Player.ID == m_LocalPlayer) { + //if (e.Player.ID == m_LocalPlayer) { Events::PlaySoundOnEntity ev; EntityID child = m_World->CreateEntity(m_LocalPlayer); m_World->AttachComponent(child, "Transform"); m_World->AttachComponent(child, "SoundEmitter"); ev.EmitterID = child; - ev.FilePath = "Audio/hurt/hurt3.wav"; // random between a bunch + std::uniform_int_distribution dist(1, 12); + int rand = dist(generator); + ev.FilePath = "Audio/hurt/hurt" + std::to_string(rand) + ".wav"; m_EventBroker->Publish(ev); - } + //} return false; } @@ -419,6 +425,14 @@ bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) return false; } +bool SoundSystem::OnComponentAttached(const Events::ComponentAttached & e) +{ + if (e.Component.Info.Name == "SoundEmitter") { + + } + return false; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. @@ -448,14 +462,15 @@ void SoundSystem::setGain(Source * source, float gain) alSourcef(source->ALsource, AL_GAIN, gain); } -void SoundSystem::setSoundProperties(ALuint source, ComponentWrapper* soundComponent) +void SoundSystem::setSoundProperties(Source* source, ComponentWrapper* soundComponent) { - alSourcef(source, AL_GAIN, (float)(double)(*soundComponent)["Gain"]); - alSourcef(source, AL_PITCH, (float)(double)(*soundComponent)["Pitch"]); - alSourcei(source, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); // YOLO - alSourcef(source, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]); - alSourcef(source, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]); - alSourcef(source, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]); + float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel; + 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"]); // YOLO + alSourcef(source->ALsource, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]); + alSourcef(source->ALsource, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]); + alSourcef(source->ALsource, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]); } void SoundSystem::initOpenAL() diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 515540bf..e0e7f611 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -154,12 +154,13 @@ void Game::Tick() if (m_IsClientOrServer) { m_ClientOrServer->Update(); } + m_SoundSystem->Update(dt); + // Iterate through systems and update world! m_EventBroker->Process(); m_SystemPipeline->Update(dt); debugTick(dt); m_Renderer->Update(dt); - m_SoundSystem->Update(dt); GLERROR("Game::Tick m_RenderQueueFactory->Update"); m_Renderer->Draw(*m_RenderFrame); m_RenderFrame->Clear(); From 83281fdaf14cc5ff3151841e81b60fd4d41df728 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 2 Feb 2016 13:39:11 +0100 Subject: [PATCH 07/38] Changed model in player.xml --- resources/Schema/Entities/Player.xml | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml index e81ec5aa..15198dcb 100644 --- a/resources/Schema/Entities/Player.xml +++ b/resources/Schema/Entities/Player.xml @@ -2,14 +2,15 @@ - + + - + 5 @@ -20,7 +21,8 @@ - + + @@ -30,12 +32,14 @@ + + Fonts/DroidSans.ttf,100 @@ -102,11 +106,11 @@ - Models/AssaultWeapon.mesh + Models/AssaultWeaponBlue.mesh - + @@ -141,7 +145,7 @@ Hold Pos - + 1 @@ -180,6 +184,13 @@ + + + + + + + From 88ad127d8de062991ac047afbbf48908ea4224d3 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 2 Feb 2016 14:57:03 +0100 Subject: [PATCH 08/38] plays notification on captured/lost point --- resources/Schema/Entities/aim_rays.xml | 51 ++++++++++++++++++++++++++ src/Engine/Sound/SoundSystem.cpp | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/resources/Schema/Entities/aim_rays.xml b/resources/Schema/Entities/aim_rays.xml index c488ca51..c8dac9a3 100644 --- a/resources/Schema/Entities/aim_rays.xml +++ b/resources/Schema/Entities/aim_rays.xml @@ -205,6 +205,57 @@ + + + + + + + + + + Models\Core\UnitCube.mesh + + + + + + + + + + + + + + + + + + + + + + + 1 + + + Models\Core\UnitCube.mesh + + + + + + + + + + + + + + + diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index d08232ec..e5684ac3 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -365,7 +365,7 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) bool SoundSystem::OnCaptured(const Events::Captured & e) { - int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "CapturePoint")["HomePointForTeam"]; + int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "Team")["Team"]; int team = (int)m_World->GetComponent(m_LocalPlayer, "Team")["Team"]; Events::PlaySoundOnEntity ev; if (team == homeTeam) { From a64d227cab6889e127f97d2022776176ce228d2f Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 3 Feb 2016 10:17:53 +0100 Subject: [PATCH 09/38] Assured the volume channels were working. Playing around with drumloop --- assets | 2 +- include/Engine/Sound/SoundSystem.h | 13 ++++++--- src/Engine/Sound/SoundSystem.cpp | 42 +++++++++++++++++++++++------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/assets b/assets index 5bf03ccd..cde9a430 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 5bf03ccd54bed358d525840635573f931adf584e +Subproject commit cde9a43029fe674a01ede2b74a1987cd57f1074a diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index e161af77..9e636c95 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -9,6 +9,8 @@ #include "OpenAL/al.h" #include "OpenAL/alc.h" +#include "imgui/imgui.h" + #include "Core/World.h" #include "Core/EventBroker.h" #include "Core/Transform.h" // Absolute transform @@ -29,6 +31,7 @@ #include "Core/EPlayerDeath.h" #include "Core/EPlayerHealthPickup.h" #include "Core/EComponentAttached.h" +#include "Collision/ETrigger.h" enum class SoundType { SFX, @@ -90,8 +93,11 @@ private: World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; std::unordered_map m_Sources; - float m_BGMVolumeChannel = 1.f; - float m_SFXVolumeChannel = 1.f; + + + + float m_BGMVolumeChannel = 1.0f; + float m_SFXVolumeChannel = 1.0f; bool m_EditorEnabled = false; const double m_PlayerFootstepInterval = 1.0; double m_TimeSinceLastFootstep = 0; @@ -117,7 +123,6 @@ private: bool OnSetBGMGain(const Events::SetBGMGain &e); // Not tested EventRelay m_ESetSFXGain; bool OnSetSFXGain(const Events::SetSFXGain &e); // Not tested - EventRelay m_EShoot; bool OnShoot(const Events::Shoot &e); EventRelay m_EPlayerSpawned; @@ -134,6 +139,8 @@ private: bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); EventRelay m_EComponentAttached; bool OnComponentAttached(const Events::ComponentAttached &e); + EventRelay m_ETriggerTouch; + bool OnTriggerTouch(const Events::TriggerTouch &e); diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index e5684ac3..08e5c0a8 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -25,6 +25,7 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundSystem::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); + EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundSystem::OnTriggerTouch); } SoundSystem::~SoundSystem() @@ -60,6 +61,10 @@ void SoundSystem::Update(double dt) deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" updateEmitters(dt); updateListener(dt); + + // Editor debug info + ImGui::SliderFloat("BGM", &m_BGMVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f); + ImGui::SliderFloat("SFX", &m_SFXVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f); } void SoundSystem::deleteInactiveEmitters() @@ -287,11 +292,12 @@ bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { auto emitterChild = m_World->CreateEntity((*it).EntityID); auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter"); - (bool&)emitter["Loop"] = true; + (bool&)emitter["Loop"] = false; (std::string&)emitter["FilePath"] = e.FilePath; m_World->AttachComponent(emitterChild, "Transform"); Source* source = createSource(e.FilePath); source->Type = SoundType::BGM; + setSoundProperties(source, &emitter); m_Sources[emitterChild] = source; playSound(source); } @@ -334,6 +340,12 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned & e) event.EmitterID = child; event.FilePath = "Audio/announcer/go.wav"; m_EventBroker->Publish(event); + // TEMP: starts bgm + { + Events::PlayBackgroundMusic ev; + ev.FilePath = "Audio/bgm/ambient.wav"; + m_EventBroker->Publish(ev); + } } return true; } @@ -384,15 +396,15 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) { //if (e.Player.ID == m_LocalPlayer) { - Events::PlaySoundOnEntity ev; - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - ev.EmitterID = child; - std::uniform_int_distribution dist(1, 12); - int rand = dist(generator); - ev.FilePath = "Audio/hurt/hurt" + std::to_string(rand) + ".wav"; - m_EventBroker->Publish(ev); + Events::PlaySoundOnEntity ev; + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + ev.EmitterID = child; + std::uniform_int_distribution dist(1, 12); + int rand = dist(generator); + ev.FilePath = "Audio/hurt/hurt" + std::to_string(rand) + ".wav"; + m_EventBroker->Publish(ev); //} return false; } @@ -433,6 +445,16 @@ bool SoundSystem::OnComponentAttached(const Events::ComponentAttached & e) return false; } +bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) +{ + if (m_World->HasComponent(e.Trigger.ID, "CapturePoint")) { + Events::PlayBackgroundMusic ev; + ev.FilePath = "Audio/bgm/drumstest.wav"; + m_EventBroker->Publish(ev); + } + return false; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. From 938caa6ba2809023cbfb9bd096f5f8481ae394c3 Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 3 Feb 2016 15:48:25 +0100 Subject: [PATCH 10/38] Now uses OnComponentAttached event. Can now queue sound on a source. --- assets | 2 +- include/Engine/Sound/SoundSystem.h | 17 ++++- src/Engine/Sound/SoundSystem.cpp | 101 +++++++++++++++++++---------- 3 files changed, 81 insertions(+), 39 deletions(-) diff --git a/assets b/assets index cde9a430..29f8e123 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit cde9a43029fe674a01ede2b74a1987cd57f1074a +Subproject commit 29f8e1234661f93ffba656c031d037a9ca484660 diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 9e636c95..20d58933 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -31,7 +31,13 @@ #include "Core/EPlayerDeath.h" #include "Core/EPlayerHealthPickup.h" #include "Core/EComponentAttached.h" +#include "Core/EComponentDeleted.h" +#include "Core/EEntityDeleted.h" #include "Collision/ETrigger.h" +#include "Core/EPause.h" + + +typedef std::pair> QueuedBuffers; enum class SoundType { SFX, @@ -67,7 +73,6 @@ private: // Logic void initOpenAL(); - void addNewEmitters(double dt); void updateEmitters(double dt); void deleteInactiveEmitters(); void stopEmitters(); @@ -79,6 +84,8 @@ private: // Specific logic void playSound(Source* source); + // Need to be the same format (sample rate etc) + void playQueue(QueuedBuffers qb); void stopSound(Source* source); void playerDamaged(); void playerShot(); @@ -94,8 +101,6 @@ private: EventBroker* m_EventBroker = nullptr; std::unordered_map m_Sources; - - float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; bool m_EditorEnabled = false; @@ -139,8 +144,14 @@ private: bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); EventRelay m_EComponentAttached; bool OnComponentAttached(const Events::ComponentAttached &e); + EventRelay m_EComponentDeleted; + bool OnComponentDeleted(const Events::ComponentDeleted &e); EventRelay m_ETriggerTouch; bool OnTriggerTouch(const Events::TriggerTouch &e); + EventRelay m_EPause; + bool OnPause(const Events::Pause &e); + EventRelay m_EResume; + bool OnResume(const Events::Resume &e); diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 08e5c0a8..ab4c5fce 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -26,6 +26,9 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundSystem::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundSystem::OnTriggerTouch); + EVENT_SUBSCRIBE_MEMBER(m_EPause, &SoundSystem::OnPause); + EVENT_SUBSCRIBE_MEMBER(m_EResume, &SoundSystem::OnResume); + EVENT_SUBSCRIBE_MEMBER(m_EComponentAttached, &SoundSystem::OnComponentAttached); } SoundSystem::~SoundSystem() @@ -57,7 +60,6 @@ void SoundSystem::Update(double dt) { m_EventBroker->Process(); playerStep(dt); - addNewEmitters(dt); // can be optimized with "EEntityCreated" deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" updateEmitters(dt); updateListener(dt); @@ -87,7 +89,7 @@ void SoundSystem::deleteInactiveEmitters() } } else { // Entity / Component has been removed - stopSound((*it).second); + stopSound(it->second); alDeleteBuffers(1, &it->second->ALsource); alDeleteSources(1, &it->second->ALsource); delete it->second; @@ -96,28 +98,14 @@ void SoundSystem::deleteInactiveEmitters() } } -void SoundSystem::addNewEmitters(double dt) -{ - auto emitterComponents = m_World->GetComponents("SoundEmitter"); - if (emitterComponents == nullptr) { - return; - } - for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { - EntityID emitter = (*it).EntityID; - std::unordered_map::iterator source; - source = m_Sources.find(emitter); - if (source == m_Sources.end()) { // Did not exist, add it - Source* source = createSource((std::string)(*it)["FilePath"]); - m_Sources[emitter] = source; - } - } -} - void SoundSystem::updateEmitters(double dt) { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { // Get previous pos + if (!m_World->ValidEntity(it->first)) { + return; + } glm::vec3 previousPos; alGetSource3f(it->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get next pos @@ -180,6 +168,15 @@ void SoundSystem::playSound(Source* source) alSourcePlay(source->ALsource); } +void SoundSystem::playQueue(QueuedBuffers qb) +{ + for (int i = 0; i < qb.second.size(); i++) { + alSourceQueueBuffers(qb.first, 1, &qb.second[i]); + } + alSourcePlay(qb.first); +} + + void SoundSystem::stopSound(Source* source) { alSourceStop(source->ALsource); @@ -210,9 +207,13 @@ void SoundSystem::playerJumps() void SoundSystem::playerStep(double dt) { + if (!m_World->ValidEntity(m_LocalPlayer)) { + return; + } if (m_LocalPlayer == EntityID_Invalid) { return; } + m_TimeSinceLastFootstep += dt; glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; float playerSpeed = glm::length(vel); @@ -260,8 +261,6 @@ bool SoundSystem::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) (float&)(double)emitter["MaxDistance"] = e.MaxDistance; (float&)(double)emitter["RollOffFactor"] = e.RollOffFactor; (float&)(double)emitter["ReferenceDistance"] = e.ReferenceDistance; - auto model = m_World->AttachComponent(emitterID, "Model"); - (std::string&)model["Resource"] = "Models/Core/UnitCube.mesh"; // 360NoScope UnitCube source->Type = SoundType::SFX; m_Sources[emitterID] = source; playSound(source); @@ -352,24 +351,20 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned & e) bool SoundSystem::OnInputCommand(const Events::InputCommand & e) { - if (e.Player.ID == EntityID_Invalid) { - //return false; - } if (e.Command == "Jump" && e.Value > 0) { if (e.PlayerID == -1) { // local player - //bool airBorne = ((glm::vec3)m_World->GetComponent(e.Player.ID, "Physics")["Velocity"]).y != 0; - //if (!airBorne) { playerJumps(); - //} return true; } } + // TEMP: testing purpose (obviously) if (e.Command == "TakeDamage" && e.Value > 0) { if (e.PlayerID == -1) { //Local Player Events::PlayerDamage ePlayerDamage; ePlayerDamage.Damage = 1; ePlayerDamage.Player = EntityWrapper(m_World, e.Player.ID); m_EventBroker->Publish(ePlayerDamage); + return true; } } return false; @@ -383,7 +378,7 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) if (team == homeTeam) { ev.FilePath = "Audio/announcer/objective_achieved.wav"; } else { - ev.FilePath = "Audio/announcer/objective_failed.wav"; + ev.FilePath = "Audio/announcer/objective_failed.wav"; // have not been tested } EntityID child = m_World->CreateEntity(m_LocalPlayer); m_World->AttachComponent(child, "Transform"); @@ -395,17 +390,27 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) { - //if (e.Player.ID == m_LocalPlayer) { - Events::PlaySoundOnEntity ev; + // Should check for only local players here... EntityID child = m_World->CreateEntity(m_LocalPlayer); m_World->AttachComponent(child, "Transform"); m_World->AttachComponent(child, "SoundEmitter"); - ev.EmitterID = child; + std::uniform_int_distribution dist(1, 12); int rand = dist(generator); - ev.FilePath = "Audio/hurt/hurt" + std::to_string(rand) + ".wav"; - m_EventBroker->Publish(ev); - //} + Source* source = createSource("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); + source->Type = SoundType::SFX; + m_Sources[child] = source; + //playSound(source); + + // breathe + std::vector buffers; + buffers.push_back(source->SoundResource->Buffer()); + int ammountOfbreaths = (e.Damage / 10) + 2; // TEMP: Idk something stupid like this shit + for (int i = 0; i < ammountOfbreaths; i++) { + buffers.push_back(ResourceManager::Load("Audio/exhausted/breath.wav")->Buffer()); + } + playQueue(QueuedBuffers(std::make_pair(source->ALsource, buffers))); + return false; } @@ -417,7 +422,7 @@ bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) m_World->AttachComponent(child, "Transform"); m_World->AttachComponent(child, "SoundEmitter"); ev.EmitterID = child; - ev.FilePath = "Audio/die/die2.wav"; // random between a bunch + ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch m_EventBroker->Publish(ev); } return false; @@ -440,6 +445,16 @@ bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) bool SoundSystem::OnComponentAttached(const Events::ComponentAttached & e) { if (e.Component.Info.Name == "SoundEmitter") { + auto component = m_World->GetComponent(e.Entity.ID, "SoundEmitter"); + Source* source = createSource(component["FilePath"]); + m_Sources[e.Entity.ID] = source; + } + return false; +} + +bool SoundSystem::OnComponentDeleted(const Events::ComponentDeleted & e) +{ + if (e.ComponentType == "SoundEmitter") { } return false; @@ -455,6 +470,22 @@ bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) return false; } +bool SoundSystem::OnPause(const Events::Pause & e) +{ + for (auto it = m_Sources.begin(); it != m_Sources.end(); it++) { + alSourcePause(it->second->ALsource); + } + return false; +} + +bool SoundSystem::OnResume(const Events::Resume &e) +{ + for (auto it = m_Sources.begin(); it != m_Sources.end(); it++) { + alSourcePlay(it->second->ALsource); + } + return false; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. From c2833b10c40d9ade38f5548ae97e9323bb948010 Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 3 Feb 2016 15:55:32 +0100 Subject: [PATCH 11/38] Hard coded (read retarded) improvements added to player walk logic --- src/Engine/Sound/SoundSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index ab4c5fce..86f3a34b 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -220,7 +220,7 @@ void SoundSystem::playerStep(double dt) bool isAirborne = vel.y != 0; if (playerSpeed > 1 && !isAirborne) { // Player is walking - if (m_TimeSinceLastFootstep * playerSpeed > m_PlayerFootstepInterval) { + if (m_TimeSinceLastFootstep * std::min(playerSpeed, 2) > m_PlayerFootstepInterval) { // Create footstep sound EntityID child = m_World->CreateEntity(m_LocalPlayer); m_World->AttachComponent(child, "Transform"); From 0a8ee1623fae4cec72b1973c52f446f12e4a48f4 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 4 Feb 2016 10:47:30 +0100 Subject: [PATCH 12/38] Merge remote-tracking branch 'origin/master' into Sound # Conflicts: # assets --- assets | 2 +- include/Engine/Sound/SoundSystem.h | 3 +++ include/Game/Events/EDoubleJump.h | 16 ++++++++++++++++ include/Game/Systems/PlayerMovementSystem.h | 1 + src/Engine/Sound/SoundSystem.cpp | 14 +++++++++++++- src/Game/Systems/PlayerMovementSystem.cpp | 2 ++ 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 include/Game/Events/EDoubleJump.h diff --git a/assets b/assets index 29f8e123..a7387630 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 29f8e1234661f93ffba656c031d037a9ca484660 +Subproject commit a73876309264a0751e3a46f15020dd46d5a67f98 diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 20d58933..597aca22 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -35,6 +35,7 @@ #include "Core/EEntityDeleted.h" #include "Collision/ETrigger.h" #include "Core/EPause.h" +#include "Game/Events/EDoubleJump.h" typedef std::pair> QueuedBuffers; @@ -152,6 +153,8 @@ private: bool OnPause(const Events::Pause &e); EventRelay m_EResume; bool OnResume(const Events::Resume &e); + EventRelay m_EDoubleJump; + bool OnDoubleJump(const Events::DoubleJump &e); diff --git a/include/Game/Events/EDoubleJump.h b/include/Game/Events/EDoubleJump.h new file mode 100644 index 00000000..767d5b39 --- /dev/null +++ b/include/Game/Events/EDoubleJump.h @@ -0,0 +1,16 @@ +#ifndef Events_DoubleJump_h__ +#define Events_DoubleJump_h__ + +#include "Core/Event.h" + +namespace Events +{ + +struct DoubleJump : public Event +{ + +}; + +} + +#endif diff --git a/include/Game/Systems/PlayerMovementSystem.h b/include/Game/Systems/PlayerMovementSystem.h index f39740ec..4d98990a 100644 --- a/include/Game/Systems/PlayerMovementSystem.h +++ b/include/Game/Systems/PlayerMovementSystem.h @@ -4,6 +4,7 @@ #include "Core/EPlayerSpawned.h" #include "Input/FirstPersonInputController.h" #include +#include "Events/EDoubleJump.h" class PlayerMovementSystem : public ImpureSystem, PureSystem { diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 92ea5142..847230d9 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -29,6 +29,7 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode EVENT_SUBSCRIBE_MEMBER(m_EPause, &SoundSystem::OnPause); EVENT_SUBSCRIBE_MEMBER(m_EResume, &SoundSystem::OnResume); EVENT_SUBSCRIBE_MEMBER(m_EComponentAttached, &SoundSystem::OnComponentAttached); + EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &SoundSystem::OnDoubleJump); } SoundSystem::~SoundSystem() @@ -400,7 +401,6 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) Source* source = createSource("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); source->Type = SoundType::SFX; m_Sources[child] = source; - //playSound(source); // breathe std::vector buffers; @@ -486,6 +486,18 @@ bool SoundSystem::OnResume(const Events::Resume &e) return false; } +bool SoundSystem::OnDoubleJump(const Events::DoubleJump & e) +{ + Events::PlaySoundOnEntity ev; + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + ev.EmitterID = child; + ev.FilePath = "Audio/jump/jump2.wav"; + m_EventBroker->Publish(ev); + return false; +} + void SoundSystem::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index 72900d7a..498c3765 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -82,6 +82,8 @@ void PlayerMovementSystem::Update(double dt) } else { controller->SetDoubleJumping(true); + Events::DoubleJump e; + m_EventBroker->Publish(e); } velocity.y += 4.f; } From d50caa176321a70a53348be7e8ca01731fc16002 Mon Sep 17 00:00:00 2001 From: stiffly Date: Thu, 4 Feb 2016 16:14:50 +0100 Subject: [PATCH 13/38] Gain events tested. works as intended --- include/Engine/Sound/SoundSystem.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 597aca22..d0b1a3dd 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -126,9 +126,9 @@ private: EventRelay m_EContinueSound; bool OnContinueSound(const Events::ContinueSound &e); EventRelay m_ESetBGMGain; - bool OnSetBGMGain(const Events::SetBGMGain &e); // Not tested + bool OnSetBGMGain(const Events::SetBGMGain &e); EventRelay m_ESetSFXGain; - bool OnSetSFXGain(const Events::SetSFXGain &e); // Not tested + bool OnSetSFXGain(const Events::SetSFXGain &e); EventRelay m_EShoot; bool OnShoot(const Events::Shoot &e); EventRelay m_EPlayerSpawned; From 71a86cab114f0cdcb9c6f1805dc79c9cce88b596 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 10:07:56 +0100 Subject: [PATCH 14/38] removed event that's not being used --- include/Engine/Sound/SoundSystem.h | 5 ----- src/Engine/Sound/SoundSystem.cpp | 11 +---------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index d0b1a3dd..51f80427 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -31,8 +31,6 @@ #include "Core/EPlayerDeath.h" #include "Core/EPlayerHealthPickup.h" #include "Core/EComponentAttached.h" -#include "Core/EComponentDeleted.h" -#include "Core/EEntityDeleted.h" #include "Collision/ETrigger.h" #include "Core/EPause.h" #include "Game/Events/EDoubleJump.h" @@ -107,7 +105,6 @@ private: bool m_EditorEnabled = false; const double m_PlayerFootstepInterval = 1.0; double m_TimeSinceLastFootstep = 0; - // TEMP EntityID m_LocalPlayer = EntityID_Invalid; bool m_LeftFoot = false; std::default_random_engine generator; @@ -145,8 +142,6 @@ private: bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); EventRelay m_EComponentAttached; bool OnComponentAttached(const Events::ComponentAttached &e); - EventRelay m_EComponentDeleted; - bool OnComponentDeleted(const Events::ComponentDeleted &e); EventRelay m_ETriggerTouch; bool OnTriggerTouch(const Events::TriggerTouch &e); EventRelay m_EPause; diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 847230d9..3045b1f4 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -177,7 +177,6 @@ void SoundSystem::playQueue(QueuedBuffers qb) alSourcePlay(qb.first); } - void SoundSystem::stopSound(Source* source) { alSourceStop(source->ALsource); @@ -402,7 +401,7 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) source->Type = SoundType::SFX; m_Sources[child] = source; - // breathe + // Breathe std::vector buffers; buffers.push_back(source->SoundResource->Buffer()); int ammountOfbreaths = (static_cast(e.Damage) / 10) + 2; // TEMP: Idk something stupid like this shit @@ -452,14 +451,6 @@ bool SoundSystem::OnComponentAttached(const Events::ComponentAttached & e) return false; } -bool SoundSystem::OnComponentDeleted(const Events::ComponentDeleted & e) -{ - if (e.ComponentType == "SoundEmitter") { - - } - return false; -} - bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) { if (m_World->HasComponent(e.Trigger.ID, "CapturePoint")) { From 3e2afd7bf29773d6a8d3ff503321a1ad25d7d5d7 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 16:37:56 +0100 Subject: [PATCH 15/38] Added dash sound --- assets | 2 +- include/Game/Events/EDashAbility.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 include/Game/Events/EDashAbility.h diff --git a/assets b/assets index a7387630..cfeeb1f8 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit a73876309264a0751e3a46f15020dd46d5a67f98 +Subproject commit cfeeb1f880e19356477a703da92204eede09db5d diff --git a/include/Game/Events/EDashAbility.h b/include/Game/Events/EDashAbility.h new file mode 100644 index 00000000..62a2b935 --- /dev/null +++ b/include/Game/Events/EDashAbility.h @@ -0,0 +1,13 @@ +#ifndef Events_DashAbility_h__ +#define Events_DashAbility_h__ + +#include "Core/Event.h" + +namespace Events +{ + +struct DashAbility : public Event { }; + +} + +#endif \ No newline at end of file From fbdaf92953569db374bbd96dccafaa1e036b5869 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 16:39:23 +0100 Subject: [PATCH 16/38] Started refactoring. Put player walk logic in a designated sound system. --- .../Engine/Input/FirstPersonInputController.h | 4 + .../Sound/{SoundSystem.h => SoundManager.h} | 89 +++--- include/Game/Game.h | 5 +- include/Game/Systems/SoundSystem.h | 31 ++ .../{SoundSystem.cpp => SoundManager.cpp} | 287 +++++++----------- src/Game/Game.cpp | 8 +- src/Game/Systems/SoundSystem.cpp | 73 +++++ 7 files changed, 281 insertions(+), 216 deletions(-) rename include/Engine/Sound/{SoundSystem.h => SoundManager.h} (71%) create mode 100644 include/Game/Systems/SoundSystem.h rename src/Engine/Sound/{SoundSystem.cpp => SoundManager.cpp} (65%) create mode 100644 src/Game/Systems/SoundSystem.cpp diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h index 2bbd768d..f3b0a17a 100644 --- a/include/Engine/Input/FirstPersonInputController.h +++ b/include/Engine/Input/FirstPersonInputController.h @@ -4,6 +4,7 @@ #include "../GLM.h" #include "../Core/InputController.h" #include "../Core/ELockMouse.h" +#include "../Game/Events/EDashAbility.h" #include "InputHandler.h" template @@ -230,6 +231,9 @@ void FirstPersonInputController::AssaultDashCheck(double dt, bool m_AssaultDashDoubleTapped = true; m_AssaultDashDoubleTapDeltaTime = 0.f; m_AssaultDashCoolDownTimer = assaultDashCoolDownMaxTimer; + + Events::DashAbility e; + m_EventBroker->Publish(e); } #endif \ No newline at end of file diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundManager.h similarity index 71% rename from include/Engine/Sound/SoundSystem.h rename to include/Engine/Sound/SoundManager.h index 51f80427..a9fb23a9 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundManager.h @@ -1,5 +1,5 @@ -#ifndef SoundSystem_h__ -#define SoundSystem_h__ +#ifndef SoundManager_h__ +#define SoundManager_h__ #include #include @@ -34,6 +34,7 @@ #include "Collision/ETrigger.h" #include "Core/EPause.h" #include "Game/Events/EDoubleJump.h" +#include "Game/Events/EDashAbility.h" typedef std::pair> QueuedBuffers; @@ -51,14 +52,30 @@ struct Source SoundType Type; }; -class SoundSystem +class SoundManager { public: - SoundSystem() { } - SoundSystem(World* world, EventBroker* eventBroker, bool editorMode); - ~SoundSystem(); + SoundManager() { } + SoundManager(World* world, EventBroker* eventBroker, bool editorMode); + ~SoundManager(); // Update emitters / listener void Update(double dt); + +protected: + // Specific logic + void playSound(Source* source); + // Need to be the same format (sample rate etc) + void playQueue(QueuedBuffers qb); + void stopSound(Source* source); + void playerJumps(); + void playerStep(double dt); + EntityID createChildEmitter(); + + // Logic + World* m_World = nullptr; + EventBroker* m_EventBroker = nullptr; + std::unordered_map m_Sources; + private: // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; @@ -81,75 +98,63 @@ private: void setGain(Source* source, float gain); void setSoundProperties(Source* source, ComponentWrapper* soundComponent); - // Specific logic - void playSound(Source* source); - // Need to be the same format (sample rate etc) - void playQueue(QueuedBuffers qb); - void stopSound(Source* source); - void playerDamaged(); - void playerShot(); - void playerJumps(); - void playerStep(double dt); + // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; ALCcontext* m_ALCcontext = nullptr; - // Logic - World* m_World = nullptr; - EventBroker* m_EventBroker = nullptr; - std::unordered_map m_Sources; + float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; bool m_EditorEnabled = false; - const double m_PlayerFootstepInterval = 1.0; - double m_TimeSinceLastFootstep = 0; EntityID m_LocalPlayer = EntityID_Invalid; - bool m_LeftFoot = false; std::default_random_engine generator; // Events - EventRelay m_EPlaySoundOnEntity; + EventRelay m_EPlaySoundOnEntity; bool OnPlaySoundOnEntity(const Events::PlaySoundOnEntity &e); - EventRelay m_EPlaySoundOnPosition; + EventRelay m_EPlaySoundOnPosition; bool OnPlaySoundOnPosition(const Events::PlaySoundOnPosition &e); - EventRelay m_EPlayBackgroundMusic; + EventRelay m_EPlayBackgroundMusic; bool OnPlayBackgroundMusic(const Events::PlayBackgroundMusic &e); - EventRelay m_EPauseSound; + EventRelay m_EPauseSound; bool OnPauseSound(const Events::PauseSound &e); - EventRelay m_EStopSound; + EventRelay m_EStopSound; bool OnStopSound(const Events::StopSound &e); - EventRelay m_EContinueSound; + EventRelay m_EContinueSound; bool OnContinueSound(const Events::ContinueSound &e); - EventRelay m_ESetBGMGain; + EventRelay m_ESetBGMGain; bool OnSetBGMGain(const Events::SetBGMGain &e); - EventRelay m_ESetSFXGain; + EventRelay m_ESetSFXGain; bool OnSetSFXGain(const Events::SetSFXGain &e); - EventRelay m_EShoot; + EventRelay m_EShoot; bool OnShoot(const Events::Shoot &e); - EventRelay m_EPlayerSpawned; + EventRelay m_EPlayerSpawned; bool OnPlayerSpawned(const Events::PlayerSpawned &e); - EventRelay m_EInputCommand; + EventRelay m_EInputCommand; bool OnInputCommand(const Events::InputCommand &e); - EventRelay m_ECaptured; + EventRelay m_ECaptured; bool OnCaptured(const Events::Captured &e); - EventRelay m_EPlayerDamage; + EventRelay m_EPlayerDamage; bool OnPlayerDamage(const Events::PlayerDamage &e); - EventRelay m_EPlayerDeath; + EventRelay m_EPlayerDeath; bool OnPlayerDeath(const Events::PlayerDeath &e); - EventRelay m_EPlayerHealthPickup; + EventRelay m_EPlayerHealthPickup; bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); - EventRelay m_EComponentAttached; + EventRelay m_EComponentAttached; bool OnComponentAttached(const Events::ComponentAttached &e); - EventRelay m_ETriggerTouch; + EventRelay m_ETriggerTouch; bool OnTriggerTouch(const Events::TriggerTouch &e); - EventRelay m_EPause; + EventRelay m_EPause; bool OnPause(const Events::Pause &e); - EventRelay m_EResume; + EventRelay m_EResume; bool OnResume(const Events::Resume &e); - EventRelay m_EDoubleJump; + EventRelay m_EDoubleJump; bool OnDoubleJump(const Events::DoubleJump &e); + EventRelay m_EDashAbility; + bool OnDashAbility(const Events::DashAbility &e); diff --git a/include/Game/Game.h b/include/Game/Game.h index 37267a68..b6a2c0d3 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -30,7 +30,8 @@ #include "Network/Client.h" // Sound -#include "Sound/SoundSystem.h" +//#include "Sound/SoundManager.h" +#include "Systems/SoundSystem.h" class Game { @@ -64,7 +65,7 @@ private: bool m_IsClientOrServer = false; // Sound - SoundSystem* m_SoundSystem; + SoundManager* m_SoundManager; //EventRelay m_EInputCommand; //bool debugOnInputCommand(const Events::InputCommand& e); diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h new file mode 100644 index 00000000..33dbf1fc --- /dev/null +++ b/include/Game/Systems/SoundSystem.h @@ -0,0 +1,31 @@ +#ifndef Systems_SoundSystem_h__ +#define Systems_SoundSystem_h__ + +#include "../Engine/Core/System.h" +#include "../Engine/Sound/SoundManager.h" +#include "../Engine/Core/EPlayerSpawned.h" + + +class SoundSystem : public PureSystem, ImpureSystem, SoundManager +{ +public: + SoundSystem(World* world, EventBroker* eventbroker); + virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) override; + virtual void Update(double dt) override; +private: + void playerStep(double dt); + EntityWrapper m_LocalPlayer = EntityWrapper(); + + World* m_World = nullptr; + EventBroker* m_EventBroker = nullptr; + + // TODO: WIP Update this + double m_TimeSinceLastFootstep = 0.0; + const double m_PlayerFootstepInterval = 1.0; + bool m_LeftFoot = false; + + EventRelay m_EPlayerSpawned; + bool OnPlayerSpawned(const Events::PlayerSpawned &e); +}; + +#endif diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundManager.cpp similarity index 65% rename from src/Engine/Sound/SoundSystem.cpp rename to src/Engine/Sound/SoundManager.cpp index 3045b1f4..3cec4deb 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -1,6 +1,6 @@ -#include "Sound/SoundSystem.h" +#include "Sound/SoundManager.h" -SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode) +SoundManager::SoundManager(World* world, EventBroker* eventBroker, bool editorMode) { m_EventBroker = eventBroker; m_World = world; @@ -12,27 +12,28 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode alDistanceModel(AL_LINEAR_DISTANCE); alDopplerFactor(1); - EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnEntity, &SoundSystem::OnPlaySoundOnEntity); - EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnPosition, &SoundSystem::OnPlaySoundOnPosition); - EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundSystem::OnPlayBackgroundMusic); - EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); - EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundSystem::OnPauseSound); - EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound); - EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundSystem::OnSetBGMGain); - EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain); - EVENT_SUBSCRIBE_MEMBER(m_EShoot, &SoundSystem::OnShoot); - EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); - EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage); - EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundSystem::OnInputCommand); - EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); - EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundSystem::OnTriggerTouch); - EVENT_SUBSCRIBE_MEMBER(m_EPause, &SoundSystem::OnPause); - EVENT_SUBSCRIBE_MEMBER(m_EResume, &SoundSystem::OnResume); - EVENT_SUBSCRIBE_MEMBER(m_EComponentAttached, &SoundSystem::OnComponentAttached); - EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &SoundSystem::OnDoubleJump); + 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_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_EShoot, &SoundManager::OnShoot); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundManager::OnPlayerDamage); + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundManager::OnInputCommand); + EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundManager::OnCaptured); + EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundManager::OnTriggerTouch); + 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_EDoubleJump, &SoundManager::OnDoubleJump); + EVENT_SUBSCRIBE_MEMBER(m_EDashAbility, &SoundManager::OnDashAbility); } -SoundSystem::~SoundSystem() +SoundManager::~SoundManager() { stopEmitters(); // Stopps emitters deleteInactiveEmitters(); // Deletes stopped emitters @@ -47,7 +48,7 @@ SoundSystem::~SoundSystem() alcCloseDevice(m_ALCdevice); } -void SoundSystem::stopEmitters() +void SoundManager::stopEmitters() { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { @@ -57,10 +58,9 @@ void SoundSystem::stopEmitters() } } -void SoundSystem::Update(double dt) +void SoundManager::Update(double dt) { - m_EventBroker->Process(); - playerStep(dt); + m_EventBroker->Process(); deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" updateEmitters(dt); updateListener(dt); @@ -70,7 +70,7 @@ void SoundSystem::Update(double dt) ImGui::SliderFloat("SFX", &m_SFXVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f); } -void SoundSystem::deleteInactiveEmitters() +void SoundManager::deleteInactiveEmitters() { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end();) { @@ -99,7 +99,7 @@ void SoundSystem::deleteInactiveEmitters() } } -void SoundSystem::updateEmitters(double dt) +void SoundManager::updateEmitters(double dt) { std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end(); it++) { @@ -132,7 +132,7 @@ void SoundSystem::updateEmitters(double dt) } } -void SoundSystem::updateListener(double dt) +void SoundManager::updateListener(double dt) { // Should only be one listener. auto listenerComponents = m_World->GetComponents("Listener"); @@ -151,7 +151,7 @@ void SoundSystem::updateListener(double dt) } } -Source* SoundSystem::createSource(std::string filePath) +Source* SoundManager::createSource(std::string filePath) { ALuint alSource; alGenSources((ALuint)1, &alSource); @@ -163,13 +163,13 @@ Source* SoundSystem::createSource(std::string filePath) return source; } -void SoundSystem::playSound(Source* source) +void SoundManager::playSound(Source* source) { alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); alSourcePlay(source->ALsource); } -void SoundSystem::playQueue(QueuedBuffers qb) +void SoundManager::playQueue(QueuedBuffers qb) { for (int i = 0; i < qb.second.size(); i++) { alSourceQueueBuffers(qb.first, 1, &qb.second[i]); @@ -177,69 +177,37 @@ void SoundSystem::playQueue(QueuedBuffers qb) alSourcePlay(qb.first); } -void SoundSystem::stopSound(Source* source) +void SoundManager::stopSound(Source* source) { alSourceStop(source->ALsource); } -void SoundSystem::playerDamaged() -{ - -} - -void SoundSystem::playerShot() -{ } - -void SoundSystem::playerJumps() +void SoundManager::playerJumps() { glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; if (vel.y == 0) { Source* source = createSource("Audio/jump/jump1.wav"); - auto emitterID = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(emitterID, "Transform"); - m_World->AttachComponent(emitterID, "SoundEmitter"); source->Type = SoundType::SFX; - m_Sources[emitterID] = source; + m_Sources[createChildEmitter()] = source; playSound(source); } } -void SoundSystem::playerStep(double dt) +void SoundManager::playerStep(double dt) { - if (!m_World->ValidEntity(m_LocalPlayer)) { - return; - } - if (m_LocalPlayer == EntityID_Invalid) { - return; - } - - m_TimeSinceLastFootstep += dt; - glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; - float playerSpeed = glm::length(vel); - bool isAirborne = vel.y != 0; - if (playerSpeed > 1 && !isAirborne) { - // Player is walking - if (m_TimeSinceLastFootstep * std::min(playerSpeed, 2) > m_PlayerFootstepInterval) { - // Create footstep sound - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - Events::PlaySoundOnEntity e; - e.EmitterID = child; - if (m_LeftFoot) { - e.FilePath = "Audio/footstep/footstep2.wav"; - } else { - e.FilePath = "Audio/footstep/footstep3.wav"; - } - m_LeftFoot = !m_LeftFoot; - m_EventBroker->Publish(e); - m_TimeSinceLastFootstep = 0; - } - } + } -bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) +EntityID SoundManager::createChildEmitter() +{ + EntityID child = m_World->CreateEntity(m_LocalPlayer); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + return child; +} + +bool SoundManager::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); source->Type = SoundType::SFX; @@ -248,7 +216,7 @@ bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) return false; } -bool SoundSystem::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) +bool SoundManager::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) { Source* source = createSource(e.FilePath); auto emitterID = m_World->CreateEntity(); @@ -267,25 +235,25 @@ bool SoundSystem::OnPlaySoundOnPosition(const Events::PlaySoundOnPosition & e) return true; } -bool SoundSystem::OnPauseSound(const Events::PauseSound & e) +bool SoundManager::OnPauseSound(const Events::PauseSound & e) { alSourcePause(m_Sources[e.EmitterID]->ALsource); return true; } -bool SoundSystem::OnStopSound(const Events::StopSound & e) +bool SoundManager::OnStopSound(const Events::StopSound & e) { alSourceStop(m_Sources[e.EmitterID]->ALsource); return true; } -bool SoundSystem::OnContinueSound(const Events::ContinueSound & e) +bool SoundManager::OnContinueSound(const Events::ContinueSound & e) { alSourcePlay(m_Sources[e.EmitterID]->ALsource); return true; } -bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) +bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) { auto listenerComponents = m_World->GetComponents("Listener"); for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { @@ -303,53 +271,40 @@ bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) return true; } -bool SoundSystem::OnSetBGMGain(const Events::SetBGMGain & e) +bool SoundManager::OnSetBGMGain(const Events::SetBGMGain & e) { m_BGMVolumeChannel = e.Gain; return true; } -bool SoundSystem::OnSetSFXGain(const Events::SetSFXGain & e) +bool SoundManager::OnSetSFXGain(const Events::SetSFXGain & e) { m_SFXVolumeChannel = e.Gain; return true; } -bool SoundSystem::OnShoot(const Events::Shoot & e) +bool SoundManager::OnShoot(const Events::Shoot & e) { Source* source = createSource("Audio/laser/laser1.wav"); - auto emitterID = m_World->CreateEntity(e.Player.ID); - m_World->AttachComponent(emitterID, "Transform"); - auto emitter = m_World->AttachComponent(emitterID, "SoundEmitter"); + //auto emitterID = m_World->CreateEntity(e.Player.ID); + //m_World->AttachComponent(emitterID, "Transform"); + //auto emitter = m_World->AttachComponent(emitterID, "SoundEmitter"); source->Type = SoundType::SFX; - m_Sources[emitterID] = source; + m_Sources[createChildEmitter()] = source; playSound(source); return true; } -bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned & e) +bool SoundManager::OnPlayerSpawned(const Events::PlayerSpawned & e) { if (e.PlayerID == -1) { // Local player m_World->AttachComponent(e.Player.ID, "Listener"); m_LocalPlayer = e.Player.ID; - EntityID child = m_World->CreateEntity(e.Player.ID); - m_World->AttachComponent(child, "SoundEmitter"); // Temp - m_World->AttachComponent(child, "Transform"); // Temp - Events::PlaySoundOnEntity event; - event.EmitterID = child; - event.FilePath = "Audio/announcer/go.wav"; - m_EventBroker->Publish(event); - // TEMP: starts bgm - { - Events::PlayBackgroundMusic ev; - ev.FilePath = "Audio/bgm/ambient.wav"; - m_EventBroker->Publish(ev); - } } return true; } -bool SoundSystem::OnInputCommand(const Events::InputCommand & e) +bool SoundManager::OnInputCommand(const Events::InputCommand & e) { if (e.Command == "Jump" && e.Value > 0) { if (e.PlayerID == -1) { // local player @@ -370,7 +325,7 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) return false; } -bool SoundSystem::OnCaptured(const Events::Captured & e) +bool SoundManager::OnCaptured(const Events::Captured & e) { int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "Team")["Team"]; int team = (int)m_World->GetComponent(m_LocalPlayer, "Team")["Team"]; @@ -380,26 +335,20 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) } else { ev.FilePath = "Audio/announcer/objective_failed.wav"; // have not been tested } - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - ev.EmitterID = child; + ev.EmitterID = createChildEmitter(); m_EventBroker->Publish(ev); return false; } -bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) +bool SoundManager::OnPlayerDamage(const Events::PlayerDamage & e) { // Should check for only local players here... - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); std::uniform_int_distribution dist(1, 12); int rand = dist(generator); Source* source = createSource("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); source->Type = SoundType::SFX; - m_Sources[child] = source; + m_Sources[createChildEmitter()] = source; // Breathe std::vector buffers; @@ -413,35 +362,29 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) return false; } -bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) +bool SoundManager::OnPlayerDeath(const Events::PlayerDeath & e) { if (e.PlayerID == m_LocalPlayer) { Events::PlaySoundOnEntity ev; - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - ev.EmitterID = child; + ev.EmitterID = createChildEmitter(); ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch m_EventBroker->Publish(ev); } return false; } -bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) +bool SoundManager::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) { if (e.PlayerHealedID == m_LocalPlayer) { Events::PlaySoundOnEntity ev; - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - ev.EmitterID = child; + ev.EmitterID = createChildEmitter(); ev.FilePath = "Audio/pickup/pickup2.wav"; m_EventBroker->Publish(ev); } return false; } -bool SoundSystem::OnComponentAttached(const Events::ComponentAttached & e) +bool SoundManager::OnComponentAttached(const Events::ComponentAttached & e) { if (e.Component.Info.Name == "SoundEmitter") { auto component = m_World->GetComponent(e.Entity.ID, "SoundEmitter"); @@ -451,7 +394,7 @@ bool SoundSystem::OnComponentAttached(const Events::ComponentAttached & e) return false; } -bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) +bool SoundManager::OnTriggerTouch(const Events::TriggerTouch & e) { if (m_World->HasComponent(e.Trigger.ID, "CapturePoint")) { Events::PlayBackgroundMusic ev; @@ -461,7 +404,7 @@ bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) return false; } -bool SoundSystem::OnPause(const Events::Pause & e) +bool SoundManager::OnPause(const Events::Pause & e) { for (auto it = m_Sources.begin(); it != m_Sources.end(); it++) { alSourcePause(it->second->ALsource); @@ -469,7 +412,7 @@ bool SoundSystem::OnPause(const Events::Pause & e) return false; } -bool SoundSystem::OnResume(const Events::Resume &e) +bool SoundManager::OnResume(const Events::Resume &e) { for (auto it = m_Sources.begin(); it != m_Sources.end(); it++) { alSourcePlay(it->second->ALsource); @@ -477,19 +420,60 @@ bool SoundSystem::OnResume(const Events::Resume &e) return false; } -bool SoundSystem::OnDoubleJump(const Events::DoubleJump & e) +bool SoundManager::OnDoubleJump(const Events::DoubleJump & e) { Events::PlaySoundOnEntity ev; - EntityID child = m_World->CreateEntity(m_LocalPlayer); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - ev.EmitterID = child; + ev.EmitterID = createChildEmitter(); ev.FilePath = "Audio/jump/jump2.wav"; m_EventBroker->Publish(ev); return false; } -void SoundSystem::setListenerOri(glm::vec3 ori) +bool SoundManager::OnDashAbility(const Events::DashAbility &e) +{ + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(); + ev.FilePath = "Audio/jump/dash1.wav"; + m_EventBroker->Publish(ev); + return false; +} + +ALenum SoundManager::getSourceState(ALuint source) +{ + ALenum state; + alGetSourcei(source, AL_SOURCE_STATE, &state); + return state; +} + +void SoundManager::setGain(Source * source, float gain) +{ + alSourcef(source->ALsource, AL_GAIN, gain); +} + +void SoundManager::setSoundProperties(Source* source, ComponentWrapper* soundComponent) +{ + float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel; + 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"]); // YOLO + alSourcef(source->ALsource, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]); + alSourcef(source->ALsource, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]); + alSourcef(source->ALsource, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]); +} + +void SoundManager::initOpenAL() +{ + // Initialize OpenAL + m_ALCdevice = alcOpenDevice(nullptr); + if (m_ALCdevice != nullptr) { + m_ALCcontext = alcCreateContext(m_ALCdevice, nullptr); + alcMakeContextCurrent(m_ALCcontext); + } else { + LOG_ERROR("OpenAL failed to initialize."); + } +} + +void SoundManager::setListenerOri(glm::vec3 ori) { // Calculate forward and up vector. glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); @@ -504,39 +488,4 @@ void SoundSystem::setListenerOri(glm::vec3 ori) glm::normalize(up); ALfloat lOri[6] = { forward.x, forward.y, forward.z, up.x, up.y, up.z }; alListenerfv(AL_ORIENTATION, lOri); -} - -ALenum SoundSystem::getSourceState(ALuint source) -{ - ALenum state; - alGetSourcei(source, AL_SOURCE_STATE, &state); - return state; -} - -void SoundSystem::setGain(Source * source, float gain) -{ - alSourcef(source->ALsource, AL_GAIN, gain); -} - -void SoundSystem::setSoundProperties(Source* source, ComponentWrapper* soundComponent) -{ - float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel; - 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"]); // YOLO - alSourcef(source->ALsource, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]); - alSourcef(source->ALsource, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]); - alSourcef(source->ALsource, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]); -} - -void SoundSystem::initOpenAL() -{ - // Initialize OpenAL - m_ALCdevice = alcOpenDevice(nullptr); - if (m_ALCdevice != nullptr) { - m_ALCcontext = alcCreateContext(m_ALCdevice, nullptr); - alcMakeContextCurrent(m_ALCcontext); - } else { - LOG_ERROR("OpenAL failed to initialize."); - } } \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 04542f11..204f425e 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -81,6 +81,7 @@ Game::Game(int argc, char* argv[]) // All systems with orderlevel 0 will be updated first. unsigned int updateOrderLevel = 0; + m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); @@ -91,6 +92,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); // Populate Octree with collidables ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision, "Collidable"); @@ -114,7 +116,7 @@ Game::Game(int argc, char* argv[]) } // Invoke sound system - m_SoundSystem = new SoundSystem(m_World, m_EventBroker, m_Config->Get("Debug.EditorEnabled", false)); + //m_SoundManager = new SoundManager(m_World, m_EventBroker, m_Config->Get("Debug.EditorEnabled", false)); m_LastTime = glfwGetTime(); } @@ -122,7 +124,7 @@ Game::Game(int argc, char* argv[]) Game::~Game() { delete m_SystemPipeline; - delete m_SoundSystem; + //delete m_SoundManager; delete m_OctreeFrustrumCulling; delete m_OctreeCollision; delete m_OctreeTrigger; @@ -157,7 +159,7 @@ void Game::Tick() if (m_IsClientOrServer) { m_ClientOrServer->Update(); } - m_SoundSystem->Update(dt); + //m_SoundManager->Update(dt); // Iterate through systems and update world! m_EventBroker->Process(); diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp new file mode 100644 index 00000000..d51996ac --- /dev/null +++ b/src/Game/Systems/SoundSystem.cpp @@ -0,0 +1,73 @@ +#include "Game/Systems/SoundSystem.h" + +SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) + : System(world, eventbroker) + , PureSystem("SoundEmitter") + , ImpureSystem() + , SoundManager(world, eventbroker, true) +{ + m_World = world; + m_EventBroker = eventbroker; + EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); +} + +void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) +{ + +} + +void SoundSystem::Update(double dt) +{ + SoundManager::Update(dt); + playerStep(dt); +} + +void SoundSystem::playerStep(double dt) +{ + if (!m_LocalPlayer.Valid()) { + return; + } + if (m_LocalPlayer.ID == EntityID_Invalid) { + return; + } + + m_TimeSinceLastFootstep += dt; + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; + float playerSpeed = glm::length(vel); + bool isAirborne = vel.y != 0; + if (playerSpeed > 1 && !isAirborne) { + // Player is walking + if (m_TimeSinceLastFootstep * std::min(playerSpeed, 2) > m_PlayerFootstepInterval) { + // Create footstep sound + Events::PlaySoundOnEntity e; + e.EmitterID = createChildEmitter(); + if (m_LeftFoot) { + e.FilePath = "Audio/footstep/footstep2.wav"; + } else { + e.FilePath = "Audio/footstep/footstep3.wav"; + } + m_LeftFoot = !m_LeftFoot; + m_EventBroker->Publish(e); + m_TimeSinceLastFootstep = 0; + } + } +} + +bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) +{ + if (e.PlayerID == -1) { // Local player + m_World->AttachComponent(e.Player.ID, "Listener"); + m_LocalPlayer = e.Player; + Events::PlaySoundOnEntity event; + event.EmitterID = createChildEmitter(); + event.FilePath = "Audio/announcer/go.wav"; + m_EventBroker->Publish(event); + // TEMP: starts bgm +// { +// Events::PlayBackgroundMusic ev; +// ev.FilePath = "Audio/bgm/ambient.wav"; +// m_EventBroker->Publish(ev); +// } + } + return true; +} From febbadbd013415648464602c10d5925c3499f36c Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 17:21:31 +0100 Subject: [PATCH 17/38] Accidentally added the sound system twice. Lead to funny behavior. --- src/Game/Game.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 204f425e..541aa001 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -92,7 +92,6 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); - m_SystemPipeline->AddSystem(updateOrderLevel); // Populate Octree with collidables ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision, "Collidable"); @@ -115,16 +114,12 @@ Game::Game(int argc, char* argv[]) networkFunction(); } - // Invoke sound system - //m_SoundManager = new SoundManager(m_World, m_EventBroker, m_Config->Get("Debug.EditorEnabled", false)); - m_LastTime = glfwGetTime(); } Game::~Game() { delete m_SystemPipeline; - //delete m_SoundManager; delete m_OctreeFrustrumCulling; delete m_OctreeCollision; delete m_OctreeTrigger; From 454eef82257c28b1f657d6a9738df09323f8711e Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 17:21:48 +0100 Subject: [PATCH 18/38] WIP. Improving player walk logic. --- include/Engine/Sound/SoundManager.h | 8 ++--- include/Game/Systems/LifetimeSystem.h | 4 ++- include/Game/Systems/SoundSystem.h | 5 +-- src/Engine/Sound/SoundManager.cpp | 31 ++++++++-------- src/Game/Systems/SoundSystem.cpp | 51 ++++++++++++--------------- 5 files changed, 47 insertions(+), 52 deletions(-) diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index a9fb23a9..ef291380 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -69,12 +69,11 @@ protected: void stopSound(Source* source); void playerJumps(); void playerStep(double dt); - EntityID createChildEmitter(); + EntityID createChildEmitter(EntityWrapper localPlayer); // Logic World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; - std::unordered_map m_Sources; private: // Help functions for working with OpenaAL @@ -98,7 +97,8 @@ private: void setGain(Source* source, float gain); void setSoundProperties(Source* source, ComponentWrapper* soundComponent); - + std::unordered_map m_Sources; + // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; @@ -109,7 +109,7 @@ private: float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; bool m_EditorEnabled = false; - EntityID m_LocalPlayer = EntityID_Invalid; + EntityWrapper m_LocalPlayer = EntityWrapper(); std::default_random_engine generator; // Events diff --git a/include/Game/Systems/LifetimeSystem.h b/include/Game/Systems/LifetimeSystem.h index da88cfa2..99cafd89 100644 --- a/include/Game/Systems/LifetimeSystem.h +++ b/include/Game/Systems/LifetimeSystem.h @@ -9,7 +9,9 @@ public: LifetimeSystem(World* world, EventBroker* eventBroker) : System(world, eventBroker) , PureSystem("Lifetime") - { } + { + LOG_INFO("ASDASDASSA"); + } virtual void Update(double dt) override; virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cLifetime, double dt) override; diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index 33dbf1fc..ec87e98a 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -20,8 +20,9 @@ private: EventBroker* m_EventBroker = nullptr; // TODO: WIP Update this - double m_TimeSinceLastFootstep = 0.0; - const double m_PlayerFootstepInterval = 1.0; + double m_DistanceMoved = 0.0; + const float m_PlayerStepLength = 1.0; + glm::vec3 m_LastPosition = glm::vec3(); bool m_LeftFoot = false; EventRelay m_EPlayerSpawned; diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 3cec4deb..b7aaaff6 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -184,14 +184,13 @@ void SoundManager::stopSound(Source* source) void SoundManager::playerJumps() { - glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer, "Physics")["Velocity"]; + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; if (vel.y == 0) { Source* source = createSource("Audio/jump/jump1.wav"); source->Type = SoundType::SFX; - m_Sources[createChildEmitter()] = source; + m_Sources[createChildEmitter(m_LocalPlayer)] = source; playSound(source); } - } void SoundManager::playerStep(double dt) @@ -199,9 +198,9 @@ void SoundManager::playerStep(double dt) } -EntityID SoundManager::createChildEmitter() +EntityID SoundManager::createChildEmitter(EntityWrapper localPlayer) { - EntityID child = m_World->CreateEntity(m_LocalPlayer); + EntityID child = m_World->CreateEntity(localPlayer.ID); m_World->AttachComponent(child, "Transform"); m_World->AttachComponent(child, "SoundEmitter"); return child; @@ -290,7 +289,7 @@ bool SoundManager::OnShoot(const Events::Shoot & e) //m_World->AttachComponent(emitterID, "Transform"); //auto emitter = m_World->AttachComponent(emitterID, "SoundEmitter"); source->Type = SoundType::SFX; - m_Sources[createChildEmitter()] = source; + m_Sources[createChildEmitter(m_LocalPlayer)] = source; playSound(source); return true; } @@ -299,7 +298,7 @@ bool SoundManager::OnPlayerSpawned(const Events::PlayerSpawned & e) { if (e.PlayerID == -1) { // Local player m_World->AttachComponent(e.Player.ID, "Listener"); - m_LocalPlayer = e.Player.ID; + m_LocalPlayer.ID = e.Player.ID; } return true; } @@ -328,14 +327,14 @@ bool SoundManager::OnInputCommand(const Events::InputCommand & e) bool SoundManager::OnCaptured(const Events::Captured & e) { int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "Team")["Team"]; - int team = (int)m_World->GetComponent(m_LocalPlayer, "Team")["Team"]; + int team = (int)m_World->GetComponent(m_LocalPlayer.ID, "Team")["Team"]; Events::PlaySoundOnEntity ev; if (team == homeTeam) { ev.FilePath = "Audio/announcer/objective_achieved.wav"; } else { ev.FilePath = "Audio/announcer/objective_failed.wav"; // have not been tested } - ev.EmitterID = createChildEmitter(); + ev.EmitterID = createChildEmitter(m_LocalPlayer); m_EventBroker->Publish(ev); return false; } @@ -348,7 +347,7 @@ bool SoundManager::OnPlayerDamage(const Events::PlayerDamage & e) int rand = dist(generator); Source* source = createSource("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); source->Type = SoundType::SFX; - m_Sources[createChildEmitter()] = source; + m_Sources[createChildEmitter(m_LocalPlayer)] = source; // Breathe std::vector buffers; @@ -364,9 +363,9 @@ bool SoundManager::OnPlayerDamage(const Events::PlayerDamage & e) bool SoundManager::OnPlayerDeath(const Events::PlayerDeath & e) { - if (e.PlayerID == m_LocalPlayer) { + if (e.PlayerID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(); + ev.EmitterID = createChildEmitter(m_LocalPlayer); ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch m_EventBroker->Publish(ev); } @@ -375,9 +374,9 @@ bool SoundManager::OnPlayerDeath(const Events::PlayerDeath & e) bool SoundManager::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) { - if (e.PlayerHealedID == m_LocalPlayer) { + if (e.PlayerHealedID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(); + ev.EmitterID = createChildEmitter(m_LocalPlayer); ev.FilePath = "Audio/pickup/pickup2.wav"; m_EventBroker->Publish(ev); } @@ -423,7 +422,7 @@ bool SoundManager::OnResume(const Events::Resume &e) bool SoundManager::OnDoubleJump(const Events::DoubleJump & e) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(); + ev.EmitterID = createChildEmitter(m_LocalPlayer); ev.FilePath = "Audio/jump/jump2.wav"; m_EventBroker->Publish(ev); return false; @@ -432,7 +431,7 @@ bool SoundManager::OnDoubleJump(const Events::DoubleJump & e) bool SoundManager::OnDashAbility(const Events::DashAbility &e) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(); + ev.EmitterID = createChildEmitter(m_LocalPlayer); ev.FilePath = "Audio/jump/dash1.wav"; m_EventBroker->Publish(ev); return false; diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index d51996ac..99f3242d 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -18,39 +18,32 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp void SoundSystem::Update(double dt) { - SoundManager::Update(dt); playerStep(dt); + SoundManager::Update(dt); } void SoundSystem::playerStep(double dt) { - if (!m_LocalPlayer.Valid()) { - return; - } - if (m_LocalPlayer.ID == EntityID_Invalid) { - return; - } - - m_TimeSinceLastFootstep += dt; - glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; - float playerSpeed = glm::length(vel); - bool isAirborne = vel.y != 0; - if (playerSpeed > 1 && !isAirborne) { - // Player is walking - if (m_TimeSinceLastFootstep * std::min(playerSpeed, 2) > m_PlayerFootstepInterval) { - // Create footstep sound - Events::PlaySoundOnEntity e; - e.EmitterID = createChildEmitter(); - if (m_LeftFoot) { - e.FilePath = "Audio/footstep/footstep2.wav"; - } else { - e.FilePath = "Audio/footstep/footstep3.wav"; - } - m_LeftFoot = !m_LeftFoot; - m_EventBroker->Publish(e); - m_TimeSinceLastFootstep = 0; - } - } + //if (!m_LocalPlayer.Valid()) { + // return; + //} + //glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"]; + //glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; + //glm::vec3 difference = pos - m_LastPosition; + //m_DistanceMoved += glm::length(difference); + //bool isAirborne = vel.y != 0; + //if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) { + // // Player is walking + // if (m_TimeSinceLastFootstep * std::min(playerSpeed, 2) > m_PlayerFootstepInterval) { + // // Create footstep sound + // Events::PlaySoundOnEntity e; + // e.EmitterID = createChildEmitter(m_LocalPlayer); + // e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav"; + // m_LeftFoot = !m_LeftFoot; + // m_EventBroker->Publish(e); + // m_TimeSinceLastFootstep = 0; + // } + //} } bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) @@ -59,7 +52,7 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) m_World->AttachComponent(e.Player.ID, "Listener"); m_LocalPlayer = e.Player; Events::PlaySoundOnEntity event; - event.EmitterID = createChildEmitter(); + event.EmitterID = createChildEmitter(m_LocalPlayer); event.FilePath = "Audio/announcer/go.wav"; m_EventBroker->Publish(event); // TEMP: starts bgm From becb197feb237fb034f3a4a8d1c670b176f09e49 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 17:46:27 +0100 Subject: [PATCH 19/38] Player walk is now based on distance traveled, and it is also reset when key is released. However when dashing the player step sound is still played. The distance of a step is also hard coded in the header file. Will config it up some day. --- include/Engine/Sound/SoundManager.h | 2 - include/Game/Systems/SoundSystem.h | 10 +++- src/Engine/Sound/SoundManager.cpp | 32 +------------ src/Game/Systems/SoundSystem.cpp | 72 ++++++++++++++++++++--------- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index ef291380..9ba4d2ad 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -133,8 +133,6 @@ private: bool OnShoot(const Events::Shoot &e); EventRelay m_EPlayerSpawned; bool OnPlayerSpawned(const Events::PlayerSpawned &e); - EventRelay m_EInputCommand; - bool OnInputCommand(const Events::InputCommand &e); EventRelay m_ECaptured; bool OnCaptured(const Events::Captured &e); EventRelay m_EPlayerDamage; diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index ec87e98a..aaadc3fd 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -4,6 +4,7 @@ #include "../Engine/Core/System.h" #include "../Engine/Sound/SoundManager.h" #include "../Engine/Core/EPlayerSpawned.h" +#include "../Engine/Input/EInputCommand.h" class SoundSystem : public PureSystem, ImpureSystem, SoundManager @@ -19,14 +20,19 @@ private: World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; + void playerJumps(); + + // TODO: WIP Update this - double m_DistanceMoved = 0.0; - const float m_PlayerStepLength = 1.0; + float m_DistanceMoved = 0.0f; + const float m_PlayerStepLength = 2.0f; glm::vec3 m_LastPosition = glm::vec3(); bool m_LeftFoot = false; EventRelay m_EPlayerSpawned; bool OnPlayerSpawned(const Events::PlayerSpawned &e); + EventRelay m_InputCommand; + bool OnInputCommand(const Events::InputCommand &e); }; #endif diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index b7aaaff6..7046ecc1 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -23,7 +23,6 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker, bool editorMo EVENT_SUBSCRIBE_MEMBER(m_EShoot, &SoundManager::OnShoot); EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundManager::OnPlayerDamage); - EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &SoundManager::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundManager::OnCaptured); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundManager::OnTriggerTouch); EVENT_SUBSCRIBE_MEMBER(m_EPause, &SoundManager::OnPause); @@ -182,16 +181,7 @@ void SoundManager::stopSound(Source* source) alSourceStop(source->ALsource); } -void SoundManager::playerJumps() -{ - glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; - if (vel.y == 0) { - Source* source = createSource("Audio/jump/jump1.wav"); - source->Type = SoundType::SFX; - m_Sources[createChildEmitter(m_LocalPlayer)] = source; - playSound(source); - } -} + void SoundManager::playerStep(double dt) { @@ -303,26 +293,6 @@ bool SoundManager::OnPlayerSpawned(const Events::PlayerSpawned & e) return true; } -bool SoundManager::OnInputCommand(const Events::InputCommand & e) -{ - if (e.Command == "Jump" && e.Value > 0) { - if (e.PlayerID == -1) { // local player - playerJumps(); - return true; - } - } - // TEMP: testing purpose (obviously) - if (e.Command == "TakeDamage" && e.Value > 0) { - if (e.PlayerID == -1) { //Local Player - Events::PlayerDamage ePlayerDamage; - ePlayerDamage.Damage = 1; - ePlayerDamage.Player = EntityWrapper(m_World, e.Player.ID); - m_EventBroker->Publish(ePlayerDamage); - return true; - } - } - return false; -} bool SoundManager::OnCaptured(const Events::Captured & e) { diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 99f3242d..e8ae324f 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -9,11 +9,12 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) m_World = world; m_EventBroker = eventbroker; EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); + EVENT_SUBSCRIBE_MEMBER(m_InputCommand, &SoundSystem::OnInputCommand); } void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) { - + } void SoundSystem::Update(double dt) @@ -24,26 +25,25 @@ void SoundSystem::Update(double dt) void SoundSystem::playerStep(double dt) { - //if (!m_LocalPlayer.Valid()) { - // return; - //} - //glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"]; - //glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; - //glm::vec3 difference = pos - m_LastPosition; - //m_DistanceMoved += glm::length(difference); - //bool isAirborne = vel.y != 0; - //if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) { - // // Player is walking - // if (m_TimeSinceLastFootstep * std::min(playerSpeed, 2) > m_PlayerFootstepInterval) { - // // Create footstep sound - // Events::PlaySoundOnEntity e; - // e.EmitterID = createChildEmitter(m_LocalPlayer); - // e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav"; - // m_LeftFoot = !m_LeftFoot; - // m_EventBroker->Publish(e); - // m_TimeSinceLastFootstep = 0; - // } - //} + if (!m_LocalPlayer.Valid()) { + return; + } + glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"]; + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; + glm::vec3 difference = pos - m_LastPosition; + m_LastPosition = pos; + m_DistanceMoved += glm::length(difference); + bool isAirborne = vel.y != 0; + if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) { + // Player moved a step's distance + // Create footstep sound + Events::PlaySoundOnEntity e; + e.EmitterID = createChildEmitter(m_LocalPlayer); + e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav"; + m_LeftFoot = !m_LeftFoot; + m_EventBroker->Publish(e); + m_DistanceMoved = 0.f; + } } bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) @@ -64,3 +64,33 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) } return true; } + +bool SoundSystem::OnInputCommand(const Events::InputCommand & e) +{ + if (e.Command == "Jump" && e.Value > 0) { + if (e.PlayerID == -1) { // local player + playerJumps(); + return true; + } + } + if (e.Command == "Forward" || e.Command == "Right") { + if (e.Value == 0) { + // Key released + // Reset the distance moved + m_DistanceMoved = 0.f; + } + } + + return false; +} + +void SoundSystem::playerJumps() +{ + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; + if (vel.y == 0) { + Events::PlaySoundOnEntity e; + e.EmitterID = createChildEmitter(m_LocalPlayer); + e.FilePath = "Audio/jump/jump1.wav"; + m_EventBroker->Publish(e); + } +} From 09fb3a6571c99193ecc932ab727256c89af7b49c Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 18:04:36 +0100 Subject: [PATCH 20/38] Added some comments. --- include/Game/Systems/SoundSystem.h | 11 ++++++++--- src/Game/Systems/SoundSystem.cpp | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index aaadc3fd..77ebcfa1 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -14,19 +14,24 @@ public: virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) override; virtual void Update(double dt) override; private: + // The logic for making the sound play when player is moving void playerStep(double dt); EntityWrapper m_LocalPlayer = EntityWrapper(); World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; + // Logic for playing a sound when a player jumps void playerJumps(); - - // TODO: WIP Update this + // Walking logic + // Keeps track of how far the player has walked within this "key press session". float m_DistanceMoved = 0.0f; - const float m_PlayerStepLength = 2.0f; + // How far a step is (How often the step sound will be played). + const float m_PlayerStepLength = 1.75f; + // To get a difference when calculating the walking state. glm::vec3 m_LastPosition = glm::vec3(); + // Determine what sound file to play. bool m_LeftFoot = false; EventRelay m_EPlayerSpawned; diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index e8ae324f..06e028f2 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -20,6 +20,7 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp void SoundSystem::Update(double dt) { playerStep(dt); + // Update listener and emitters. SoundManager::Update(dt); } @@ -28,11 +29,13 @@ void SoundSystem::playerStep(double dt) if (!m_LocalPlayer.Valid()) { return; } + // Position of the local player, used see how far a player has moved. glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"]; + // Velocity of the local player, used to see if a player is airborne. glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; - glm::vec3 difference = pos - m_LastPosition; + m_DistanceMoved += glm::length(pos - m_LastPosition); + // Set the last position for next iteration m_LastPosition = pos; - m_DistanceMoved += glm::length(difference); bool isAirborne = vel.y != 0; if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) { // Player moved a step's distance @@ -56,11 +59,11 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) event.FilePath = "Audio/announcer/go.wav"; m_EventBroker->Publish(event); // TEMP: starts bgm -// { -// Events::PlayBackgroundMusic ev; -// ev.FilePath = "Audio/bgm/ambient.wav"; -// m_EventBroker->Publish(ev); -// } + { + Events::PlayBackgroundMusic ev; + ev.FilePath = "Audio/bgm/ambient.wav"; + m_EventBroker->Publish(ev); + } } return true; } @@ -76,7 +79,7 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) if (e.Command == "Forward" || e.Command == "Right") { if (e.Value == 0) { // Key released - // Reset the distance moved + // Reset the distance moved (player walk logic) m_DistanceMoved = 0.f; } } From 12c05d5a1cc1c5d879c3e382e4c78f17e2611c0a Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 13:59:35 +0100 Subject: [PATCH 21/38] Added an Queue event for sound emitters. --- include/Engine/Sound/EPlayQueueOnEntity.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 include/Engine/Sound/EPlayQueueOnEntity.h diff --git a/include/Engine/Sound/EPlayQueueOnEntity.h b/include/Engine/Sound/EPlayQueueOnEntity.h new file mode 100644 index 00000000..e880819a --- /dev/null +++ b/include/Engine/Sound/EPlayQueueOnEntity.h @@ -0,0 +1,18 @@ +#ifndef Events_PlayQueueOnEntity_h__ +#define Events_PlayQueueOnEntity_h__ + +#include "../Core/Event.h" +#include "../Core/EntityWrapper.h" + +namespace Events +{ + +struct PlayQueueOnEntity : public Event +{ + EntityWrapper Emitter; + std::vector FilePaths; +}; + +} + +#endif From 3a5c0a27620e3bf443cbd0dab49746aaf9b3711d Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 14:01:32 +0100 Subject: [PATCH 22/38] SoundSystem does not longer inherit from SoundManager. SoundManager is a individual class that listens to events. The SoundManager only listens to the local player. --- include/Engine/Sound/Sound.h | 3 + include/Engine/Sound/SoundManager.h | 65 +++-------- include/Game/Game.h | 2 +- include/Game/Systems/SoundSystem.h | 40 ++++++- src/Engine/Sound/SoundManager.cpp | 165 ++++++---------------------- src/Game/Game.cpp | 5 + src/Game/Systems/SoundSystem.cpp | 119 +++++++++++++++++++- 7 files changed, 211 insertions(+), 188 deletions(-) diff --git a/include/Engine/Sound/Sound.h b/include/Engine/Sound/Sound.h index 6b2aac04..cccbdf07 100644 --- a/include/Engine/Sound/Sound.h +++ b/include/Engine/Sound/Sound.h @@ -1,6 +1,9 @@ #ifndef Sound_h__ #define Sound_h__ +#include +#include + #include "Core/ResourceManager.h" class Sound : public Resource diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index 9ba4d2ad..3923a17b 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -15,6 +15,7 @@ #include "Core/EventBroker.h" #include "Core/Transform.h" // Absolute transform #include "Sound/Sound.h" +#include "../Engine/Sound/EPlayQueueOnEntity.h" #include "Sound/EPlaySoundOnEntity.h" #include "Sound/EPlaySoundOnPosition.h" #include "Sound/EPlayBackgroundMusic.h" @@ -23,19 +24,9 @@ #include "Sound/EStopSound.h" #include "Sound/ESetBGMGain.h" #include "Sound/ESetSFXGain.h" -#include "Core/EShoot.h" -#include "Core/EPlayerSpawned.h" -#include "Input/EInputCommand.h" -#include "Core/ECaptured.h" -#include "Core/EPlayerDamage.h" -#include "Core/EPlayerDeath.h" -#include "Core/EPlayerHealthPickup.h" -#include "Core/EComponentAttached.h" -#include "Collision/ETrigger.h" #include "Core/EPause.h" -#include "Game/Events/EDoubleJump.h" -#include "Game/Events/EDashAbility.h" - +#include "Core/EComponentAttached.h" +#include "../Core/EPlayerSpawned.h" typedef std::pair> QueuedBuffers; @@ -61,20 +52,6 @@ public: // Update emitters / listener void Update(double dt); -protected: - // Specific logic - void playSound(Source* source); - // Need to be the same format (sample rate etc) - void playQueue(QueuedBuffers qb); - void stopSound(Source* source); - void playerJumps(); - void playerStep(double dt); - EntityID createChildEmitter(EntityWrapper localPlayer); - - // Logic - World* m_World = nullptr; - EventBroker* m_EventBroker = nullptr; - private: // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; @@ -92,25 +69,30 @@ private: void deleteInactiveEmitters(); void stopEmitters(); void updateListener(double dt); - Source* createSource(std::string filePath); ALenum getSourceState(ALuint source); void setGain(Source* source, float gain); void setSoundProperties(Source* source, ComponentWrapper* soundComponent); + // Specific logic + void playSound(Source* source); + // Need to be the same format (sample rate etc) + void playQueue(QueuedBuffers qb); + void stopSound(Source* source); + Source* createSource(std::string filePath); std::unordered_map m_Sources; + // Logic + World* m_World = nullptr; + EventBroker* m_EventBroker = nullptr; // OpenAL system variables ALCdevice* m_ALCdevice = nullptr; ALCcontext* m_ALCcontext = nullptr; - - float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; bool m_EditorEnabled = false; EntityWrapper m_LocalPlayer = EntityWrapper(); - std::default_random_engine generator; // Events EventRelay m_EPlaySoundOnEntity; @@ -129,31 +111,16 @@ private: bool OnSetBGMGain(const Events::SetBGMGain &e); EventRelay m_ESetSFXGain; bool OnSetSFXGain(const Events::SetSFXGain &e); - EventRelay m_EShoot; - bool OnShoot(const Events::Shoot &e); - EventRelay m_EPlayerSpawned; - bool OnPlayerSpawned(const Events::PlayerSpawned &e); - EventRelay m_ECaptured; - bool OnCaptured(const Events::Captured &e); - EventRelay m_EPlayerDamage; - bool OnPlayerDamage(const Events::PlayerDamage &e); - EventRelay m_EPlayerDeath; - bool OnPlayerDeath(const Events::PlayerDeath &e); - EventRelay m_EPlayerHealthPickup; - bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); EventRelay m_EComponentAttached; bool OnComponentAttached(const Events::ComponentAttached &e); - EventRelay m_ETriggerTouch; - bool OnTriggerTouch(const Events::TriggerTouch &e); EventRelay m_EPause; bool OnPause(const Events::Pause &e); EventRelay m_EResume; bool OnResume(const Events::Resume &e); - EventRelay m_EDoubleJump; - bool OnDoubleJump(const Events::DoubleJump &e); - EventRelay m_EDashAbility; - bool OnDashAbility(const Events::DashAbility &e); - + EventRelay m_EPlayerSpawned; + bool OnPlayerSpawned(const Events::PlayerSpawned &e); + EventRelay m_EPlayQueueOnEntity; + bool OnPlayQueueOnEntity(const Events::PlayQueueOnEntity &e); }; diff --git a/include/Game/Game.h b/include/Game/Game.h index b6a2c0d3..6177a818 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -30,7 +30,7 @@ #include "Network/Client.h" // Sound -//#include "Sound/SoundManager.h" +#include "Sound/SoundManager.h" #include "Systems/SoundSystem.h" class Game diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index 77ebcfa1..b14d7486 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -1,13 +1,29 @@ #ifndef Systems_SoundSystem_h__ #define Systems_SoundSystem_h__ +#include + #include "../Engine/Core/System.h" -#include "../Engine/Sound/SoundManager.h" +#include "../Engine/Core/ResourceManager.h" +#include "../Engine/Sound/Sound.h" +#include "../Engine/Sound/EPlayQueueOnEntity.h" #include "../Engine/Core/EPlayerSpawned.h" #include "../Engine/Input/EInputCommand.h" +#include "../Engine/Core/EShoot.h" +#include "../Engine/Core/EPlayerSpawned.h" +#include "../Engine/Input/EInputCommand.h" +#include "../Engine/Core/ECaptured.h" +#include "../Engine/Core/EPlayerDamage.h" +#include "../Engine/Core/EPlayerDeath.h" +#include "../Engine/Core/EPlayerHealthPickup.h" +#include "../Engine/Collision/ETrigger.h" +#include "../Engine/Sound/EPlaySoundOnEntity.h" +#include "../Engine/Sound/EPlayBackgroundMusic.h" +#include "../Game/Events/EDoubleJump.h" +#include "../Game/Events/EDashAbility.h" -class SoundSystem : public PureSystem, ImpureSystem, SoundManager +class SoundSystem : public PureSystem, ImpureSystem { public: SoundSystem(World* world, EventBroker* eventbroker); @@ -23,6 +39,8 @@ private: // Logic for playing a sound when a player jumps void playerJumps(); + // Helper function + EntityID createChildEmitter(EntityWrapper parent); // Walking logic // Keeps track of how far the player has walked within this "key press session". @@ -34,10 +52,28 @@ private: // Determine what sound file to play. bool m_LeftFoot = false; + std::default_random_engine generator; + EventRelay m_EPlayerSpawned; bool OnPlayerSpawned(const Events::PlayerSpawned &e); EventRelay m_InputCommand; bool OnInputCommand(const Events::InputCommand &e); + EventRelay m_EDoubleJump; + bool OnDoubleJump(const Events::DoubleJump &e); + EventRelay m_EDashAbility; + bool OnDashAbility(const Events::DashAbility &e); + EventRelay m_ETriggerTouch; + bool OnTriggerTouch(const Events::TriggerTouch &e); + EventRelay m_EShoot; + bool OnShoot(const Events::Shoot &e); + EventRelay m_ECaptured; + bool OnCaptured(const Events::Captured &e); + EventRelay m_EPlayerDamage; + bool OnPlayerDamage(const Events::PlayerDamage &e); + EventRelay m_EPlayerDeath; + bool OnPlayerDeath(const Events::PlayerDeath &e); + EventRelay m_EPlayerHealthPickup; + bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e); }; #endif diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 7046ecc1..d75e9afc 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -20,16 +20,11 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker, bool editorMo 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_EShoot, &SoundManager::OnShoot); - EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned); - EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundManager::OnPlayerDamage); - EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundManager::OnCaptured); - EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundManager::OnTriggerTouch); 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_EDoubleJump, &SoundManager::OnDoubleJump); - EVENT_SUBSCRIBE_MEMBER(m_EDashAbility, &SoundManager::OnDashAbility); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned); + EVENT_SUBSCRIBE_MEMBER(m_EPlayQueueOnEntity, &SoundManager::OnPlayQueueOnEntity); } SoundManager::~SoundManager() @@ -135,18 +130,21 @@ void SoundManager::updateListener(double dt) { // Should only be one listener. auto listenerComponents = m_World->GetComponents("Listener"); - if (listenerComponents == nullptr) { + if (listenerComponents == nullptr || !m_LocalPlayer.Valid()) { return; } for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { - EntityID listener = (*it).EntityID; - glm::vec3 previousPos; - alGetListener3f(AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos - glm::vec3 nextPos = Transform::AbsolutePosition(m_World, listener); // Get next (current) pos - glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt; // Calculate velocity - setListenerPos(nextPos); - setListenerVel(velocity); - setListenerOri(glm::eulerAngles(Transform::AbsoluteOrientation(m_World, listener))); + EntityWrapper listener(m_World, (*it).EntityID); + if (listener.IsChildOf(m_LocalPlayer) || listener == m_LocalPlayer) { + glm::vec3 previousPos; + alGetListener3f(AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos + glm::vec3 nextPos = Transform::AbsolutePosition(listener); // Get next (current) pos + glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt; // Calculate velocity + setListenerPos(nextPos); + setListenerVel(velocity); + setListenerOri(glm::eulerAngles(Transform::AbsoluteOrientation(listener))); + break; + } } } @@ -181,21 +179,6 @@ void SoundManager::stopSound(Source* source) alSourceStop(source->ALsource); } - - -void SoundManager::playerStep(double dt) -{ - -} - -EntityID SoundManager::createChildEmitter(EntityWrapper localPlayer) -{ - EntityID child = m_World->CreateEntity(localPlayer.ID); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - return child; -} - bool SoundManager::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); @@ -272,87 +255,6 @@ bool SoundManager::OnSetSFXGain(const Events::SetSFXGain & e) return true; } -bool SoundManager::OnShoot(const Events::Shoot & e) -{ - Source* source = createSource("Audio/laser/laser1.wav"); - //auto emitterID = m_World->CreateEntity(e.Player.ID); - //m_World->AttachComponent(emitterID, "Transform"); - //auto emitter = m_World->AttachComponent(emitterID, "SoundEmitter"); - source->Type = SoundType::SFX; - m_Sources[createChildEmitter(m_LocalPlayer)] = source; - playSound(source); - return true; -} - -bool SoundManager::OnPlayerSpawned(const Events::PlayerSpawned & e) -{ - if (e.PlayerID == -1) { // Local player - m_World->AttachComponent(e.Player.ID, "Listener"); - m_LocalPlayer.ID = e.Player.ID; - } - return true; -} - - -bool SoundManager::OnCaptured(const Events::Captured & e) -{ - int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "Team")["Team"]; - int team = (int)m_World->GetComponent(m_LocalPlayer.ID, "Team")["Team"]; - Events::PlaySoundOnEntity ev; - if (team == homeTeam) { - ev.FilePath = "Audio/announcer/objective_achieved.wav"; - } else { - ev.FilePath = "Audio/announcer/objective_failed.wav"; // have not been tested - } - ev.EmitterID = createChildEmitter(m_LocalPlayer); - m_EventBroker->Publish(ev); - return false; -} - -bool SoundManager::OnPlayerDamage(const Events::PlayerDamage & e) -{ - // Should check for only local players here... - - std::uniform_int_distribution dist(1, 12); - int rand = dist(generator); - Source* source = createSource("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); - source->Type = SoundType::SFX; - m_Sources[createChildEmitter(m_LocalPlayer)] = source; - - // Breathe - std::vector buffers; - buffers.push_back(source->SoundResource->Buffer()); - int ammountOfbreaths = (static_cast(e.Damage) / 10) + 2; // TEMP: Idk something stupid like this shit - for (int i = 0; i < ammountOfbreaths; i++) { - buffers.push_back(ResourceManager::Load("Audio/exhausted/breath.wav")->Buffer()); - } - playQueue(QueuedBuffers(std::make_pair(source->ALsource, buffers))); - - return false; -} - -bool SoundManager::OnPlayerDeath(const Events::PlayerDeath & e) -{ - if (e.PlayerID == m_LocalPlayer.ID) { - Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch - m_EventBroker->Publish(ev); - } - return false; -} - -bool SoundManager::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) -{ - if (e.PlayerHealedID == m_LocalPlayer.ID) { - Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/pickup/pickup2.wav"; - m_EventBroker->Publish(ev); - } - return false; -} - bool SoundManager::OnComponentAttached(const Events::ComponentAttached & e) { if (e.Component.Info.Name == "SoundEmitter") { @@ -363,16 +265,6 @@ bool SoundManager::OnComponentAttached(const Events::ComponentAttached & e) return false; } -bool SoundManager::OnTriggerTouch(const Events::TriggerTouch & e) -{ - if (m_World->HasComponent(e.Trigger.ID, "CapturePoint")) { - Events::PlayBackgroundMusic ev; - ev.FilePath = "Audio/bgm/drumstest.wav"; - m_EventBroker->Publish(ev); - } - return false; -} - bool SoundManager::OnPause(const Events::Pause & e) { for (auto it = m_Sources.begin(); it != m_Sources.end(); it++) { @@ -389,22 +281,29 @@ bool SoundManager::OnResume(const Events::Resume &e) return false; } -bool SoundManager::OnDoubleJump(const Events::DoubleJump & e) + +bool SoundManager::OnPlayerSpawned(const Events::PlayerSpawned &e) { - Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/jump/jump2.wav"; - m_EventBroker->Publish(ev); + if (e.PlayerID == -1) { // Local player + m_LocalPlayer = e.Player; + return true; + } return false; } -bool SoundManager::OnDashAbility(const Events::DashAbility &e) + +bool SoundManager::OnPlayQueueOnEntity(const Events::PlayQueueOnEntity &e) { - Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/jump/dash1.wav"; - m_EventBroker->Publish(ev); - return false; + Source* source = createSource(*e.FilePaths.begin()); + std::vector buffers; + buffers.push_back(source->SoundResource->Buffer()); + source->Type = SoundType::BGM; + std::vector::const_iterator it; + for (it = e.FilePaths.begin() + 1; it != e.FilePaths.end(); it++) { + buffers.push_back(ResourceManager::Load(*it)->Buffer()); + } + playQueue(QueuedBuffers(source->ALsource, buffers)); + return true; } ALenum SoundManager::getSourceState(ALuint source) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 541aa001..d18aaf8d 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -71,6 +71,8 @@ Game::Game(int argc, char* argv[]) fp.MergeEntities(m_World); } + // Create the sound manager + m_SoundManager = new SoundManager(m_World, m_EventBroker, true); // Create Octrees m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); @@ -123,6 +125,7 @@ Game::~Game() delete m_OctreeFrustrumCulling; delete m_OctreeCollision; delete m_OctreeTrigger; + delete m_SoundManager; delete m_World; delete m_FrameStack; delete m_InputProxy; @@ -150,6 +153,8 @@ void Game::Tick() m_InputProxy->Process(); m_EventBroker->Swap(); + m_SoundManager->Update(dt); + // Update network if (m_IsClientOrServer) { m_ClientOrServer->Update(); diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 06e028f2..3f9b75d0 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -4,12 +4,18 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) : System(world, eventbroker) , PureSystem("SoundEmitter") , ImpureSystem() - , SoundManager(world, eventbroker, true) { m_World = world; m_EventBroker = eventbroker; EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_InputCommand, &SoundSystem::OnInputCommand); + EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &SoundSystem::OnDoubleJump); + EVENT_SUBSCRIBE_MEMBER(m_EDashAbility, &SoundSystem::OnDashAbility); + EVENT_SUBSCRIBE_MEMBER(m_EShoot, &SoundSystem::OnShoot); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); + EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage); + EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); + EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundSystem::OnTriggerTouch); } void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) @@ -20,8 +26,6 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp void SoundSystem::Update(double dt) { playerStep(dt); - // Update listener and emitters. - SoundManager::Update(dt); } void SoundSystem::playerStep(double dt) @@ -83,6 +87,12 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) m_DistanceMoved = 0.f; } } + if (e.Command == "TakeDamage" && e.Value > 0) { + Events::PlayerDamage ev; + ev.Player = m_LocalPlayer; + ev.Damage = 1.0; + m_EventBroker->Publish(ev); + } return false; } @@ -97,3 +107,106 @@ void SoundSystem::playerJumps() m_EventBroker->Publish(e); } } + +bool SoundSystem::OnShoot(const Events::Shoot & e) +{ + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/laser/laser1.wav"; + m_EventBroker->Publish(ev); + return true; +} + +bool SoundSystem::OnCaptured(const Events::Captured & e) +{ + int homeTeam = (int)m_World->GetComponent(e.CapturePointID, "Team")["Team"]; + int team = (int)m_World->GetComponent(m_LocalPlayer.ID, "Team")["Team"]; + Events::PlaySoundOnEntity ev; + if (team == homeTeam) { + ev.FilePath = "Audio/announcer/objective_achieved.wav"; + } else { + ev.FilePath = "Audio/announcer/objective_failed.wav"; // have not been tested + } + ev.EmitterID = createChildEmitter(m_LocalPlayer); + m_EventBroker->Publish(ev); + return false; +} + +// Testing purposes atm... +bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) +{ + // Should check for only local players here... + std::uniform_int_distribution dist(1, 12); + int rand = dist(generator); + std::vector paths; + paths.push_back("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); + + // Breathe + int ammountOfbreaths = (static_cast(e.Damage) / 10) + 2; // TEMP: Idk something stupid like this shit + for (int i = 0; i < ammountOfbreaths; i++) { + paths.push_back("Audio/exhausted/breath.wav"); + } + Events::PlayQueueOnEntity ev; + ev.Emitter = m_LocalPlayer; + ev.FilePaths = paths; + m_EventBroker->Publish(ev); + return false; +} + +bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) +{ + if (e.PlayerID == m_LocalPlayer.ID) { + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch + m_EventBroker->Publish(ev); + } + return false; +} + +bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) +{ + if (e.PlayerHealedID == m_LocalPlayer.ID) { + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/pickup/pickup2.wav"; + m_EventBroker->Publish(ev); + } + return false; +} + +bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) +{ + if (m_World->HasComponent(e.Trigger.ID, "CapturePoint")) { + Events::PlayBackgroundMusic ev; + ev.FilePath = "Audio/bgm/drumstest.wav"; + m_EventBroker->Publish(ev); + } + return false; +} + +bool SoundSystem::OnDoubleJump(const Events::DoubleJump & e) +{ + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/jump/jump2.wav"; + m_EventBroker->Publish(ev); + return false; +} + +bool SoundSystem::OnDashAbility(const Events::DashAbility &e) +{ + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/jump/dash1.wav"; + m_EventBroker->Publish(ev); + return false; +} + +EntityID SoundSystem::createChildEmitter(EntityWrapper localPlayer) +{ + EntityID child = m_World->CreateEntity(localPlayer.ID); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + return child; +} \ No newline at end of file From 8e86cc2123b72dd26f348b355a5f30217a19eaa6 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 14:23:53 +0100 Subject: [PATCH 23/38] Walking sound logic moved to PlayerMovementSystem and makes use of "wishDirection" to alleviate the problem where m_DistanceMoved were reset when it was not supposed to. --- include/Game/Systems/PlayerMovementSystem.h | 14 ++++++++ include/Game/Systems/SoundSystem.h | 2 -- src/Game/Systems/PlayerMovementSystem.cpp | 40 ++++++++++++++++++++- src/Game/Systems/SoundSystem.cpp | 26 -------------- 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/include/Game/Systems/PlayerMovementSystem.h b/include/Game/Systems/PlayerMovementSystem.h index 395ccce5..defaa8ae 100644 --- a/include/Game/Systems/PlayerMovementSystem.h +++ b/include/Game/Systems/PlayerMovementSystem.h @@ -5,6 +5,7 @@ #include "Input/FirstPersonInputController.h" #include #include "Events/EDoubleJump.h" +#include "../Engine/Sound/EPlaySoundOnEntity.h" class PlayerMovementSystem : public ImpureSystem, PureSystem { @@ -19,6 +20,19 @@ private: // State std::unordered_map*> m_PlayerInputControllers; + EntityWrapper m_LocalPlayer = EntityWrapper::Invalid; + // Walking logic + // Keeps track of how far the player has walked within this "key press session". + float m_DistanceMoved = 0.0f; + // How far a step is (How often the step sound will be played). + const float m_PlayerStepLength = 1.75f; + // Determine what sound file to play. + bool m_LeftFoot = false; + // To get a difference when calculating the walking state. + glm::vec3 m_LastPosition = glm::vec3(); + // The logic for making the sound play when player is moving + void playerStep(double dt); + EventRelay m_EPlayerSpawned; bool OnPlayerSpawned(Events::PlayerSpawned& e); diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index b14d7486..4f0e7211 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -30,8 +30,6 @@ public: virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) override; virtual void Update(double dt) override; private: - // The logic for making the sound play when player is moving - void playerStep(double dt); EntityWrapper m_LocalPlayer = EntityWrapper(); World* m_World = nullptr; diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index 3e4d9e76..c5c50a41 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -56,6 +56,12 @@ void PlayerMovementSystem::Update(double dt) } else { wishSpeed = playerMovementSpeed; } + if (player.ID == m_LocalPlayer.ID) { + if (glm::length(wishDirection) == 0) { + // If no key is pressed, reset the distance moved since last step. + m_DistanceMoved = 0; + } + } glm::vec3& velocity = cPhysics["Velocity"]; ImGui::Text("velocity: (%f, %f, %f)", velocity.x, velocity.y, velocity.z); glm::vec3 groundVelocity(0.f, 0.f, 0.f); @@ -135,6 +141,7 @@ void PlayerMovementSystem::Update(double dt) controller->Reset(); } + playerStep(dt); } void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt) @@ -169,10 +176,41 @@ void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp position += velocity * (float)dt; } +void PlayerMovementSystem::playerStep(double dt) +{ + if (!m_LocalPlayer.Valid()) { + return; + } + // Position of the local player, used see how far a player has moved. + glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"]; + // Velocity of the local player, used to see if a player is airborne. + glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; + m_DistanceMoved += glm::length(pos - m_LastPosition); + // Set the last position for next iteration + m_LastPosition = pos; + bool isAirborne = vel.y != 0; + if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) { + // Player moved a step's distance + // Create footstep sound + Events::PlaySoundOnEntity e; + EntityID child = m_World->CreateEntity(m_LocalPlayer.ID); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + e.EmitterID = child; + e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav"; + m_LeftFoot = !m_LeftFoot; + m_EventBroker->Publish(e); + m_DistanceMoved = 0.f; + } +} + bool PlayerMovementSystem::OnPlayerSpawned(Events::PlayerSpawned& e) { // When a player spawns, create an input controller for them m_PlayerInputControllers[e.Player] = new FirstPersonInputController(m_EventBroker, e.PlayerID); - + if (e.PlayerID == -1) { + // Keep track of the local player + m_LocalPlayer = e.Player; + } return true; } diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 3f9b75d0..ac3ab2e9 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -25,32 +25,6 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp void SoundSystem::Update(double dt) { - playerStep(dt); -} - -void SoundSystem::playerStep(double dt) -{ - if (!m_LocalPlayer.Valid()) { - return; - } - // Position of the local player, used see how far a player has moved. - glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"]; - // Velocity of the local player, used to see if a player is airborne. - glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; - m_DistanceMoved += glm::length(pos - m_LastPosition); - // Set the last position for next iteration - m_LastPosition = pos; - bool isAirborne = vel.y != 0; - if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) { - // Player moved a step's distance - // Create footstep sound - Events::PlaySoundOnEntity e; - e.EmitterID = createChildEmitter(m_LocalPlayer); - e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav"; - m_LeftFoot = !m_LeftFoot; - m_EventBroker->Publish(e); - m_DistanceMoved = 0.f; - } } bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) From 14c5410524e12a5b0e97eb8cd5d766754a91495a Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 14:29:55 +0100 Subject: [PATCH 24/38] Merge fix --- src/Game/Systems/SoundSystem.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index ac3ab2e9..555e7ff0 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -24,8 +24,7 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp } void SoundSystem::Update(double dt) -{ -} +{ } bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) { @@ -129,23 +128,23 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) { - if (e.PlayerID == m_LocalPlayer.ID) { - Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch - m_EventBroker->Publish(ev); - } + //if (e.PlayerID == m_LocalPlayer.ID) { + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch + m_EventBroker->Publish(ev); + //} return false; } bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) { - if (e.PlayerHealedID == m_LocalPlayer.ID) { - Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/pickup/pickup2.wav"; - m_EventBroker->Publish(ev); - } + //if (e.PlayerHealedID == m_LocalPlayer.ID) { + Events::PlaySoundOnEntity ev; + ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.FilePath = "Audio/pickup/pickup2.wav"; + m_EventBroker->Publish(ev); + //} return false; } From 6f163bf3a3dfefc01924778989c14f6a74b08e54 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 16:13:09 +0100 Subject: [PATCH 25/38] Fixed bug where it crashed on respawn. Also bug where i subscribed to an event twice. --- src/Engine/Sound/SoundManager.cpp | 16 ++++++++++++++++ src/Game/Systems/SoundSystem.cpp | 13 ++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index d75e9afc..b952426c 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -101,9 +101,17 @@ void SoundManager::updateEmitters(double dt) if (!m_World->ValidEntity(it->first)) { return; } + if (!m_World->HasComponent(it->first, "SoundEmitter")) + return; + glm::vec3 previousPos; alGetSource3f(it->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get next pos + if (!m_World->HasComponent(it->first, "Transform")) + return; + if (!m_World->ValidEntity(m_World->GetParent(it->first))) { + return; + } glm::vec3 nextPos = Transform::AbsolutePosition(m_World, it->first); // Calculate velocity glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt; @@ -135,6 +143,10 @@ void SoundManager::updateListener(double dt) } for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { EntityWrapper listener(m_World, (*it).EntityID); + if (!listener.Valid()) + { + break; + } if (listener.IsChildOf(m_LocalPlayer) || listener == m_LocalPlayer) { glm::vec3 previousPos; alGetListener3f(AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos @@ -228,7 +240,11 @@ bool SoundManager::OnContinueSound(const Events::ContinueSound & e) bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) { auto listenerComponents = m_World->GetComponents("Listener"); + LOG_INFO("SIZZE: %i", listenerComponents->size()); 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; diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 555e7ff0..8315160c 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -3,7 +3,7 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) : System(world, eventbroker) , PureSystem("SoundEmitter") - , ImpureSystem() + //, ImpureSystem() { m_World = world; m_EventBroker = eventbroker; @@ -12,7 +12,6 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &SoundSystem::OnDoubleJump); EVENT_SUBSCRIBE_MEMBER(m_EDashAbility, &SoundSystem::OnDashAbility); EVENT_SUBSCRIBE_MEMBER(m_EShoot, &SoundSystem::OnShoot); - EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage); EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &SoundSystem::OnTriggerTouch); @@ -31,10 +30,10 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) if (e.PlayerID == -1) { // Local player m_World->AttachComponent(e.Player.ID, "Listener"); m_LocalPlayer = e.Player; - Events::PlaySoundOnEntity event; - event.EmitterID = createChildEmitter(m_LocalPlayer); - event.FilePath = "Audio/announcer/go.wav"; - m_EventBroker->Publish(event); + Events::PlaySoundOnEntity go; + go.EmitterID = createChildEmitter(m_LocalPlayer); + go.FilePath = "Audio/announcer/go.wav"; + m_EventBroker->Publish(go); // TEMP: starts bgm { Events::PlayBackgroundMusic ev; @@ -131,7 +130,7 @@ bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) //if (e.PlayerID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; ev.EmitterID = createChildEmitter(m_LocalPlayer); - ev.FilePath = "Audio/die/die2.wav"; // should random between a bunch + ev.FilePath = "Audio/die/die2.wav"; m_EventBroker->Publish(ev); //} return false; From 7752832ce54a8851a2e9bd0096ccad3bbf8190f6 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 16:21:49 +0100 Subject: [PATCH 26/38] No longer needs this walk logic in SoundSystem --- include/Game/Systems/SoundSystem.h | 12 +----------- src/Game/Systems/SoundSystem.cpp | 11 +---------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index 4f0e7211..c69f7296 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -39,17 +39,7 @@ private: void playerJumps(); // Helper function EntityID createChildEmitter(EntityWrapper parent); - - // Walking logic - // Keeps track of how far the player has walked within this "key press session". - float m_DistanceMoved = 0.0f; - // How far a step is (How often the step sound will be played). - const float m_PlayerStepLength = 1.75f; - // To get a difference when calculating the walking state. - glm::vec3 m_LastPosition = glm::vec3(); - // Determine what sound file to play. - bool m_LeftFoot = false; - + std::default_random_engine generator; EventRelay m_EPlayerSpawned; diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 8315160c..b218e6c1 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -18,9 +18,7 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) } void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) -{ - -} +{ } void SoundSystem::Update(double dt) { } @@ -52,13 +50,6 @@ bool SoundSystem::OnInputCommand(const Events::InputCommand & e) return true; } } - if (e.Command == "Forward" || e.Command == "Right") { - if (e.Value == 0) { - // Key released - // Reset the distance moved (player walk logic) - m_DistanceMoved = 0.f; - } - } if (e.Command == "TakeDamage" && e.Value > 0) { Events::PlayerDamage ev; ev.Player = m_LocalPlayer; From aa5878d6dfe8453a69c0e287c11cae53cfa7abb0 Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 16:40:13 +0100 Subject: [PATCH 27/38] Used config for soundsystem properties. --- assets | 2 +- include/Engine/Sound/SoundManager.h | 2 ++ include/Game/Systems/SoundSystem.h | 2 ++ resources/DefaultConfig.ini | 5 +++++ src/Engine/Sound/SoundManager.cpp | 4 +++- src/Game/Systems/SoundSystem.cpp | 8 +++++--- 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/assets b/assets index cfeeb1f8..88b1b9f0 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit cfeeb1f880e19356477a703da92204eede09db5d +Subproject commit 88b1b9f047c2cf02c1d39da39dff472cadfb93e7 diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index 3923a17b..2c07578c 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -13,6 +13,8 @@ #include "Core/World.h" #include "Core/EventBroker.h" +#include "../Engine/Core/ResourceManager.h" +#include "../Engine/Core/ConfigFile.h" #include "Core/Transform.h" // Absolute transform #include "Sound/Sound.h" #include "../Engine/Sound/EPlayQueueOnEntity.h" diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index c69f7296..0f7f5102 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -5,6 +5,7 @@ #include "../Engine/Core/System.h" #include "../Engine/Core/ResourceManager.h" +#include "../Engine/Core/ConfigFile.h" #include "../Engine/Sound/Sound.h" #include "../Engine/Sound/EPlayQueueOnEntity.h" #include "../Engine/Core/EPlayerSpawned.h" @@ -34,6 +35,7 @@ private: World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; + std::string m_Announcer = ""; // Logic for playing a sound when a player jumps void playerJumps(); diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index 86dc735c..12ec06c8 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -29,3 +29,8 @@ TimeoutMs=15000 [Multithreading] ResourceLoading=true + +[Sound] +BGMVolume=1.0 +SFXVolume=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 b952426c..972a38bb 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -2,12 +2,14 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker, bool editorMode) { + ConfigFile* config = ResourceManager::Load("Config.ini"); m_EventBroker = eventBroker; m_World = world; m_EditorEnabled = editorMode; + m_BGMVolumeChannel = config->Get("Sound.BGMVolume", 1.f); + m_SFXVolumeChannel = config->Get("Sound.SFXVolume", 1.f); initOpenAL(); - alSpeedOfSound(340.29f); alDistanceModel(AL_LINEAR_DISTANCE); alDopplerFactor(1); diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index b218e6c1..06e7d67a 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -5,6 +5,8 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventbroker) , PureSystem("SoundEmitter") //, ImpureSystem() { + ConfigFile* config = ResourceManager::Load("Config.ini"); + m_Announcer = ResourceManager::Load("Config.ini")->Get("Sound.Announcer", "female"); m_World = world; m_EventBroker = eventbroker; EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); @@ -30,7 +32,7 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) m_LocalPlayer = e.Player; Events::PlaySoundOnEntity go; go.EmitterID = createChildEmitter(m_LocalPlayer); - go.FilePath = "Audio/announcer/go.wav"; + go.FilePath = "Audio/announcer/" + m_Announcer + "/go.wav"; m_EventBroker->Publish(go); // TEMP: starts bgm { @@ -86,9 +88,9 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) int team = (int)m_World->GetComponent(m_LocalPlayer.ID, "Team")["Team"]; Events::PlaySoundOnEntity ev; if (team == homeTeam) { - ev.FilePath = "Audio/announcer/objective_achieved.wav"; + ev.FilePath = "Audio/announcer/" + m_Announcer + "/objective_achieved.wav"; } else { - ev.FilePath = "Audio/announcer/objective_failed.wav"; // have not been tested + ev.FilePath = "Audio/announcer/" + m_Announcer + "/objective_failed.wav"; // have not been tested } ev.EmitterID = createChildEmitter(m_LocalPlayer); m_EventBroker->Publish(ev); From 2c64ffc3d11782a0bccb0245fc1b18b91d2b00ad Mon Sep 17 00:00:00 2001 From: stiffly Date: Mon, 8 Feb 2016 16:52:35 +0100 Subject: [PATCH 28/38] No longer using a variable to see if we're in editor mode. --- include/Engine/Sound/SoundManager.h | 3 +-- src/Engine/Sound/SoundManager.cpp | 26 ++++++++++---------------- src/Game/Game.cpp | 2 +- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index 2c07578c..38ec62ff 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -49,7 +49,7 @@ class SoundManager { public: SoundManager() { } - SoundManager(World* world, EventBroker* eventBroker, bool editorMode); + SoundManager(World* world, EventBroker* eventBroker); ~SoundManager(); // Update emitters / listener void Update(double dt); @@ -93,7 +93,6 @@ private: float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; - bool m_EditorEnabled = false; EntityWrapper m_LocalPlayer = EntityWrapper(); // Events diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 972a38bb..8ba6a502 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -1,11 +1,10 @@ #include "Sound/SoundManager.h" -SoundManager::SoundManager(World* world, EventBroker* eventBroker, bool editorMode) +SoundManager::SoundManager(World* world, EventBroker* eventBroker) { ConfigFile* config = ResourceManager::Load("Config.ini"); m_EventBroker = eventBroker; m_World = world; - m_EditorEnabled = editorMode; m_BGMVolumeChannel = config->Get("Sound.BGMVolume", 1.f); m_SFXVolumeChannel = config->Get("Sound.SFXVolume", 1.f); @@ -103,9 +102,9 @@ void SoundManager::updateEmitters(double dt) if (!m_World->ValidEntity(it->first)) { return; } - if (!m_World->HasComponent(it->first, "SoundEmitter")) + if (!m_World->HasComponent(it->first, "SoundEmitter")) return; - + glm::vec3 previousPos; alGetSource3f(it->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get next pos @@ -123,14 +122,11 @@ void SoundManager::updateEmitters(double dt) auto emitter = m_World->GetComponent(it->first, "SoundEmitter"); setSoundProperties(it->second, &emitter); - // To make an emitter play when spawned in editor mode - if (m_EditorEnabled) { - // Path changed - if (it->second->SoundResource->Path() != (std::string)emitter["FilePath"]) { - it->second->SoundResource = ResourceManager::Load((std::string)emitter["FilePath"]); - if (it->second->SoundResource->Buffer() != 0) { - playSound(it->second); - } + // Path changed + if (it->second->SoundResource->Path() != (std::string)emitter["FilePath"]) { + it->second->SoundResource = ResourceManager::Load((std::string)emitter["FilePath"]); + if (it->second->SoundResource->Buffer() != 0) { + playSound(it->second); } } } @@ -145,8 +141,7 @@ void SoundManager::updateListener(double dt) } for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { EntityWrapper listener(m_World, (*it).EntityID); - if (!listener.Valid()) - { + if (!listener.Valid()) { break; } if (listener.IsChildOf(m_LocalPlayer) || listener == m_LocalPlayer) { @@ -242,9 +237,8 @@ bool SoundManager::OnContinueSound(const Events::ContinueSound & e) bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) { auto listenerComponents = m_World->GetComponents("Listener"); - LOG_INFO("SIZZE: %i", listenerComponents->size()); for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { - if((*it).EntityID != m_LocalPlayer.ID) { + if ((*it).EntityID != m_LocalPlayer.ID) { break; } auto emitterChild = m_World->CreateEntity((*it).EntityID); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 6c495272..37eb2ea6 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -74,7 +74,7 @@ Game::Game(int argc, char* argv[]) } // Create the sound manager - m_SoundManager = new SoundManager(m_World, m_EventBroker, true); + m_SoundManager = new SoundManager(m_World, m_EventBroker); // Create Octrees m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); From b748deb1e1e91c3177af65e1c778454f6afd1585 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 10:05:49 +0100 Subject: [PATCH 29/38] WIP FrustumCulling, most code added but not working now. --- include/Engine/Core/Octree.h | 117 ++++++++++++++++++++++++ include/Engine/Rendering/RenderSystem.h | 7 +- src/Engine/Rendering/RenderSystem.cpp | 53 ++++++++++- src/Game/Game.cpp | 10 +- 4 files changed, 179 insertions(+), 8 deletions(-) diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index 907d5736..d97d644b 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -2,6 +2,7 @@ #define Octree_h__ #include +#include #include "../Common.h" #include "AABB.h" @@ -40,6 +41,8 @@ public: //The type Box must be AABB, or inherit from AABB. template void ObjectsInSameRegion(const Box& box, std::vector& outObjects); + //Get the objects that are inside the frustum defined by the viewProjection matrix, the objects are put in outObjects. + void ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector& outObjects); //Empty the tree of all objects, static and dynamic. void ClearObjects(); //Empty the tree of all dynamic objects. Static objects remain in the tree. @@ -68,6 +71,56 @@ struct Output float CollideDistance; }; +//Contains points P in: dot(normal, P) + d = 0 +struct Plane +{ + glm::vec3 normal; + float distance; +}; + +//A frustum defined by 6 planes. +struct Frustum +{ + enum Output + { + Inside, + Outside, + Intersects + }; + Plane planes[6]; + + Output VsAABB(const AABB& box) const + { + const glm::vec3& maxCorner = box.MaxCorner(); + const glm::vec3& minCorner = box.MinCorner(); + bool completelyInside = true; + for (const Plane& p : planes) { + bool anyWasInside = false; + bool anyWasOutside = false; + //If points are on both sides of the plane, we can stop. + for (int i = 0; i < 8 && (!anyWasInside || !anyWasOutside); ++i) { + std::bitset<3> bits(i); + glm::vec3 corner; + corner.x = bits.test(0) ? maxCorner.x : minCorner.x; + corner.y = bits.test(1) ? maxCorner.y : minCorner.y; + corner.z = bits.test(2) ? maxCorner.z : minCorner.z; + if (glm::dot(p.normal, corner) > p.distance) { + anyWasInside = true; + } else { + anyWasOutside = true; + } + } + if (!anyWasInside) { + return Outside; + } + if (anyWasOutside) { + completelyInside = false; + } + } + return completelyInside ? Inside : Intersects; + } +}; + struct ContainedObject { ContainedObject() @@ -97,6 +150,8 @@ struct Child void AddStaticObject(const AABB& box); template void ObjectsInSameRegion(const Box& box, std::vector& outObjects) const; + template + void ObjectsInFrustum(const Frustum& frustum, std::vector& outObjects, bool takeAllDontTest) const; void ClearObjects(); void ClearDynamicObjects(); bool RayCollides(const Ray& ray, Output& data) const; @@ -154,6 +209,26 @@ void Octree::ObjectsInSameRegion(const Box& box, std::vector& outObjects) m_Root->ObjectsInSameRegion(box, outObjects); } +template +void Octree::ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector& outObjects) +{ + falsifyObjectChecks(); + OctSpace::Frustum frustum; + for (int i = 0; i < 6; ++i) { + int sign = 2 * (i % 2) - 1; + int index = i / 2; + OctSpace::Plane& plane = frustum.planes[i]; + plane.normal.x = viewProj[0].w + sign * viewProj[0][index]; + plane.normal.y = viewProj[1].w + sign * viewProj[1][index]; + plane.normal.z = viewProj[2].w + sign * viewProj[2][index]; + plane.distance = viewProj[3].w + sign * viewProj[3][index]; + float divByNormalLength = 1.0f / glm::length(plane.normal); + plane.normal *= divByNormalLength; + plane.distance *= divByNormalLength; + } + m_Root->ObjectsInFrustum(frustum, outObjects, false); +} + template void Octree::ClearObjects() { @@ -230,4 +305,46 @@ void OctSpace::Child::ObjectsInSameRegion(const Box& box, std::vector& outObj } } +template +void OctSpace::Child::ObjectsInFrustum(const Frustum& frustum, std::vector& outObjects, bool takeAllDontTest) const +{ + if (hasChildren()) { + for (const Child* c : m_Children) { + Frustum::Output out = Frustum::Inside; + if (!takeAllDontTest) { + out = frustum.VsAABB(c->m_Box); + if (out == Frustum::Outside) { + continue; + } + } + c->ObjectsInFrustum(frustum, outObjects, out == Frustum::Inside); + } + } else { + size_t startIndex = outObjects.size(); + int numDuplicates = 0; + outObjects.resize(outObjects.size() + m_StaticObjIndices.size() + m_DynamicObjIndices.size()); + for (size_t i = 0; i < m_StaticObjIndices.size(); ++i) { + ContainedObject& obj = m_StaticObjectsRef[m_StaticObjIndices[i]]; + if (obj.Checked || !frustum.VsAABB(obj.Box)) { + ++numDuplicates; + } else { + obj.Checked = true; + outObjects[startIndex + i - numDuplicates] = *static_cast(obj.Box.get()); + } + } + for (size_t i = 0; i < m_DynamicObjIndices.size(); ++i) { + ContainedObject& obj = m_DynamicObjectsRef[m_DynamicObjIndices[i]]; + if (obj.Checked || !frustum.VsAABB(obj.Box)) { + ++numDuplicates; + } else { + obj.Checked = true; + outObjects[startIndex + i - numDuplicates] = *static_cast(obj.Box.get()); + } + } + for (size_t i = 0; i < numDuplicates; ++i) { + outObjects.pop_back(); + } + } +} + #endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index d64147b9..d404c56a 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -16,11 +16,13 @@ #include "PointLightJob.h" #include "../Core/Transform.h" #include "../Core/EPlayerSpawned.h" +#include "../Core/Octree.h" +#include "../Collision/EntityAABB.h" class RenderSystem : public ImpureSystem { public: - RenderSystem(World* world, EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame); + RenderSystem(World* world, EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame, Octree* frustumCullOctree); ~RenderSystem(); virtual void Update(double dt) override; @@ -29,9 +31,12 @@ private: const IRenderer* m_Renderer; RenderFrame* m_RenderFrame; Camera* m_Camera; + Camera* m_LastCullCamera; + Camera** m_FrustumCamPtr; World* m_World; EntityWrapper m_CurrentCamera = EntityWrapper::Invalid; EntityWrapper m_LocalPlayer = EntityWrapper::Invalid; + Octree* m_Octree; EventRelay m_ESetCamera; bool OnSetCamera(Events::SetCamera &event); diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index eaecf99e..d57e263a 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -1,21 +1,26 @@ #include "Rendering/RenderSystem.h" +#include "Collision/Collision.h" -RenderSystem::RenderSystem(World* world, EventBroker* eventBroker, const IRenderer* renderer, RenderFrame* renderFrame) +RenderSystem::RenderSystem(World* world, EventBroker* eventBroker, const IRenderer* renderer, RenderFrame* renderFrame, Octree* frustumCullOctree) : System(world, eventBroker) , m_Renderer(renderer) , m_RenderFrame(renderFrame) , m_World(world) + , m_Octree(frustumCullOctree) { EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &RenderSystem::OnPlayerSpawned); m_Camera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); + m_LastCullCamera = new Camera(*m_Camera); + m_FrustumCamPtr = &m_Camera; } RenderSystem::~RenderSystem() { delete m_Camera; + delete m_LastCullCamera; } bool RenderSystem::OnSetCamera(Events::SetCamera& e) @@ -28,6 +33,17 @@ bool RenderSystem::OnSetCamera(Events::SetCamera& e) m_Camera->SetPosition(cTransform["Position"]); m_Camera->SetOrientation(glm::quat((const glm::vec3&)cTransform["Orientation"])); m_CurrentCamera = e.CameraEntity; + //Right now, lets set the camera to cull away stuff if it is connected to anything. + //TODO: This won't work with spectators, or death anim. + if (m_CurrentCamera.Parent().Valid()) { + //Copy the camera into the last frustum camera, without allocating new memory. + new ((void*)m_LastCullCamera) Camera(*m_Camera); + m_FrustumCamPtr = &m_Camera; + } else { + //If the camera has no parents, i.e. a free camera, + //then we cull from the last camera, so we can see if the culling works. + m_FrustumCamPtr = &m_LastCullCamera; + } return true; } @@ -42,11 +58,33 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) void RenderSystem::fillModels(std::list>& opaqueJobs, std::list>& transparentJobs) { + + std::vector seenEntities; + //m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(), seenEntities); + //m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ViewMatrix() * (*m_FrustumCamPtr)->ProjectionMatrix(), seenEntities); + + glm::mat4x4 viewProj = (*m_FrustumCamPtr)->ViewMatrix() * (*m_FrustumCamPtr)->ProjectionMatrix(); + OctSpace::Frustum frustum; + for (int i = 0; i < 6; ++i) { + int sign = 2 * (i % 2) - 1; + int index = i / 2; + OctSpace::Plane& plane = frustum.planes[i]; + plane.normal.x = viewProj[0].w + sign * viewProj[0][index]; + plane.normal.y = viewProj[1].w + sign * viewProj[1][index]; + plane.normal.z = viewProj[2].w + sign * viewProj[2][index]; + plane.distance = viewProj[3].w + sign * viewProj[3][index]; + float divByNormalLength = 1.0f / glm::length(plane.normal); + plane.normal *= divByNormalLength; + plane.distance *= divByNormalLength; + } + + //for (auto& seenEntity : seenEntities) { + // EntityWrapper entity = seenEntity.Entity; + // ComponentWrapper cModel = entity["Model"]; auto models = m_World->GetComponents("Model"); if (models == nullptr) { return; } - for (auto& cModel : *models) { bool visible = cModel["Visible"]; if (!visible) { @@ -57,7 +95,7 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, continue; } - EntityWrapper entity(m_World, cModel.EntityID); + EntityWrapper entity = EntityWrapper(m_World, cModel.EntityID); // Only render children of a camera if that camera is currently active if (isChildOfACamera(entity) && !isChildOfCurrentCamera(entity)) { @@ -69,6 +107,15 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, continue; } + if (entity.HasComponent("AABB")) { + OctSpace::Frustum::Output o = frustum.VsAABB(*Collision::EntityAbsoluteAABB(entity)); + if (o == OctSpace::Frustum::Outside) { + continue; + } + } else { + continue; + } + Model* model; try { model = ResourceManager::Load<::Model, true>(resource); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 0c92271d..d3c2eb57 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -75,9 +75,10 @@ Game::Game(int argc, char* argv[]) // Create Octrees - m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); - m_OctreeTrigger = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); - m_OctreeFrustrumCulling = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); + AABB boxContainingTheWorld(glm::vec3(-300), glm::vec3(300)); + m_OctreeCollision = new Octree(boxContainingTheWorld, 4); + m_OctreeTrigger = new Octree(boxContainingTheWorld, 4); + m_OctreeFrustrumCulling = new Octree(boxContainingTheWorld, 4); // Create system pipeline m_SystemPipeline = new SystemPipeline(m_World, m_EventBroker); @@ -99,6 +100,7 @@ Game::Game(int argc, char* argv[]) ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision, "Collidable"); m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeTrigger, "Player"); + m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeFrustrumCulling, "Model"); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); @@ -107,7 +109,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeTrigger); ++updateOrderLevel; - m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame); + m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame, m_OctreeFrustrumCulling); ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame); From b30891aecdbd9886d4f876d01b8cad91d41b6eb2 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 15:00:30 +0100 Subject: [PATCH 30/38] WIP, debugging code added, still not working. --- include/Engine/Core/Octree.h | 27 ++++----- include/Engine/Rendering/RenderSystem.h | 1 + src/Engine/Rendering/RenderSystem.cpp | 73 ++++++++++++++++--------- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index d97d644b..e0165cef 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -74,8 +74,8 @@ struct Output //Contains points P in: dot(normal, P) + d = 0 struct Plane { - glm::vec3 normal; - float distance; + glm::vec3 Normal; + float Distance; }; //A frustum defined by 6 planes. @@ -87,14 +87,14 @@ struct Frustum Outside, Intersects }; - Plane planes[6]; + Plane Planes[6]; Output VsAABB(const AABB& box) const { const glm::vec3& maxCorner = box.MaxCorner(); const glm::vec3& minCorner = box.MinCorner(); bool completelyInside = true; - for (const Plane& p : planes) { + for (const Plane& p : Planes) { bool anyWasInside = false; bool anyWasOutside = false; //If points are on both sides of the plane, we can stop. @@ -104,7 +104,7 @@ struct Frustum corner.x = bits.test(0) ? maxCorner.x : minCorner.x; corner.y = bits.test(1) ? maxCorner.y : minCorner.y; corner.z = bits.test(2) ? maxCorner.z : minCorner.z; - if (glm::dot(p.normal, corner) > p.distance) { + if (glm::dot(p.Normal, corner) > p.Distance) { anyWasInside = true; } else { anyWasOutside = true; @@ -214,17 +214,18 @@ void Octree::ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector& ou { falsifyObjectChecks(); OctSpace::Frustum frustum; + //Order: Right, left, top, bottom, far, near. for (int i = 0; i < 6; ++i) { int sign = 2 * (i % 2) - 1; int index = i / 2; - OctSpace::Plane& plane = frustum.planes[i]; - plane.normal.x = viewProj[0].w + sign * viewProj[0][index]; - plane.normal.y = viewProj[1].w + sign * viewProj[1][index]; - plane.normal.z = viewProj[2].w + sign * viewProj[2][index]; - plane.distance = viewProj[3].w + sign * viewProj[3][index]; - float divByNormalLength = 1.0f / glm::length(plane.normal); - plane.normal *= divByNormalLength; - plane.distance *= divByNormalLength; + OctSpace::Plane& plane = frustum.Planes[i]; + plane.Normal.x = viewProj[0].w + sign * viewProj[0][index]; + plane.Normal.y = viewProj[1].w + sign * viewProj[1][index]; + plane.Normal.z = viewProj[2].w + sign * viewProj[2][index]; + plane.Distance = viewProj[3].w + sign * viewProj[3][index]; + float divByNormalLength = 1.0f / glm::length(plane.Normal); + plane.Normal *= divByNormalLength; + plane.Distance *= divByNormalLength; } m_Root->ObjectsInFrustum(frustum, outObjects, false); } diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index d404c56a..e4e6475a 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -33,6 +33,7 @@ private: Camera* m_Camera; Camera* m_LastCullCamera; Camera** m_FrustumCamPtr; + EntityWrapper frustumEntity; World* m_World; EntityWrapper m_CurrentCamera = EntityWrapper::Invalid; EntityWrapper m_LocalPlayer = EntityWrapper::Invalid; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index d57e263a..9d7e7530 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -25,6 +25,19 @@ RenderSystem::~RenderSystem() bool RenderSystem::OnSetCamera(Events::SetCamera& e) { + //Right now, lets set the camera to cull away stuff if it is connected to a player. + //TODO: This won't work with spectators, or death anim. + if (e.CameraEntity.FirstParentWithComponent("Player").Valid()) { + m_FrustumCamPtr = &m_Camera; + LOG_INFO("Setting frustum to new camera."); + } else if (e.CameraEntity != m_CurrentCamera) { + //If the camera has no parents, i.e. a free camera, + //then we cull from the last camera, so we can see if the culling works. + //Copy the camera into the last frustum camera, without allocating new memory. + new ((void*)m_LastCullCamera) Camera(*m_Camera); + m_FrustumCamPtr = &m_LastCullCamera; + LOG_INFO("New camera, frustum remains at old camera."); + } ComponentWrapper cTransform = e.CameraEntity["Transform"]; ComponentWrapper cCamera = e.CameraEntity["Camera"]; m_Camera->SetFOV((double)cCamera["FOV"]); @@ -33,17 +46,6 @@ bool RenderSystem::OnSetCamera(Events::SetCamera& e) m_Camera->SetPosition(cTransform["Position"]); m_Camera->SetOrientation(glm::quat((const glm::vec3&)cTransform["Orientation"])); m_CurrentCamera = e.CameraEntity; - //Right now, lets set the camera to cull away stuff if it is connected to anything. - //TODO: This won't work with spectators, or death anim. - if (m_CurrentCamera.Parent().Valid()) { - //Copy the camera into the last frustum camera, without allocating new memory. - new ((void*)m_LastCullCamera) Camera(*m_Camera); - m_FrustumCamPtr = &m_Camera; - } else { - //If the camera has no parents, i.e. a free camera, - //then we cull from the last camera, so we can see if the culling works. - m_FrustumCamPtr = &m_LastCullCamera; - } return true; } @@ -56,26 +58,45 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera); } +float frustrumTODO = 0.f; + void RenderSystem::fillModels(std::list>& opaqueJobs, std::list>& transparentJobs) { + if (!frustumEntity.Valid() && m_World->GetComponentPools().size() > 0) { + frustumEntity = EntityWrapper(m_World, m_World->CreateEntity()); + m_World->AttachComponent(frustumEntity.ID, "Transform"); + m_World->AttachComponent(frustumEntity.ID, "Model"); + frustumEntity["Model"]["Resource"] = "Models/Core/UnitCube.mesh"; + } std::vector seenEntities; //m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(), seenEntities); - //m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ViewMatrix() * (*m_FrustumCamPtr)->ProjectionMatrix(), seenEntities); - glm::mat4x4 viewProj = (*m_FrustumCamPtr)->ViewMatrix() * (*m_FrustumCamPtr)->ProjectionMatrix(); + glm::mat4x4 viewProj = (*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(); OctSpace::Frustum frustum; + //Order: Right, left, top, bottom, far, near. + int sign = 1; for (int i = 0; i < 6; ++i) { - int sign = 2 * (i % 2) - 1; + sign = -sign; int index = i / 2; - OctSpace::Plane& plane = frustum.planes[i]; - plane.normal.x = viewProj[0].w + sign * viewProj[0][index]; - plane.normal.y = viewProj[1].w + sign * viewProj[1][index]; - plane.normal.z = viewProj[2].w + sign * viewProj[2][index]; - plane.distance = viewProj[3].w + sign * viewProj[3][index]; - float divByNormalLength = 1.0f / glm::length(plane.normal); - plane.normal *= divByNormalLength; - plane.distance *= divByNormalLength; + OctSpace::Plane& plane = frustum.Planes[i]; + plane.Normal.x = viewProj[0].w + sign * viewProj[0][index]; + plane.Normal.y = viewProj[1].w + sign * viewProj[1][index]; + plane.Normal.z = viewProj[2].w + sign * viewProj[2][index]; + plane.Distance = viewProj[3].w + sign * viewProj[3][index]; + float divByNormalLength = 1.0f / glm::length(plane.Normal); + plane.Normal *= divByNormalLength; + plane.Distance *= divByNormalLength; + } + if (frustumEntity.Valid()) { + int planeI = 0; + glm::vec3 pos = (*m_FrustumCamPtr)->Position() + frustrumTODO * (*m_FrustumCamPtr)->Forward(); + float dist = glm::dot(frustum.Planes[planeI].Normal, pos) + frustum.Planes[planeI].Distance; + frustumEntity["Transform"]["Position"] = pos - dist * frustum.Planes[planeI].Normal; + frustumEntity["Transform"]["Scale"] = glm::vec3(0.15f); + } + if (++frustrumTODO > 75) { + frustrumTODO = 0.f; } //for (auto& seenEntity : seenEntities) { @@ -109,11 +130,11 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, if (entity.HasComponent("AABB")) { OctSpace::Frustum::Output o = frustum.VsAABB(*Collision::EntityAbsoluteAABB(entity)); - if (o == OctSpace::Frustum::Outside) { - continue; + if (o == OctSpace::Frustum::Outside && entity != frustumEntity) { + resource = "Models/Core/UnitRaptor.mesh"; } - } else { - continue; + } else if (entity != frustumEntity){ + resource = "Models/Core/Error.mesh"; } Model* model; From 239a8e7a6a38616614f9f09aa5a44dfab95b23e7 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 17:12:23 +0100 Subject: [PATCH 31/38] Frustum culling should be working. --- include/Engine/Core/Frustum.h | 77 +++++++++++++++++++++++ include/Engine/Core/Octree.h | 81 +++---------------------- include/Engine/Rendering/RenderSystem.h | 3 - src/Engine/Rendering/RenderSystem.cpp | 78 ++---------------------- src/Game/Game.cpp | 2 +- 5 files changed, 93 insertions(+), 148 deletions(-) create mode 100644 include/Engine/Core/Frustum.h diff --git a/include/Engine/Core/Frustum.h b/include/Engine/Core/Frustum.h new file mode 100644 index 00000000..c2d83b23 --- /dev/null +++ b/include/Engine/Core/Frustum.h @@ -0,0 +1,77 @@ +#ifndef Frustum_h__ +#define Frustum_h__ + +#include "../GLM.h" +#include "AABB.h" +#include + +//A frustum defined by 6 planes. +struct Frustum +{ + //Contains points P in: dot(normal, P) + d = 0 + struct Plane + { + glm::vec3 Normal; + float Distance; + }; + + enum class Output + { + Inside, + Outside, + Intersects + }; + Plane Planes[6]; + + Frustum() = default; + Frustum(glm::mat4x4 viewProjMatrix) + { + //Order: Right, left, top, bottom, far, near. + int sign = 1; + for (int i = 0; i < 6; ++i) { + sign = -sign; + int index = i / 2; + Plane& plane = Planes[i]; + plane.Normal.x = viewProjMatrix[0].w + sign * viewProjMatrix[0][index]; + plane.Normal.y = viewProjMatrix[1].w + sign * viewProjMatrix[1][index]; + plane.Normal.z = viewProjMatrix[2].w + sign * viewProjMatrix[2][index]; + plane.Distance = viewProjMatrix[3].w + sign * viewProjMatrix[3][index]; + float divByNormalLength = 1.0f / glm::length(plane.Normal); + plane.Normal *= divByNormalLength; + plane.Distance *= divByNormalLength; + } + } + + Output VsAABB(const AABB& box) const + { + const glm::vec3& maxCorner = box.MaxCorner(); + const glm::vec3& minCorner = box.MinCorner(); + bool completelyInside = true; + for (const Plane& p : Planes) { + bool anyWasInside = false; + bool anyWasOutside = false; + //If points are on both sides of the plane, we can stop. + for (int i = 0; i < 8 && (!anyWasInside || !anyWasOutside); ++i) { + std::bitset<3> bits(i); + glm::vec3 corner; + corner.x = bits.test(0) ? maxCorner.x : minCorner.x; + corner.y = bits.test(1) ? maxCorner.y : minCorner.y; + corner.z = bits.test(2) ? maxCorner.z : minCorner.z; + if (glm::dot(p.Normal, corner) > -p.Distance) { + anyWasInside = true; + } else { + anyWasOutside = true; + } + } + if (!anyWasInside) { + return Output::Outside; + } + if (anyWasOutside) { + completelyInside = false; + } + } + return completelyInside ? Output::Inside : Output::Intersects; + } +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index e0165cef..49a8b045 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -6,6 +6,7 @@ #include "../Common.h" #include "AABB.h" +#include "Frustum.h" //Fwd declarations. class Ray; @@ -41,8 +42,8 @@ public: //The type Box must be AABB, or inherit from AABB. template void ObjectsInSameRegion(const Box& box, std::vector& outObjects); - //Get the objects that are inside the frustum defined by the viewProjection matrix, the objects are put in outObjects. - void ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector& outObjects); + //Get the objects that are inside the frustum, the objects are put in outObjects. + void ObjectsInFrustum(const Frustum& frustum, std::vector& outObjects); //Empty the tree of all objects, static and dynamic. void ClearObjects(); //Empty the tree of all dynamic objects. Static objects remain in the tree. @@ -71,56 +72,6 @@ struct Output float CollideDistance; }; -//Contains points P in: dot(normal, P) + d = 0 -struct Plane -{ - glm::vec3 Normal; - float Distance; -}; - -//A frustum defined by 6 planes. -struct Frustum -{ - enum Output - { - Inside, - Outside, - Intersects - }; - Plane Planes[6]; - - Output VsAABB(const AABB& box) const - { - const glm::vec3& maxCorner = box.MaxCorner(); - const glm::vec3& minCorner = box.MinCorner(); - bool completelyInside = true; - for (const Plane& p : Planes) { - bool anyWasInside = false; - bool anyWasOutside = false; - //If points are on both sides of the plane, we can stop. - for (int i = 0; i < 8 && (!anyWasInside || !anyWasOutside); ++i) { - std::bitset<3> bits(i); - glm::vec3 corner; - corner.x = bits.test(0) ? maxCorner.x : minCorner.x; - corner.y = bits.test(1) ? maxCorner.y : minCorner.y; - corner.z = bits.test(2) ? maxCorner.z : minCorner.z; - if (glm::dot(p.Normal, corner) > p.Distance) { - anyWasInside = true; - } else { - anyWasOutside = true; - } - } - if (!anyWasInside) { - return Outside; - } - if (anyWasOutside) { - completelyInside = false; - } - } - return completelyInside ? Inside : Intersects; - } -}; - struct ContainedObject { ContainedObject() @@ -210,23 +161,9 @@ void Octree::ObjectsInSameRegion(const Box& box, std::vector& outObjects) } template -void Octree::ObjectsInFrustum(const glm::mat4x4& viewProj, std::vector& outObjects) +void Octree::ObjectsInFrustum(const Frustum& frustum, std::vector& outObjects) { falsifyObjectChecks(); - OctSpace::Frustum frustum; - //Order: Right, left, top, bottom, far, near. - for (int i = 0; i < 6; ++i) { - int sign = 2 * (i % 2) - 1; - int index = i / 2; - OctSpace::Plane& plane = frustum.Planes[i]; - plane.Normal.x = viewProj[0].w + sign * viewProj[0][index]; - plane.Normal.y = viewProj[1].w + sign * viewProj[1][index]; - plane.Normal.z = viewProj[2].w + sign * viewProj[2][index]; - plane.Distance = viewProj[3].w + sign * viewProj[3][index]; - float divByNormalLength = 1.0f / glm::length(plane.Normal); - plane.Normal *= divByNormalLength; - plane.Distance *= divByNormalLength; - } m_Root->ObjectsInFrustum(frustum, outObjects, false); } @@ -311,14 +248,14 @@ void OctSpace::Child::ObjectsInFrustum(const Frustum& frustum, std::vector& o { if (hasChildren()) { for (const Child* c : m_Children) { - Frustum::Output out = Frustum::Inside; + Frustum::Output out = Frustum::Output::Inside; if (!takeAllDontTest) { out = frustum.VsAABB(c->m_Box); - if (out == Frustum::Outside) { + if (out == Frustum::Output::Outside) { continue; } } - c->ObjectsInFrustum(frustum, outObjects, out == Frustum::Inside); + c->ObjectsInFrustum(frustum, outObjects, out == Frustum::Output::Inside); } } else { size_t startIndex = outObjects.size(); @@ -326,7 +263,7 @@ void OctSpace::Child::ObjectsInFrustum(const Frustum& frustum, std::vector& o outObjects.resize(outObjects.size() + m_StaticObjIndices.size() + m_DynamicObjIndices.size()); for (size_t i = 0; i < m_StaticObjIndices.size(); ++i) { ContainedObject& obj = m_StaticObjectsRef[m_StaticObjIndices[i]]; - if (obj.Checked || !frustum.VsAABB(obj.Box)) { + if (obj.Checked || frustum.VsAABB(*obj.Box) == Frustum::Output::Outside) { ++numDuplicates; } else { obj.Checked = true; @@ -335,7 +272,7 @@ void OctSpace::Child::ObjectsInFrustum(const Frustum& frustum, std::vector& o } for (size_t i = 0; i < m_DynamicObjIndices.size(); ++i) { ContainedObject& obj = m_DynamicObjectsRef[m_DynamicObjIndices[i]]; - if (obj.Checked || !frustum.VsAABB(obj.Box)) { + if (obj.Checked || frustum.VsAABB(*obj.Box) == Frustum::Output::Outside) { ++numDuplicates; } else { obj.Checked = true; diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index e4e6475a..44088913 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -31,9 +31,6 @@ private: const IRenderer* m_Renderer; RenderFrame* m_RenderFrame; Camera* m_Camera; - Camera* m_LastCullCamera; - Camera** m_FrustumCamPtr; - EntityWrapper frustumEntity; World* m_World; EntityWrapper m_CurrentCamera = EntityWrapper::Invalid; EntityWrapper m_LocalPlayer = EntityWrapper::Invalid; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 9d7e7530..26eea7f2 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -1,5 +1,6 @@ #include "Rendering/RenderSystem.h" #include "Collision/Collision.h" +#include "Core/Frustum.h" RenderSystem::RenderSystem(World* world, EventBroker* eventBroker, const IRenderer* renderer, RenderFrame* renderFrame, Octree* frustumCullOctree) : System(world, eventBroker) @@ -13,31 +14,15 @@ RenderSystem::RenderSystem(World* world, EventBroker* eventBroker, const IRender EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &RenderSystem::OnPlayerSpawned); m_Camera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); - m_LastCullCamera = new Camera(*m_Camera); - m_FrustumCamPtr = &m_Camera; } RenderSystem::~RenderSystem() { delete m_Camera; - delete m_LastCullCamera; } bool RenderSystem::OnSetCamera(Events::SetCamera& e) { - //Right now, lets set the camera to cull away stuff if it is connected to a player. - //TODO: This won't work with spectators, or death anim. - if (e.CameraEntity.FirstParentWithComponent("Player").Valid()) { - m_FrustumCamPtr = &m_Camera; - LOG_INFO("Setting frustum to new camera."); - } else if (e.CameraEntity != m_CurrentCamera) { - //If the camera has no parents, i.e. a free camera, - //then we cull from the last camera, so we can see if the culling works. - //Copy the camera into the last frustum camera, without allocating new memory. - new ((void*)m_LastCullCamera) Camera(*m_Camera); - m_FrustumCamPtr = &m_LastCullCamera; - LOG_INFO("New camera, frustum remains at old camera."); - } ComponentWrapper cTransform = e.CameraEntity["Transform"]; ComponentWrapper cCamera = e.CameraEntity["Camera"]; m_Camera->SetFOV((double)cCamera["FOV"]); @@ -58,55 +43,15 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera); } -float frustrumTODO = 0.f; - void RenderSystem::fillModels(std::list>& opaqueJobs, std::list>& transparentJobs) { - if (!frustumEntity.Valid() && m_World->GetComponentPools().size() > 0) { - frustumEntity = EntityWrapper(m_World, m_World->CreateEntity()); - m_World->AttachComponent(frustumEntity.ID, "Transform"); - m_World->AttachComponent(frustumEntity.ID, "Model"); - frustumEntity["Model"]["Resource"] = "Models/Core/UnitCube.mesh"; - } - + Frustum frustum(m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix()); std::vector seenEntities; - //m_Octree->ObjectsInFrustum((*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(), seenEntities); + m_Octree->ObjectsInFrustum(frustum, seenEntities); - glm::mat4x4 viewProj = (*m_FrustumCamPtr)->ProjectionMatrix() * (*m_FrustumCamPtr)->ViewMatrix(); - OctSpace::Frustum frustum; - //Order: Right, left, top, bottom, far, near. - int sign = 1; - for (int i = 0; i < 6; ++i) { - sign = -sign; - int index = i / 2; - OctSpace::Plane& plane = frustum.Planes[i]; - plane.Normal.x = viewProj[0].w + sign * viewProj[0][index]; - plane.Normal.y = viewProj[1].w + sign * viewProj[1][index]; - plane.Normal.z = viewProj[2].w + sign * viewProj[2][index]; - plane.Distance = viewProj[3].w + sign * viewProj[3][index]; - float divByNormalLength = 1.0f / glm::length(plane.Normal); - plane.Normal *= divByNormalLength; - plane.Distance *= divByNormalLength; - } - if (frustumEntity.Valid()) { - int planeI = 0; - glm::vec3 pos = (*m_FrustumCamPtr)->Position() + frustrumTODO * (*m_FrustumCamPtr)->Forward(); - float dist = glm::dot(frustum.Planes[planeI].Normal, pos) + frustum.Planes[planeI].Distance; - frustumEntity["Transform"]["Position"] = pos - dist * frustum.Planes[planeI].Normal; - frustumEntity["Transform"]["Scale"] = glm::vec3(0.15f); - } - if (++frustrumTODO > 75) { - frustrumTODO = 0.f; - } - - //for (auto& seenEntity : seenEntities) { - // EntityWrapper entity = seenEntity.Entity; - // ComponentWrapper cModel = entity["Model"]; - auto models = m_World->GetComponents("Model"); - if (models == nullptr) { - return; - } - for (auto& cModel : *models) { + for (auto& seenEntity : seenEntities) { + EntityWrapper entity = seenEntity.Entity; + ComponentWrapper cModel = entity["Model"]; bool visible = cModel["Visible"]; if (!visible) { continue; @@ -116,8 +61,6 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, continue; } - EntityWrapper entity = EntityWrapper(m_World, cModel.EntityID); - // Only render children of a camera if that camera is currently active if (isChildOfACamera(entity) && !isChildOfCurrentCamera(entity)) { continue; @@ -128,15 +71,6 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, continue; } - if (entity.HasComponent("AABB")) { - OctSpace::Frustum::Output o = frustum.VsAABB(*Collision::EntityAbsoluteAABB(entity)); - if (o == OctSpace::Frustum::Outside && entity != frustumEntity) { - resource = "Models/Core/UnitRaptor.mesh"; - } - } else if (entity != frustumEntity){ - resource = "Models/Core/Error.mesh"; - } - Model* model; try { model = ResourceManager::Load<::Model, true>(resource); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 669e48d0..1124f2e4 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -100,7 +100,7 @@ Game::Game(int argc, char* argv[]) ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision, "Collidable"); m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeTrigger, "Player"); - m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeFrustrumCulling, "Model"); + m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeFrustrumCulling, "Model"); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); From eec59a40c842c17f2386836841ce0a6a1963d0e4 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 17:19:39 +0100 Subject: [PATCH 32/38] Nitpicks. --- include/Engine/Core/Octree.h | 1 - src/Game/Game.cpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Engine/Core/Octree.h b/include/Engine/Core/Octree.h index 49a8b045..6bdea4d3 100644 --- a/include/Engine/Core/Octree.h +++ b/include/Engine/Core/Octree.h @@ -2,7 +2,6 @@ #define Octree_h__ #include -#include #include "../Common.h" #include "AABB.h" diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 1124f2e4..d32eb2ba 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -75,6 +75,7 @@ Game::Game(int argc, char* argv[]) // Create Octrees + // TODO: Perhaps the world bounds should be set in some non-arbitrary way instead of this. AABB boxContainingTheWorld(glm::vec3(-300), glm::vec3(300)); m_OctreeCollision = new Octree(boxContainingTheWorld, 4); m_OctreeTrigger = new Octree(boxContainingTheWorld, 4); From 7140b740b5dc06d0ff122413093d9ba88af09972 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 19:20:04 +0100 Subject: [PATCH 33/38] Models/Collideables/etc. won't need a AABB component anymore, if it has a Model component. In addition, they should take rotation into account, i.e it doesn't have to be axis-aligned. --- include/Engine/Collision/Collision.h | 2 - include/Engine/Rendering/Model.h | 5 +- src/Engine/Collision/Collision.cpp | 71 ++++++++++++----------- src/Engine/Collision/FillOctreeSystem.cpp | 6 -- src/Engine/Collision/TriggerSystem.cpp | 5 -- src/Engine/Rendering/Model.cpp | 9 +++ 6 files changed, 51 insertions(+), 47 deletions(-) diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 6d16e090..6e4858b2 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -78,8 +78,6 @@ bool AABBVsAABB(const AABB& a, const AABB& b); //Also outputs the minimum translation that box [a] would need in order to resolve collision. bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation); -//Attaches an AABB which contains all vertices in the entitys Model. -bool AttachAABBComponentFromModel(EntityWrapper entity); // Calculates an absolute AABB from an entity AABB component boost::optional EntityAbsoluteAABB(EntityWrapper& entity); diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index f751a8cc..1f5231e2 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -4,6 +4,7 @@ #include "Rendering/RawModelCustom.h" //#include "Rendering/RawModelAssimp.h" #include "../OpenGL.h" +#include "Core/AABB.h" class Model : public ThreadUnsafeResource { @@ -17,13 +18,15 @@ public: const std::vector& MaterialGroups() const { return m_RawModel->MaterialGroups; } const glm::mat4& Matrix() const { return m_RawModel->m_Matrix; } const std::vector& Vertices() const { return m_RawModel->m_Vertices; } + const AABB& Box() const { return m_Box; } GLuint VAO; GLuint ElementBuffer; RawModel* m_RawModel; private: - + AABB m_Box; + GLuint VertexBuffer; GLuint NormalBuffer; GLuint TangentNormalsBuffer; diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index b150d667..db2319c7 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -1,4 +1,5 @@ #include +#include #include "Collision/Collision.h" #include "Engine/GLM.h" @@ -564,45 +565,49 @@ bool AABBvsTriangles(const AABB& box, return hit; } -bool AttachAABBComponentFromModel(EntityWrapper entity) -{ - if (!entity.HasComponent("Model")) { - return false; - } - //Derive AABB from model - RawModel* model; - try { - model = ResourceManager::Load(entity["Model"]["Resource"]); - } catch (const std::exception&) { - return false; - } - - glm::vec3 mini(INFINITY); - glm::vec3 maxi(-INFINITY); - for (const auto& v : model->m_Vertices) { - mini = glm::min(mini, v.Position); - maxi = glm::max(maxi, v.Position); - } - - entity.AttachComponent("AABB"); - entity["AABB"]["Origin"] = 0.5f * (maxi + mini); - entity["AABB"]["Size"] = maxi - mini; - return true; -} - boost::optional EntityAbsoluteAABB(EntityWrapper& entity) { - if (!entity.HasComponent("AABB")) { + AABB modelSpaceBox; + if (entity.HasComponent("AABB")) { + ComponentWrapper& cAABB = entity["AABB"]; + modelSpaceBox = EntityAABB::FromOriginSize((glm::vec3)cAABB["Origin"], (glm::vec3)cAABB["Size"]); + } else if (entity.HasComponent("Model")) { + Model* model; + std::string res = entity["Model"]["Resource"]; + if (res.empty()) { + return boost::none; + } + try { + model = ResourceManager::Load<::Model, true>(res); + } catch (const Resource::StillLoadingException&) { + return boost::none; + } catch (const std::exception&) { + return boost::none; + } + modelSpaceBox = model->Box(); + } else { return boost::none; } - ComponentWrapper& cAABB = entity["AABB"]; - glm::vec3 absPosition = Transform::AbsolutePosition(entity.World, entity.ID); - glm::vec3 absScale = Transform::AbsoluteScale(entity.World, entity.ID); - glm::vec3 origin = absPosition + (glm::vec3)cAABB["Origin"]; - glm::vec3 size = (glm::vec3)cAABB["Size"] * absScale; + glm::mat4 modelMat = Transform::AbsoluteTransformation(entity); + glm::vec3 mini(INFINITY); + glm::vec3 maxi(-INFINITY); + glm::vec3 maxCorner = modelSpaceBox.MaxCorner(); + glm::vec3 minCorner = modelSpaceBox.MinCorner(); + for (int i = 0; i < 8; ++i) { + std::bitset<3> bits(i); + glm::vec3 corner; + corner.x = bits.test(0) ? maxCorner.x : minCorner.x; + corner.y = bits.test(1) ? maxCorner.y : minCorner.y; + corner.z = bits.test(2) ? maxCorner.z : minCorner.z; + corner = Transform::TransformPoint(corner, modelMat); + mini = glm::min(mini, corner); + maxi = glm::max(maxi, corner); + } + + EntityAABB aabb; + aabb = AABB(mini, maxi); - EntityAABB aabb = EntityAABB::FromOriginSize(origin, size); aabb.Entity = entity; return aabb; diff --git a/src/Engine/Collision/FillOctreeSystem.cpp b/src/Engine/Collision/FillOctreeSystem.cpp index 8c03b5c2..a727eb86 100644 --- a/src/Engine/Collision/FillOctreeSystem.cpp +++ b/src/Engine/Collision/FillOctreeSystem.cpp @@ -7,12 +7,6 @@ void FillOctreeSystem::Update(double dt) void FillOctreeSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt) { - if (!entity.HasComponent("AABB")) { - //Derive AABB from model. - if (!Collision::AttachAABBComponentFromModel(entity)) { - return; - } - } boost::optional absoluteAABB = Collision::EntityAbsoluteAABB(entity); if (absoluteAABB) { m_Octree->AddDynamicObject(*absoluteAABB); diff --git a/src/Engine/Collision/TriggerSystem.cpp b/src/Engine/Collision/TriggerSystem.cpp index adc778e5..21a47721 100644 --- a/src/Engine/Collision/TriggerSystem.cpp +++ b/src/Engine/Collision/TriggerSystem.cpp @@ -5,11 +5,6 @@ void TriggerSystem::UpdateComponent(EntityWrapper& triggerEntity, ComponentWrapper& cTrigger, double dt) { - // The trigger *should* have a bounding box, or something, to test against so it can be triggered. - // If it doesn't, add one as big as the model for now, then size can be modified in editor if necessary. - if (!triggerEntity.HasComponent("AABB")) { - Collision::AttachAABBComponentFromModel(triggerEntity); - } boost::optional triggerBox = Collision::EntityAbsoluteAABB(triggerEntity); if (!triggerBox) { return; diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index cf4923a3..a61cf8e3 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -64,6 +64,15 @@ Model::Model(std::string fileName) GLERROR("GLEW: BufferFail5"); //CreateBuffers(); + + glm::vec3 mini(INFINITY); + glm::vec3 maxi(-INFINITY); + for (const auto& v : m_RawModel->m_Vertices) { + mini = glm::min(mini, v.Position); + maxi = glm::max(maxi, v.Position); + } + + m_Box = AABB(maxi, mini); } Model::~Model() From a93ccecfeb41bcdaccfffd0e6b4d9a52f6e83c6d Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 19:23:12 +0100 Subject: [PATCH 34/38] Debug test code. --- src/Engine/Collision/Collision.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index db2319c7..a20ae4d3 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -568,10 +568,10 @@ bool AABBvsTriangles(const AABB& box, boost::optional EntityAbsoluteAABB(EntityWrapper& entity) { AABB modelSpaceBox; - if (entity.HasComponent("AABB")) { + /*if (entity.HasComponent("AABB")) { ComponentWrapper& cAABB = entity["AABB"]; modelSpaceBox = EntityAABB::FromOriginSize((glm::vec3)cAABB["Origin"], (glm::vec3)cAABB["Size"]); - } else if (entity.HasComponent("Model")) { + } else */if (entity.HasComponent("Model")) { Model* model; std::string res = entity["Model"]["Resource"]; if (res.empty()) { @@ -584,6 +584,7 @@ boost::optional EntityAbsoluteAABB(EntityWrapper& entity) } catch (const std::exception&) { return boost::none; } + entity.AttachComponent("AABB"); modelSpaceBox = model->Box(); } else { return boost::none; @@ -609,7 +610,8 @@ boost::optional EntityAbsoluteAABB(EntityWrapper& entity) aabb = AABB(mini, maxi); aabb.Entity = entity; - + (glm::vec3&)entity["AABB"]["Origin"] = aabb.Origin(); + (glm::vec3&)entity["AABB"]["Size"] = aabb.Size(); return aabb; } From ecfacee5876ad73a5fc4daa1746914390b2db8ae Mon Sep 17 00:00:00 2001 From: William Moberg Date: Tue, 9 Feb 2016 19:33:28 +0100 Subject: [PATCH 35/38] Undo debug test code. --- src/Engine/Collision/Collision.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index a20ae4d3..c0c27190 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -568,10 +568,10 @@ bool AABBvsTriangles(const AABB& box, boost::optional EntityAbsoluteAABB(EntityWrapper& entity) { AABB modelSpaceBox; - /*if (entity.HasComponent("AABB")) { + if (entity.HasComponent("AABB")) { ComponentWrapper& cAABB = entity["AABB"]; modelSpaceBox = EntityAABB::FromOriginSize((glm::vec3)cAABB["Origin"], (glm::vec3)cAABB["Size"]); - } else */if (entity.HasComponent("Model")) { + } else if (entity.HasComponent("Model")) { Model* model; std::string res = entity["Model"]["Resource"]; if (res.empty()) { @@ -584,7 +584,6 @@ boost::optional EntityAbsoluteAABB(EntityWrapper& entity) } catch (const std::exception&) { return boost::none; } - entity.AttachComponent("AABB"); modelSpaceBox = model->Box(); } else { return boost::none; @@ -610,8 +609,6 @@ boost::optional EntityAbsoluteAABB(EntityWrapper& entity) aabb = AABB(mini, maxi); aabb.Entity = entity; - (glm::vec3&)entity["AABB"]["Origin"] = aabb.Origin(); - (glm::vec3&)entity["AABB"]["Size"] = aabb.Size(); return aabb; } From d65ce805a502f82b45e92e116148b794442ce6a8 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 10 Feb 2016 10:53:25 +0100 Subject: [PATCH 36/38] FrustumOctree has its own system, won't cull away explosion effects. --- .../Collision/FillFrustumOctreeSystem.h | 25 +++++++++++++++++++ include/Engine/Collision/FillOctreeSystem.h | 4 +-- .../Collision/FillFrustumOctreeSystem.cpp | 21 ++++++++++++++++ src/Game/Game.cpp | 3 ++- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 include/Engine/Collision/FillFrustumOctreeSystem.h create mode 100644 src/Engine/Collision/FillFrustumOctreeSystem.cpp diff --git a/include/Engine/Collision/FillFrustumOctreeSystem.h b/include/Engine/Collision/FillFrustumOctreeSystem.h new file mode 100644 index 00000000..f7f1413c --- /dev/null +++ b/include/Engine/Collision/FillFrustumOctreeSystem.h @@ -0,0 +1,25 @@ +#ifndef FillFrustumOctreeSystem_h__ +#define FillFrustumOctreeSystem_h__ + +#include "../Core/System.h" +#include "../Core/Octree.h" +#include "Collision.h" +#include "EntityAABB.h" + +class FillFrustumOctreeSystem : public ImpureSystem, public PureSystem +{ +public: + FillFrustumOctreeSystem(World* world, EventBroker* eventBroker, Octree* octree) + : System(world, eventBroker) + , PureSystem("Model") + , m_Octree(octree) + { } + + virtual void Update(double dt) override; + virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt) override; + +private: + Octree* m_Octree; +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Collision/FillOctreeSystem.h b/include/Engine/Collision/FillOctreeSystem.h index 9fd87b94..3bacccac 100644 --- a/include/Engine/Collision/FillOctreeSystem.h +++ b/include/Engine/Collision/FillOctreeSystem.h @@ -1,5 +1,5 @@ -#ifndef CollidableOctreeSystem_h__ -#define CollidableOctreeSystem_h__ +#ifndef FillOctreeSystem_h__ +#define FillOctreeSystem_h__ #include "../Core/System.h" #include "../Core/Octree.h" diff --git a/src/Engine/Collision/FillFrustumOctreeSystem.cpp b/src/Engine/Collision/FillFrustumOctreeSystem.cpp new file mode 100644 index 00000000..f02a30f1 --- /dev/null +++ b/src/Engine/Collision/FillFrustumOctreeSystem.cpp @@ -0,0 +1,21 @@ +#include "Collision/FillFrustumOctreeSystem.h" + +void FillFrustumOctreeSystem::Update(double dt) +{ + m_Octree->ClearDynamicObjects(); +} + +void FillFrustumOctreeSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt) +{ + if (entity.HasComponent("ExplosionEffect")) { + //TODO: Fix hack, get real box by using shader equation. + EntityAABB aabb = AABB(glm::vec3(-300), glm::vec3(300)); + aabb.Entity = entity; + m_Octree->AddDynamicObject(aabb); + } else { + boost::optional absoluteAABB = Collision::EntityAbsoluteAABB(entity); + if (absoluteAABB) { + m_Octree->AddDynamicObject(*absoluteAABB); + } + } +} \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index d32eb2ba..0f836c6f 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -1,5 +1,6 @@ #include "Game.h" #include "Collision/FillOctreeSystem.h" +#include "Collision/FillFrustumOctreeSystem.h" #include "Collision/EntityAABB.h" #include "Collision/TriggerSystem.h" #include "Collision/CollisionSystem.h" @@ -101,7 +102,7 @@ Game::Game(int argc, char* argv[]) ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision, "Collidable"); m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeTrigger, "Player"); - m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeFrustrumCulling, "Model"); + m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeFrustrumCulling); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); From 94ff12cf66e800af9c3f073e9c6031d2eedb3736 Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 10 Feb 2016 11:04:18 +0100 Subject: [PATCH 37/38] When using a PlaySoundOnEntity Event, you no longer have to provide the child of an entity as parameter. --- include/Engine/Sound/SoundManager.h | 2 +- include/Game/Systems/SoundSystem.h | 3 --- src/Engine/Sound/SoundManager.cpp | 7 +++++-- src/Game/Systems/PlayerMovementSystem.cpp | 5 +---- src/Game/Systems/SoundSystem.cpp | 24 ++++++++--------------- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index 38ec62ff..5b027c62 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -94,7 +94,7 @@ private: float m_BGMVolumeChannel = 1.0f; float m_SFXVolumeChannel = 1.0f; EntityWrapper m_LocalPlayer = EntityWrapper(); - + // Events EventRelay m_EPlaySoundOnEntity; bool OnPlaySoundOnEntity(const Events::PlaySoundOnEntity &e); diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index 0f7f5102..ed30a3c4 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -36,11 +36,8 @@ private: World* m_World = nullptr; EventBroker* m_EventBroker = nullptr; std::string m_Announcer = ""; - // Logic for playing a sound when a player jumps void playerJumps(); - // Helper function - EntityID createChildEmitter(EntityWrapper parent); std::default_random_engine generator; diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 8ba6a502..8e103d5c 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -192,7 +192,10 @@ bool SoundManager::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) { Source* source = createSource(e.FilePath); source->Type = SoundType::SFX; - m_Sources[e.EmitterID] = source; + EntityID child = m_World->CreateEntity(e.EmitterID); + m_World->AttachComponent(child, "Transform"); + m_World->AttachComponent(child, "SoundEmitter"); + m_Sources[child] = source; playSound(source); return false; } @@ -243,7 +246,7 @@ bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e) } auto emitterChild = m_World->CreateEntity((*it).EntityID); auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter"); - (bool&)emitter["Loop"] = false; + (bool&)emitter["Loop"] = true; (std::string&)emitter["FilePath"] = e.FilePath; m_World->AttachComponent(emitterChild, "Transform"); Source* source = createSource(e.FilePath); diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index fca52dfd..34d8c689 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -197,10 +197,7 @@ void PlayerMovementSystem::playerStep(double dt) // Player moved a step's distance // Create footstep sound Events::PlaySoundOnEntity e; - EntityID child = m_World->CreateEntity(m_LocalPlayer.ID); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - e.EmitterID = child; + e.EmitterID = m_LocalPlayer.ID; 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 06e7d67a..0f35d617 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -31,7 +31,7 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) m_World->AttachComponent(e.Player.ID, "Listener"); m_LocalPlayer = e.Player; Events::PlaySoundOnEntity go; - go.EmitterID = createChildEmitter(m_LocalPlayer); + go.EmitterID = m_LocalPlayer.ID; go.FilePath = "Audio/announcer/" + m_Announcer + "/go.wav"; m_EventBroker->Publish(go); // TEMP: starts bgm @@ -67,7 +67,7 @@ void SoundSystem::playerJumps() glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"]; if (vel.y == 0) { Events::PlaySoundOnEntity e; - e.EmitterID = createChildEmitter(m_LocalPlayer); + e.EmitterID = m_LocalPlayer.ID; e.FilePath = "Audio/jump/jump1.wav"; m_EventBroker->Publish(e); } @@ -76,7 +76,7 @@ void SoundSystem::playerJumps() bool SoundSystem::OnShoot(const Events::Shoot & e) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/laser/laser1.wav"; m_EventBroker->Publish(ev); return true; @@ -92,7 +92,7 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) } else { ev.FilePath = "Audio/announcer/" + m_Announcer + "/objective_failed.wav"; // have not been tested } - ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.EmitterID = m_LocalPlayer.ID; m_EventBroker->Publish(ev); return false; } @@ -122,7 +122,7 @@ bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) { //if (e.PlayerID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/die/die2.wav"; m_EventBroker->Publish(ev); //} @@ -133,7 +133,7 @@ bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) { //if (e.PlayerHealedID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/pickup/pickup2.wav"; m_EventBroker->Publish(ev); //} @@ -153,7 +153,7 @@ bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) bool SoundSystem::OnDoubleJump(const Events::DoubleJump & e) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/jump/jump2.wav"; m_EventBroker->Publish(ev); return false; @@ -162,16 +162,8 @@ bool SoundSystem::OnDoubleJump(const Events::DoubleJump & e) bool SoundSystem::OnDashAbility(const Events::DashAbility &e) { Events::PlaySoundOnEntity ev; - ev.EmitterID = createChildEmitter(m_LocalPlayer); + ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/jump/dash1.wav"; m_EventBroker->Publish(ev); return false; -} - -EntityID SoundSystem::createChildEmitter(EntityWrapper localPlayer) -{ - EntityID child = m_World->CreateEntity(localPlayer.ID); - m_World->AttachComponent(child, "Transform"); - m_World->AttachComponent(child, "SoundEmitter"); - return child; } \ No newline at end of file From 3d2f9fa79d4a9a4362b73206ea492e7bcc808023 Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 10 Feb 2016 11:36:54 +0100 Subject: [PATCH 38/38] Drum sounds does not start every single time a TriggerTouch happens. Specific and temporary logic for play test. --- include/Game/Systems/SoundSystem.h | 5 ++++ src/Game/Systems/SoundSystem.cpp | 43 ++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/Game/Systems/SoundSystem.h b/include/Game/Systems/SoundSystem.h index ed30a3c4..09582826 100644 --- a/include/Game/Systems/SoundSystem.h +++ b/include/Game/Systems/SoundSystem.h @@ -39,6 +39,11 @@ private: // Logic for playing a sound when a player jumps void playerJumps(); + // Temporary solution for play test. + bool m_DrumsIsPlaying = false; + double m_DrumTimer = 0.0; + bool drumTimer(double dt); + std::default_random_engine generator; EventRelay m_EPlayerSpawned; diff --git a/src/Game/Systems/SoundSystem.cpp b/src/Game/Systems/SoundSystem.cpp index 0f35d617..df14cd86 100644 --- a/src/Game/Systems/SoundSystem.cpp +++ b/src/Game/Systems/SoundSystem.cpp @@ -23,7 +23,12 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp { } void SoundSystem::Update(double dt) -{ } +{ + // Temp for play test. + if(m_DrumsIsPlaying) { + m_DrumsIsPlaying = !drumTimer(dt); + } +} bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e) { @@ -73,6 +78,17 @@ void SoundSystem::playerJumps() } } +bool SoundSystem::drumTimer(double dt) +{ + m_DrumTimer += dt; + if (m_DrumTimer > 15) { + m_DrumTimer = 0.0; + return true; + } else { + return false; + } +} + bool SoundSystem::OnShoot(const Events::Shoot & e) { Events::PlaySoundOnEntity ev; @@ -94,6 +110,8 @@ bool SoundSystem::OnCaptured(const Events::Captured & e) } ev.EmitterID = m_LocalPlayer.ID; m_EventBroker->Publish(ev); + // Temp for play test. + m_DrumsIsPlaying = false; return false; } @@ -106,11 +124,11 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) std::vector paths; paths.push_back("Audio/hurt/hurt" + std::to_string(rand) + ".wav"); - // Breathe - int ammountOfbreaths = (static_cast(e.Damage) / 10) + 2; // TEMP: Idk something stupid like this shit - for (int i = 0; i < ammountOfbreaths; i++) { - paths.push_back("Audio/exhausted/breath.wav"); - } +// // Breathe +// int ammountOfbreaths = (static_cast(e.Damage) / 10) + 2; // TEMP: Idk something stupid like this shit +// for (int i = 0; i < ammountOfbreaths; i++) { +// paths.push_back("Audio/exhausted/breath.wav"); +// } Events::PlayQueueOnEntity ev; ev.Emitter = m_LocalPlayer; ev.FilePaths = paths; @@ -120,32 +138,35 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e) bool SoundSystem::OnPlayerDeath(const Events::PlayerDeath & e) { - //if (e.PlayerID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/die/die2.wav"; m_EventBroker->Publish(ev); - //} return false; } bool SoundSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup & e) { - //if (e.PlayerHealedID == m_LocalPlayer.ID) { Events::PlaySoundOnEntity ev; ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/pickup/pickup2.wav"; m_EventBroker->Publish(ev); - //} return false; } bool SoundSystem::OnTriggerTouch(const Events::TriggerTouch & e) { + // Temp for play test. + if (m_DrumsIsPlaying) { + return false; + } if (m_World->HasComponent(e.Trigger.ID, "CapturePoint")) { - Events::PlayBackgroundMusic ev; + Events::PlaySoundOnEntity ev; // should be BGM + ev.EmitterID = m_LocalPlayer.ID; ev.FilePath = "Audio/bgm/drumstest.wav"; m_EventBroker->Publish(ev); + // Temp for play test. + m_DrumsIsPlaying = true; } return false; }