diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index ba47e23..d70ddc6 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -19,6 +19,7 @@ void GameWorld::Initialize() std::shared_ptr camera; std::shared_ptr bounds; std::shared_ptr collision; + std::shared_ptr soundEmitter; EntityID ent; // Camera @@ -103,6 +104,14 @@ void GameWorld::Initialize() bounds = AddComponent(m_Player, "Bounds"); bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3); bounds->VolumeVector = glm::vec3(4.f,0.7f,4); + soundEmitter = AddComponent(m_Player, "SoundEmitter"); + soundEmitter->Loop = true; + soundEmitter->MaxDistance = FLT_MAX; + soundEmitter->ReferenceDistance = 10; + soundEmitter->Gain = 1; + soundEmitter->Pitch = 1; + soundEmitter->Path = "Sounds/hallelujah.wav"; + GetSystem("SoundSystem")->PlaySound(soundEmitter); ent = CreateEntity(); transform = AddComponent(ent, "Transform"); diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index 8cd9c53..1bdd10a 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -12,7 +12,6 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID transform->Position[1] += parentTransform->Position[1]; transform->Position[2] += parentTransform->Position[2]; } - LOG_INFO("Updating entity %i with parent %i", entity, parent); auto entities = m_World->GetEntities(); diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 4a27d0a..a0396a6 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -20,7 +20,9 @@ public: void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentCreated(std::string type, std::shared_ptr component) override; - void PlaySound(std::shared_ptr emitter, std::string fileName); + void PlaySound(std::shared_ptr emitter, std::string path); // Use if you want to play a temporary .wav file not from component + void PlaySound(std::shared_ptr emitter); // Use if you want to play .wav file from component // imon no hate plx T.T + void StopSound(std::shared_ptr emitter); private: ALuint LoadFile(std::string fileName); diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index 88789c7..2073a64 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -72,14 +72,27 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par } } -void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string fileName) +void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string path) { - ALuint buffer = LoadFile(fileName); + ALuint buffer = LoadFile(path); ALuint source = m_Source[emitter.get()]; alSourcei(source, AL_BUFFER, buffer); alSourcePlay(m_Source[emitter.get()]); } +void Systems::SoundSystem::PlaySound(std::shared_ptr emitter) +{ + ALuint buffer = LoadFile(emitter->Path); + ALuint source = m_Source[emitter.get()]; + alSourcei(source, AL_BUFFER, buffer); + alSourcePlay(m_Source[emitter.get()]); +} + +void Systems::SoundSystem::StopSound(std::shared_ptr emitter) +{ + alSourceStop(m_Source[emitter.get()]); +} + void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) { if(type == "SoundEmitter") { @@ -88,13 +101,13 @@ void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr< } } -ALuint Systems::SoundSystem::LoadFile(std::string fileName) +ALuint Systems::SoundSystem::LoadFile(std::string path) { - if (m_BufferCache.find(fileName) != m_BufferCache.end()) - return m_BufferCache[fileName]; + if (m_BufferCache.find(path) != m_BufferCache.end()) + return m_BufferCache[path]; FILE *fp = NULL; - fp = fopen(fileName.c_str(), "rb"); + fp = fopen(path.c_str(), "rb"); //CHECK FOR VALID WAVE-FILE fread(type, sizeof(char), 4, fp); @@ -160,7 +173,7 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName) alBufferData(buffer, format, buf, dataSize, sampleRate); delete[] buf; - m_BufferCache[fileName] = buffer; + m_BufferCache[path] = buffer; return buffer; } @@ -169,5 +182,8 @@ ALuint Systems::SoundSystem::CreateSource() ALuint source; alGenSources((ALuint)1, &source); + alDopplerFactor(2.f); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect + alDopplerVelocity(350.f); // Defines the velocity of the sound + return source; } \ No newline at end of file