diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index 8e471d5..d325dac 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -11,7 +11,7 @@ namespace Components struct SoundEmitter : Component { float Gain; - float MaxDistance; + //float MaxDistance; float ReferenceDistance; float Pitch; bool Loop; diff --git a/Escape-the-Dawn/Sounds/hum.wav b/Escape-the-Dawn/Sounds/hum.wav new file mode 100644 index 0000000..30601bf Binary files /dev/null and b/Escape-the-Dawn/Sounds/hum.wav differ diff --git a/Escape-the-Dawn/Sounds/siren.wav b/Escape-the-Dawn/Sounds/siren.wav new file mode 100644 index 0000000..54a0238 Binary files /dev/null and b/Escape-the-Dawn/Sounds/siren.wav differ diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp index b2cd7f3..4fa35da 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp @@ -10,7 +10,6 @@ Systems::LevelGenerationSystem::LevelGenerationSystem( World* world ) void Systems::LevelGenerationSystem::SpawnObstacle() { typeRandom = 0 + (rand() % 3); - EntityID ent; @@ -21,37 +20,43 @@ void Systems::LevelGenerationSystem::SpawnObstacle() bounds = m_World->AddComponent(ent, "Bounds"); collision = m_World->AddComponent(ent, "Collision"); model = m_World->AddComponent(ent, "Model"); + auto sound = m_World->AddComponent(ent, "SoundEmitter"); //pointLight = m_World->AddComponent(ent, "PointLight"); positionRandom = -500 + (rand() % 1000); + transform->Position = glm::vec3(positionRandom, startyz); // fix position + transform->Velocity = glm::vec3(0.f, 10.f, 100.f); + + sound->Loop = true; + sound->Gain = 1.f; + sound->ReferenceDistance = 15.f; + m_World->GetSystem("SoundSystem")->PlaySound(sound, "Sounds/hum.wav"); switch (typeRandom) { case 0: - transform->Position = glm::vec3( positionRandom, startyz); // fix position bounds->VolumeVector = glm::vec3(9, 12, 7); bounds->Origin = glm::vec3(1,11,-1); model->ModelFile = "Models/obstacle_mountain_1.obj"; break; case 1: - transform->Position = glm::vec3( positionRandom, startyz); // fix position bounds->VolumeVector = glm::vec3(3.5f, 7.5f, 3.5f); bounds->Origin = glm::vec3(0,7.5f,0); model->ModelFile = "Models/obstacle_mountain_2.obj"; break; case 2: - transform->Position = glm::vec3( positionRandom, startyz); // fix position bounds->VolumeVector = glm::vec3(1, 1, 1); bounds->Origin = glm::vec3(0,1,0); -// pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); -// pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0); -// pointLight->Diffuse = glm::vec3(0.0, 3.0, 0.0); -// pointLight->constantAttenuation = 0.f; -// pointLight->linearAttenuation = 1.f; -// pointLight->quadraticAttenuation = 0.f; -// pointLight->spotExponent = 0.0f; + + //pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); + //pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0); + //pointLight->Diffuse = glm::vec3(0.0, 3.0, 0.0); + //pointLight->constantAttenuation = 0.f; + //pointLight->linearAttenuation = 1.f; + //pointLight->quadraticAttenuation = 0.f; + //pointLight->spotExponent = 0.0f; model->ModelFile = "Models/obstacle_cube_1.obj"; break; @@ -59,6 +64,9 @@ void Systems::LevelGenerationSystem::SpawnObstacle() break; } + + // Put it below ground level + transform->Position.y -= bounds->VolumeVector.y * 2.f; } @@ -76,10 +84,16 @@ void Systems::LevelGenerationSystem::Update( double dt ) for(auto ent : obstacles) { - auto transformComponent = m_World->GetComponent(ent, "Transform"); - transformComponent->Position.z += 100.f * dt; + auto transform = m_World->GetComponent(ent, "Transform"); + transform->Position += transform->Velocity * (float)dt; - if(transformComponent->Position.z > 100) + // Stop raising obstacles when they reach ground level + if (transform->Velocity.y > 0 && transform->Position.y >= 0) { + transform->Position.y = 0; + transform->Velocity.y = 0; + } + + if(transform->Position.z > 800) { removethis.push_back(ent); } diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.h b/Escape-the-Dawn/Systems/LevelGenerationSystem.h index e436df1..1020d3c 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.h +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.h @@ -2,12 +2,14 @@ #define LevelGenerationSystem_h__ #include "System.h" +#include "Systems/SoundSystem.h" #include "World.h" #include "Components/Transform.h" #include "Components/Bounds.h" #include "Components/Collision.h" #include "Components/Model.h" #include "Components/PointLight.h" +#include "Components/SoundEmitter.h" namespace Systems diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 4a27d0a..f8b97eb 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -20,6 +20,7 @@ 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 OnComponentRemoved(std::string type, Component* component) override; void PlaySound(std::shared_ptr emitter, std::string fileName); private: @@ -35,7 +36,7 @@ private: unsigned long dataSize; - std::map m_Source; + std::map m_Sources; std::map m_BufferCache; // string = fileName }; diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index 6ab5d0a..673a4e6 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -18,6 +18,9 @@ Systems::SoundSystem::SoundSystem(World* world) } alGetError(); + + alSpeedOfSound(340.29f); // Speed of sound + alDistanceModel(AL_INVERSE_DISTANCE); } void Systems::SoundSystem::Update(double dt) @@ -54,10 +57,10 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); if(soundEmitter != nullptr) { - ALuint source = m_Source[soundEmitter]; - alSourcei(source, AL_GAIN, soundEmitter->Gain); - alSourcei(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); - alSourcei(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); + ALuint source = m_Sources[soundEmitter]; + alSourcef(source, AL_GAIN, soundEmitter->Gain); + //alSourcef(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); + alSourcef(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); alSourcef(source, AL_PITCH, soundEmitter->Pitch); alSourcei(source, AL_LOOPING, soundEmitter->Loop); @@ -75,16 +78,26 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string fileName) { ALuint buffer = LoadFile(fileName); - ALuint source = m_Source[emitter.get()]; + ALuint source = m_Sources[emitter.get()]; alSourcei(source, AL_BUFFER, buffer); - alSourcePlay(m_Source[emitter.get()]); + alSourcePlay(m_Sources[emitter.get()]); } void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) { if(type == "SoundEmitter") { ALuint source = CreateSource(); - m_Source[component.get()] = source; + m_Sources[component.get()] = source; + } +} + +void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* component) +{ + if(type == "SoundEmitter") { + if (m_Sources.find(component) != m_Sources.end()) { + ALuint source = m_Sources[component]; + alDeleteSources(1, &source); + } } }