This commit is contained in:
stiffly
2016-03-08 16:55:35 +01:00
parent ec3ed1f46f
commit e0de91cce3
2 changed files with 23 additions and 17 deletions
+4 -2
View File
@@ -2,6 +2,7 @@
#define Systems_SoundSystem_h__ #define Systems_SoundSystem_h__
#include <random> #include <random>
#include <chrono>
#include "../Engine/Core/System.h" #include "../Engine/Core/System.h"
#include "../Engine/Core/ResourceManager.h" #include "../Engine/Core/ResourceManager.h"
@@ -34,9 +35,10 @@ public:
private: private:
std::string m_Announcer = ""; std::string m_Announcer = "";
// Logic for playing a sound when a player jumps // Logic for playing a sound when a player jumps
void playerJumps(); void playerJumps(EntityWrapper player);
std::default_random_engine generator; std::default_random_engine m_RandomGenerator;
std::uniform_int_distribution<int> m_RandIntDistribution;
EventRelay<SoundSystem, Events::PlayerSpawned> m_EPlayerSpawned; EventRelay<SoundSystem, Events::PlayerSpawned> m_EPlayerSpawned;
bool OnPlayerSpawned(const Events::PlayerSpawned &e); bool OnPlayerSpawned(const Events::PlayerSpawned &e);
+17 -13
View File
@@ -6,14 +6,15 @@ SoundSystem::SoundSystem(SystemParams params)
{ {
ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini"); ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini");
m_Announcer = ResourceManager::Load<ConfigFile>("Config.ini")->Get<std::string>("Sound.Announcer", "female"); m_Announcer = ResourceManager::Load<ConfigFile>("Config.ini")->Get<std::string>("Sound.Announcer", "female");
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
m_RandomGenerator = std::default_random_engine(seed);
m_RandIntDistribution = std::uniform_int_distribution<int>(1, 12);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundSystem::OnPlayerSpawned);
EVENT_SUBSCRIBE_MEMBER(m_InputCommand, &SoundSystem::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_InputCommand, &SoundSystem::OnInputCommand);
EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &SoundSystem::OnDoubleJump); EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &SoundSystem::OnDoubleJump);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage); EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &SoundSystem::OnPlayerDamage);
EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured); EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &SoundSystem::OnCaptured);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDeath, &SoundSystem::OnPlayerDeath); EVENT_SUBSCRIBE_MEMBER(m_EPlayerDeath, &SoundSystem::OnPlayerDeath);
} }
void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt)
@@ -42,24 +43,28 @@ bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e)
bool SoundSystem::OnInputCommand(const Events::InputCommand & e) bool SoundSystem::OnInputCommand(const Events::InputCommand & e)
{ {
if (e.Command == "Jump" && e.Value > 0) { if (e.Command == "Jump" && e.Value > 0) {
if (e.PlayerID == -1) { // local player if (e.PlayerID == -1) {
playerJumps(); playerJumps(LocalPlayer);
return true; } else {
playerJumps(e.Player);
} }
return true;
} }
return false; return false;
} }
void SoundSystem::playerJumps() void SoundSystem::playerJumps(EntityWrapper player)
{ {
if (!IsClient) { // Only play for clients if (!IsClient) { // Only play for clients
return; return;
} }
if(!player.HasComponent("Physics")) {
bool grounded = (bool)m_World->GetComponent(LocalPlayer.ID, "Physics")["IsOnGround"]; return;
}
bool grounded = (bool)player["Physics"]["IsOnGround"];
if (grounded) { if (grounded) {
Events::PlaySoundOnEntity e; Events::PlaySoundOnEntity e;
e.Emitter = LocalPlayer; e.Emitter = player;
e.FilePath = "Audio/Jump/Jump1.wav"; e.FilePath = "Audio/Jump/Jump1.wav";
m_EventBroker->Publish(e); m_EventBroker->Publish(e);
} }
@@ -88,13 +93,12 @@ bool SoundSystem::OnPlayerDamage(const Events::PlayerDamage & e)
if (!IsClient) { // Only play for clients if (!IsClient) { // Only play for clients
return false; return false;
} }
if(!e.Victim.Valid()) { if (!e.Victim.Valid()) {
return false; return false;
} }
std::uniform_int_distribution<int> dist(1, 12);
int rand = dist(generator);
std::vector<std::string> paths; std::vector<std::string> paths;
paths.push_back("Audio/Hurt/Hurt" + std::to_string(rand) + ".wav"); paths.push_back("Audio/Hurt/Hurt" + std::to_string(m_RandIntDistribution(m_RandomGenerator)) + ".wav");
Events::PlayQueueOnEntity ev; Events::PlayQueueOnEntity ev;
ev.Emitter = e.Victim; ev.Emitter = e.Victim;