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
{
float Gain;
float MaxDistance;
//float MaxDistance;
float ReferenceDistance;
float Pitch;
bool Loop;
Binary file not shown.
Binary file not shown.
@@ -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<Components::Bounds>(ent, "Bounds");
collision = m_World->AddComponent<Components::Collision>(ent, "Collision");
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");
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)
{
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<Components::Transform>(ent, "Transform");
transformComponent->Position.z += 100.f * dt;
auto transform = m_World->GetComponent<Components::Transform>(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);
}
@@ -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
+2 -1
View File
@@ -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> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
private:
@@ -35,7 +36,7 @@ private:
unsigned long dataSize;
std::map<Component*, ALuint> m_Source;
std::map<Component*, ALuint> m_Sources;
std::map<std::string, ALuint> m_BufferCache; // string = fileName
};
+20 -7
View File
@@ -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<Components::SoundEmitter>(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<Components::SoundEmitter> 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> 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);
}
}
}