From cb0feb0b84acd312f5ea8a145819e7de6ef702c7 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 2 Feb 2016 13:25:25 +0100 Subject: [PATCH] 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();