From becb197feb237fb034f3a4a8d1c670b176f09e49 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 5 Feb 2016 17:46:27 +0100 Subject: [PATCH] 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); + } +}