Fancy sounds

This commit is contained in:
2014-03-13 16:28:39 +01:00
parent b34d2d3dd0
commit a711ea7db8
7 changed files with 53 additions and 23 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ namespace Components
struct SoundEmitter : Component struct SoundEmitter : Component
{ {
float Gain; float Gain;
float MaxDistance; //float MaxDistance;
float ReferenceDistance; float ReferenceDistance;
float Pitch; float Pitch;
bool Loop; bool Loop;
Binary file not shown.
Binary file not shown.
@@ -11,7 +11,6 @@ void Systems::LevelGenerationSystem::SpawnObstacle()
{ {
typeRandom = 0 + (rand() % 3); typeRandom = 0 + (rand() % 3);
EntityID ent; EntityID ent;
ent = m_World->CreateEntity(); ent = m_World->CreateEntity();
@@ -21,37 +20,43 @@ void Systems::LevelGenerationSystem::SpawnObstacle()
bounds = m_World->AddComponent<Components::Bounds>(ent, "Bounds"); bounds = m_World->AddComponent<Components::Bounds>(ent, "Bounds");
collision = m_World->AddComponent<Components::Collision>(ent, "Collision"); collision = m_World->AddComponent<Components::Collision>(ent, "Collision");
model = m_World->AddComponent<Components::Model>(ent, "Model"); model = m_World->AddComponent<Components::Model>(ent, "Model");
auto sound = m_World->AddComponent<Components::SoundEmitter>(ent, "SoundEmitter");
//pointLight = m_World->AddComponent<Components::PointLight>(ent, "PointLight"); //pointLight = m_World->AddComponent<Components::PointLight>(ent, "PointLight");
positionRandom = -500 + (rand() % 1000); 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<Systems::SoundSystem>("SoundSystem")->PlaySound(sound, "Sounds/hum.wav");
switch (typeRandom) switch (typeRandom)
{ {
case 0: case 0:
transform->Position = glm::vec3( positionRandom, startyz); // fix position
bounds->VolumeVector = glm::vec3(9, 12, 7); bounds->VolumeVector = glm::vec3(9, 12, 7);
bounds->Origin = glm::vec3(1,11,-1); bounds->Origin = glm::vec3(1,11,-1);
model->ModelFile = "Models/obstacle_mountain_1.obj"; model->ModelFile = "Models/obstacle_mountain_1.obj";
break; break;
case 1: case 1:
transform->Position = glm::vec3( positionRandom, startyz); // fix position
bounds->VolumeVector = glm::vec3(3.5f, 7.5f, 3.5f); bounds->VolumeVector = glm::vec3(3.5f, 7.5f, 3.5f);
bounds->Origin = glm::vec3(0,7.5f,0); bounds->Origin = glm::vec3(0,7.5f,0);
model->ModelFile = "Models/obstacle_mountain_2.obj"; model->ModelFile = "Models/obstacle_mountain_2.obj";
break; break;
case 2: case 2:
transform->Position = glm::vec3( positionRandom, startyz); // fix position
bounds->VolumeVector = glm::vec3(1, 1, 1); bounds->VolumeVector = glm::vec3(1, 1, 1);
bounds->Origin = glm::vec3(0,1,0); 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->Specular = glm::vec3(1.0, 1.0, 1.0);
// pointLight->Diffuse = glm::vec3(0.0, 3.0, 0.0); //pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0);
// pointLight->constantAttenuation = 0.f; //pointLight->Diffuse = glm::vec3(0.0, 3.0, 0.0);
// pointLight->linearAttenuation = 1.f; //pointLight->constantAttenuation = 0.f;
// pointLight->quadraticAttenuation = 0.f; //pointLight->linearAttenuation = 1.f;
// pointLight->spotExponent = 0.0f; //pointLight->quadraticAttenuation = 0.f;
//pointLight->spotExponent = 0.0f;
model->ModelFile = "Models/obstacle_cube_1.obj"; model->ModelFile = "Models/obstacle_cube_1.obj";
break; break;
@@ -60,6 +65,9 @@ void Systems::LevelGenerationSystem::SpawnObstacle()
break; break;
} }
// Put it below ground level
transform->Position.y -= bounds->VolumeVector.y * 2.f;
} }
void Systems::LevelGenerationSystem::Update( double dt ) void Systems::LevelGenerationSystem::Update( double dt )
@@ -76,10 +84,16 @@ void Systems::LevelGenerationSystem::Update( double dt )
for(auto ent : obstacles) for(auto ent : obstacles)
{ {
auto transformComponent = m_World->GetComponent<Components::Transform>(ent, "Transform"); auto transform = m_World->GetComponent<Components::Transform>(ent, "Transform");
transformComponent->Position.z += 100.f * dt; 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); removethis.push_back(ent);
} }
@@ -2,12 +2,14 @@
#define LevelGenerationSystem_h__ #define LevelGenerationSystem_h__
#include "System.h" #include "System.h"
#include "Systems/SoundSystem.h"
#include "World.h" #include "World.h"
#include "Components/Transform.h" #include "Components/Transform.h"
#include "Components/Bounds.h" #include "Components/Bounds.h"
#include "Components/Collision.h" #include "Components/Collision.h"
#include "Components/Model.h" #include "Components/Model.h"
#include "Components/PointLight.h" #include "Components/PointLight.h"
#include "Components/SoundEmitter.h"
namespace Systems namespace Systems
+2 -1
View File
@@ -20,6 +20,7 @@ public:
void Update(double dt) override; void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override; void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName); void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
private: private:
@@ -35,7 +36,7 @@ private:
unsigned long dataSize; unsigned long dataSize;
std::map<Component*, ALuint> m_Source; std::map<Component*, ALuint> m_Sources;
std::map<std::string, ALuint> m_BufferCache; // string = fileName std::map<std::string, ALuint> m_BufferCache; // string = fileName
}; };
+20 -7
View File
@@ -18,6 +18,9 @@ Systems::SoundSystem::SoundSystem(World* world)
} }
alGetError(); alGetError();
alSpeedOfSound(340.29f); // Speed of sound
alDistanceModel(AL_INVERSE_DISTANCE);
} }
void Systems::SoundSystem::Update(double dt) 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<Components::SoundEmitter>(entity, "SoundEmitter"); auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr) if(soundEmitter != nullptr)
{ {
ALuint source = m_Source[soundEmitter]; ALuint source = m_Sources[soundEmitter];
alSourcei(source, AL_GAIN, soundEmitter->Gain); alSourcef(source, AL_GAIN, soundEmitter->Gain);
alSourcei(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); //alSourcef(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcei(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); alSourcef(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance);
alSourcef(source, AL_PITCH, soundEmitter->Pitch); alSourcef(source, AL_PITCH, soundEmitter->Pitch);
alSourcei(source, AL_LOOPING, soundEmitter->Loop); 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<Components::SoundEmitter> emitter, std::string fileName) void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName)
{ {
ALuint buffer = LoadFile(fileName); ALuint buffer = LoadFile(fileName);
ALuint source = m_Source[emitter.get()]; ALuint source = m_Sources[emitter.get()];
alSourcei(source, AL_BUFFER, buffer); 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> component) void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{ {
if(type == "SoundEmitter") { if(type == "SoundEmitter") {
ALuint source = CreateSource(); 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);
}
} }
} }