Tested hurt event. Randomizes between several different ones.
This commit is contained in:
+1
-1
Submodule assets updated: 9b4f9ab2e6...5bf03ccd54
@@ -2,6 +2,7 @@
|
||||
#define SoundSystem_h__
|
||||
|
||||
#include <unordered_map>
|
||||
#include <random>
|
||||
|
||||
#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<SoundSystem, Events::PlaySoundOnEntity> m_EPlaySoundOnEntity;
|
||||
@@ -129,6 +132,8 @@ private:
|
||||
bool OnPlayerDeath(const Events::PlayerDeath &e);
|
||||
EventRelay<SoundSystem, Events::PlayerHealthPickup> m_EPlayerHealthPickup;
|
||||
bool OnPlayerHealthPickup(const Events::PlayerHealthPickup &e);
|
||||
EventRelay<SoundSystem, Events::ComponentAttached> m_EComponentAttached;
|
||||
bool OnComponentAttached(const Events::ComponentAttached &e);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<int> 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()
|
||||
|
||||
+2
-1
@@ -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<SystemPipeline>();
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user