Merge branch 'master' of github.com:sippeangelo/Escape-the-Dawn

Conflicts:
	Escape-the-Dawn/GameWorld.cpp
	Escape-the-Dawn/Systems/CollisionSystem.cpp
	Escape-the-Dawn/Systems/SoundSystem.h
	Escape-the-Dawn/Systems/SoundsSystem.cpp
This commit is contained in:
Adam
2014-03-13 20:09:41 +01:00
42 changed files with 1421 additions and 303 deletions
+15 -3
View File
@@ -3,7 +3,7 @@
void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
if (parent != 0)
/*if (parent != 0)
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
auto parentTransform = m_World->GetComponent<Components::Transform>(parent, "Transform");
@@ -11,7 +11,18 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
transform->Position[0] += parentTransform->Position[0];
transform->Position[1] += parentTransform->Position[1];
transform->Position[2] += parentTransform->Position[2];
}
}*/
auto collisionComponent = m_World->GetComponent<Components::Collision>(entity, "Collision");
if (!collisionComponent)
return;
// Clear old collisions
collisionComponent->CollidingEntities.clear();
// Quit out if we're not interested in collision events
if (!collisionComponent->Interested)
return;
auto entities = m_World->GetEntities();
@@ -36,7 +47,8 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
//pair.first, pair.second;
if(entity == entity2)
break;
continue;
Intersects(entity, entity2);
}
}
@@ -0,0 +1,134 @@
#include "CollisionSystem.h"
#include "World.h"
void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
/*if (parent != 0)
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
auto parentTransform = m_World->GetComponent<Components::Transform>(parent, "Transform");
transform->Position[0] += parentTransform->Position[0];
transform->Position[1] += parentTransform->Position[1];
transform->Position[2] += parentTransform->Position[2];
<<<<<<< HEAD
}
=======
}*/
auto collisionComponent = m_World->GetComponent<Components::Collision>(entity, "Collision");
if (!collisionComponent)
return;
// Clear old collisions
collisionComponent->CollidingEntities.clear();
// Quit out if we're not interested in collision events
if (!collisionComponent->Interested)
return;
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
auto entities = m_World->GetEntities();
for (auto pair : *entities) {
EntityID entity2 = pair.first;
EntityID parent2 = pair.second;
// Check if entity2 is a child of entity
/*auto currentParent = parent;
bool childOfEntity2 = false;
while (currentParent != 0) {
if (currentParent == entity2) {
childOfEntity2 = true;
break;
}
currentParent = entities[currentParent];
}
if (childOfEntity2)
continue;*/
// Check if we're the parent to entity2
//pair.first, pair.second;
if(entity == entity2)
continue;
Intersects(entity, entity2);
}
}
void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity)
{
auto aTransform = m_World->GetComponent<Components::Transform>(aEntity, "Transform");
if(aTransform == nullptr)
return;
auto bTransform = m_World->GetComponent<Components::Transform>(bEntity, "Transform");
if(bTransform == nullptr)
return;
auto aCollisionComponent = m_World->GetComponent<Components::Collision>(aEntity, "Collision");
if(aCollisionComponent == nullptr)
return;
auto bCollisionComponent = m_World->GetComponent<Components::Collision>(bEntity, "Collision");
if(bCollisionComponent == nullptr)
return;
auto aBounds = m_World->GetComponent<Components::Bounds>(aEntity, "Bounds");
auto bBounds = m_World->GetComponent<Components::Bounds>(bEntity, "Bounds");
glm::vec3 aPos = aTransform->Position + (aBounds->Origin * aTransform->Scale);
glm::vec3 bPos = bTransform->Position + (bBounds->Origin * bTransform->Scale);
glm::vec3 aMax = aPos + aBounds->VolumeVector * aTransform->Scale;
glm::vec3 aMin = aPos - aBounds->VolumeVector * aTransform->Scale;
glm::vec3 bMax = bPos + bBounds->VolumeVector * bTransform->Scale;
glm::vec3 bMin = bPos - bBounds->VolumeVector * bTransform->Scale;
if (aMin.x <= bMax.x && bMin.x <= aMax.x) {
if (aMin.y <= bMax.y && bMin.y <= aMax.y) {
if (aMin.z <= bMax.z && bMin.z <= aMax.z) {
aCollisionComponent->CollidingEntities.push_back(aEntity);
bCollisionComponent->CollidingEntities.push_back(bEntity);
return;
}
}
}
}
void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr<Components::Bounds> bounds)
{
float width = abs(bounds->VolumeVector.x);
float height = abs(bounds->VolumeVector.y);
float depth = abs(bounds->VolumeVector.z);
GLfloat vertices[] = {
bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0,
};
GLuint vbo_vertices;
glGenBuffers(1, &vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLushort elements[] = {
0, 1, 2, 3,
4, 5, 6, 7,
0, 4, 1, 5,
2, 6, 3, 7
};
GLuint ibo_elements;
glGenBuffers(1, &ibo_elements);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
+4
View File
@@ -47,6 +47,10 @@ void Systems::InputSystem::Update(double dt)
if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2]) {
m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
}
// Bounds
if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3]) {
m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
}
#endif
}
@@ -4,16 +4,15 @@ Systems::LevelGenerationSystem::LevelGenerationSystem( World* world )
: System(world)
{
elapsedtime = 0;
velocity = 0;
startx = 0;
startyz = glm::vec2(0, -1000);
}
void Systems::LevelGenerationSystem::SpawnObstacle()
{
typeRandom = 0 + (rand() % 3);
typeRandom = 0 + (rand() % 10);
EntityID ent;
@@ -24,36 +23,64 @@ 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");
positionRandom = -500 + (rand() % 1000);
positionRandom = -500 + startx + (rand() % 1000);
transform->Position = glm::vec3(positionRandom, startyz);
//transform->Velocity = glm::vec3(0.f, 10.f, 100.f);
switch (typeRandom)
{
case 0:
transform->Position = glm::vec3( positionRandom, startyz); // fix position
sound->Loop = true;
sound->Gain = 1.f;
sound->ReferenceDistance = 15.f;
m_World->GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(sound, "Sounds/hum.wav");
if(typeRandom >= 0 && typeRandom < 2) // Mountain stuff :D
{
float scale = (float)(rand() % 1000) / 250;
transform->Scale = glm::vec3(scale);
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
}
else if(typeRandom >= 2 && typeRandom < 5) // Single Mountain :D
{
float scale = (float)(rand() % 1000) / 200;
transform->Scale = glm::vec3(scale);
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
}
else if(typeRandom >= 5 && typeRandom < 8)
{
float scale = (float)(rand() % 1000) / 100;
transform->Scale = glm::vec3(scale);
bounds->VolumeVector = glm::vec3(1, 1, 1);
bounds->Origin = glm::vec3(0,1,0);
model->ModelFile = "Models/obstacle_cube_1.obj";
}
if(typeRandom >= 8 && typeRandom < 10)
{
float scale = (float)(rand() % 1000) / 100;
bounds->VolumeVector = glm::vec3(4, 4, 4);
bounds->Origin = glm::vec3(0,4,0);
pointLight = m_World->AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.3, 1.0, 0.3);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
break;
default:
powerUp = m_World->AddComponent<Components::PowerUp>(ent, "PowerUp");
powerUp->Speed = 10.f;
break;
}
model->ModelFile = "Models/powerup.obj";
}
// Put it below ground level
transform->Position.y -= bounds->VolumeVector.y * 2.f;
}
@@ -67,17 +94,24 @@ void Systems::LevelGenerationSystem::Update( double dt )
elapsedtime = 0;
}
std::vector<EntityID> removethis;
std::list<EntityID> removethis;
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");
if(transformComponent->Position.z > 100)
transform->Velocity.z = -velocity;
transform->Position += transform->Velocity * (float)dt;
// 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);
}
}
@@ -90,4 +124,15 @@ void Systems::LevelGenerationSystem::Update( double dt )
}
void Systems::LevelGenerationSystem::UpdateEntity( double dt, EntityID entity, EntityID parent )
{
if( m_World->GetProperty<std::string>(entity, "Name") == "PlayerShip")
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
startyz = glm::vec2(0, transformComponent->Position.z - 1000);
startx = transformComponent->Position.x;
velocity = transformComponent->Velocity.z;
}
}
@@ -2,11 +2,15 @@
#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"
#include "Components/PowerUp.h"
namespace Systems
@@ -20,20 +24,25 @@ namespace Systems
void SpawnObstacle();
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
int typeRandom;
int positionRandom;
glm::vec2 startyz;
float startx;
double elapsedtime;
std::list<EntityID> obstacles;
float velocity;
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Bounds> bounds;
std::shared_ptr<Components::Collision> collision;
std::shared_ptr<Components::Model> model;
std::shared_ptr<Components::PointLight> pointLight;
std::shared_ptr<Components::PowerUp> powerUp;
};
}
+17 -14
View File
@@ -3,6 +3,8 @@
Systems::PlayerSystem::PlayerSystem( World* world ) : System(world)
{
m_PlayerSpeed = 20;
m_PlayerOriginalBounds = glm::vec3(0);
}
void Systems::PlayerSystem::Update(double dt)
@@ -18,14 +20,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
if (input == nullptr)
return;
float speed = 20.0f;
auto name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "Camera") {
glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation);
glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation);
float speed = m_PlayerSpeed;
if(input->KeyState[GLFW_KEY_LEFT_SHIFT]) {
speed *= 4.0f;
}
@@ -59,26 +59,23 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
auto collision = m_World->GetComponent<Components::Collision>(entity, "Collision");
if(collision->CollidingEntities.size() != 0)
int c = 1; // lol
}
name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "PlayerShip")
{
auto bounds = m_World->GetComponent<Components::Bounds>(entity, "Bounds");
if (bounds && m_PlayerOriginalBounds == glm::vec3(0)) {
m_PlayerOriginalBounds = bounds->VolumeVector;
}
glm::vec3 Ship_Right = glm::vec3(glm::vec4(1, 0, 0, 0));
glm::vec3 Ship_Forward = glm::vec3(glm::vec4(0, 0, 1, 0));
float TurnSpeed = 2.0f;
float TurnSpeed = 1.0f;
glm::vec3 Euler = glm::eulerAngles(transform->Orientation);
if(input->KeyState[GLFW_KEY_LEFT]) {
transform->Position -= Ship_Right * (float)dt * speed;
transform->Position -= Ship_Right * (float)dt * (m_PlayerSpeed + Euler.z);
if(Euler.z < 10.f)
{
@@ -94,7 +91,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
}
}
else if(input->KeyState[GLFW_KEY_RIGHT]) {
transform->Position += Ship_Right * (float)dt * speed;
transform->Position += Ship_Right * (float)dt * (m_PlayerSpeed - Euler.z);
if(Euler.z > -10.f)
{
@@ -118,6 +115,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
else if(Euler.z > 0.f)
transform->Orientation = transform->Orientation * glm::angleAxis<float>((float)dt,glm::vec3(0,0,-1));
}
// Update player bounds based on rotation
if (bounds) {
Euler = glm::eulerAngles(transform->Orientation);
bounds->VolumeVector.x = m_PlayerOriginalBounds.x * glm::cos(glm::radians(Euler.z));
}
}
}
+3 -1
View File
@@ -7,6 +7,7 @@
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/Collision.h"
#include "Components/Bounds.h"
#include "logging.h"
@@ -23,7 +24,8 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
float m_PlayerSpeed;
glm::vec3 m_PlayerOriginalBounds;
};
}
+3 -2
View File
@@ -30,11 +30,12 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p
// Debug draw bounds
#ifdef DEBUG
auto collision = m_World->GetComponent<Components::Collision>(entity, "Collision");
auto bounds = m_World->GetComponent<Components::Bounds>(entity, "Bounds");
if (bounds != nullptr) {
glm::vec3 origin = transformComponent->Scale * (transformComponent->Position + bounds->Origin);
glm::vec3 origin = transformComponent->Position + (transformComponent->Scale * bounds->Origin);
glm::vec3 volumeVector = transformComponent->Scale * bounds->VolumeVector;
m_Renderer->AddAABBToDraw(origin, volumeVector);
m_Renderer->AddAABBToDraw(origin, volumeVector, (collision != nullptr && collision->CollidingEntities.size() > 0));
}
#endif
+1
View File
@@ -10,6 +10,7 @@
#include "Components/Transform.h"
#include "Components/Camera.h"
#include "Components/Bounds.h"
#include "Components/Collision.h"
#include "Renderer.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 path); // Use if you want to play a temporary .wav file not from component
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter); // Use if you want to play .wav file from component // imon no hate plx T.T
void StopSound(std::shared_ptr<Components::SoundEmitter> emitter);
@@ -37,7 +38,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
};
@@ -0,0 +1,51 @@
#ifndef SoundEmitter_h__
#define SoundEmitter_h__
#include "System.h"
#include "Components/Transform.h"
#include "Components/SoundEmitter.h"
#include <AL/al.h>
#include <AL/alc.h>
#include <vector>
#include <glm/gtx/quaternion.hpp>
namespace Systems
{
class SoundSystem : public System
{
public:
SoundSystem(World* world);
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;
<<<<<<< HEAD
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string path); // Use if you want to play a temporary .wav file not from component
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter); // Use if you want to play .wav file from component // imon no hate plx T.T
void StopSound(std::shared_ptr<Components::SoundEmitter> emitter);
=======
void OnComponentRemoved(std::string type, Component* component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
private:
ALuint LoadFile(std::string fileName);
ALuint CreateSource();
//File-info
char type[4];
unsigned long size, chunkSize;
short formatType, channels;
unsigned long sampleRate, avgBytesPerSec;
short bytesPerSample, bitsPerSample;
unsigned long dataSize;
std::map<Component*, ALuint> m_Sources;
std::map<std::string, ALuint> m_BufferCache; // string = fileName
};
}
#endif // !SoundEmitter_h__
+25 -9
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.get()];
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);
@@ -72,12 +75,15 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
}
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string path)
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName)
{
ALuint buffer = LoadFile(path);
ALuint source = m_Source[emitter.get()];
if (m_Sources.find(emitter.get()) == m_Sources.end())
return;
ALuint buffer = LoadFile(fileName);
ALuint source = m_Sources[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Source[emitter.get()]);
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter)
@@ -97,7 +103,17 @@ void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<
{
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);
}
}
}
@@ -0,0 +1,210 @@
#include "SoundSystem.h"
#include "World.h"
Systems::SoundSystem::SoundSystem(World* world)
: System(world)
{
//initialize OpenAL
ALCdevice* Device = alcOpenDevice(NULL);
ALCcontext* context;
if(Device)
{
context = alcCreateContext(Device, NULL);
alcMakeContextCurrent(context);
}
else
{
LOG_ERROR("OMG OPEN AL FAIL");
}
alGetError();
alSpeedOfSound(340.29f); // Speed of sound
alDistanceModel(AL_INVERSE_DISTANCE);
}
void Systems::SoundSystem::Update(double dt)
{
}
void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (transformComponent == nullptr)
return;
auto entityName = m_World->GetProperty<std::string>(entity, "Name");
if (entityName == "Camera")
{
glm::vec3 playerPos = transformComponent->Position;
ALfloat listenerPos[3] = { playerPos.x, playerPos.y, -playerPos.z };
glm::vec3 playerVel = transformComponent->Velocity;
ALfloat listenerVel[3] = { playerVel.x, playerVel.y, -playerVel.z };
glm::fquat playerQuatOri = transformComponent->Orientation;
glm::vec3 playerOriFW = glm::rotate(playerQuatOri, glm::vec3(0, 0, -1));
glm::vec3 playerOriUP = glm::rotate(playerQuatOri, glm::vec3(0, 1, 0));
ALfloat listenerOri[6] = { playerOriFW.x, playerOriFW.y, playerOriFW.z, playerOriUP.x, playerOriUP.y, playerOriUP.z };
//Listener
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
}
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr)
{
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);
glm::vec3 emitterPos = transformComponent->Position;
ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, -emitterPos.z };
glm::vec3 emitterVel= transformComponent->Velocity;
ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, -emitterVel.z };
alSourcefv(source, AL_POSITION, sourcePos);
alSourcefv(source, AL_VELOCITY, sourceVel);
}
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string path)
{
<<<<<<< HEAD
ALuint buffer = LoadFile(path);
ALuint source = m_Source[emitter.get()];
=======
if (m_Sources.find(emitter.get()) == m_Sources.end())
return;
ALuint buffer = LoadFile(fileName);
ALuint source = m_Sources[emitter.get()];
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> 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<Components::SoundEmitter> emitter)
{
alSourceStop(m_Source[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == "SoundEmitter") {
ALuint source = CreateSource();
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);
}
}
}
ALuint Systems::SoundSystem::LoadFile(std::string path)
{
if (m_BufferCache.find(path) != m_BufferCache.end())
return m_BufferCache[path];
FILE *fp = NULL;
fp = fopen(path.c_str(), "rb");
//CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp);
if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') {
LOG_ERROR("ERROR: No RIFF in WAVE-file");
return 0;
}
fread(&size, sizeof(unsigned long), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') {
LOG_ERROR("ERROR: Not WAVE-file");
return 0;
}
fread(type, sizeof(char), 4, fp);
if(type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ') {
LOG_ERROR("ERROR: No fmt in WAVE-file");
return 0;
}
//READ THE DATA FROM WAVE-FILE
fread(&chunkSize, sizeof(unsigned long), 1, fp);
fread(&formatType, sizeof(short), 1, fp);
fread(&channels, sizeof(short), 1, fp);
fread(&sampleRate, sizeof(unsigned long), 1, fp);
fread(&avgBytesPerSec, sizeof(unsigned long), 1, fp);
fread(&bytesPerSample, sizeof(short), 1, fp);
fread(&bitsPerSample, sizeof(short), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a')
{
LOG_ERROR("ERROR: WAVE-file Missing data");
return 0;
}
fread(&dataSize, sizeof(unsigned long), 1, fp);
unsigned char* buf = new unsigned char[dataSize];
fread(buf, sizeof(unsigned char), dataSize, fp);
fclose(fp);
// Create buffer
ALuint format = 0;
if(bitsPerSample == 8)
{
if(channels == 1)
format = AL_FORMAT_MONO8;
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
if(bitsPerSample == 16)
{
if (channels == 1)
format = AL_FORMAT_MONO16;
else if (channels == 2)
format = AL_FORMAT_STEREO16;
}
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, buf, dataSize, sampleRate);
delete[] buf;
m_BufferCache[path] = buffer;
return buffer;
}
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;
}