Uses an enumerator to seperate 3D and 2D sound instead of the path name.
This commit is contained in:
@@ -7,14 +7,15 @@
|
|||||||
|
|
||||||
namespace Components
|
namespace Components
|
||||||
{
|
{
|
||||||
|
|
||||||
struct SoundEmitter : Component
|
struct SoundEmitter : Component
|
||||||
{
|
{
|
||||||
enum SoundType
|
enum class SoundType
|
||||||
{
|
{
|
||||||
BGM_SOUND,
|
SOUND_3D,
|
||||||
SFX_SOUND
|
SOUND_2D
|
||||||
};
|
};
|
||||||
|
|
||||||
SoundEmitter() : Gain(1.f), MaxDistance(1.f), MinDistance(1.f), Pitch(1.f), Loop(false) {}
|
SoundEmitter() : Gain(1.f), MaxDistance(1.f), MinDistance(1.f), Pitch(1.f), Loop(false) {}
|
||||||
float Gain;
|
float Gain;
|
||||||
float MaxDistance;
|
float MaxDistance;
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ void InputManager::Update(double dt)
|
|||||||
if(m_CurrentKeyState[GLFW_KEY_Z]) //TEMP
|
if(m_CurrentKeyState[GLFW_KEY_Z]) //TEMP
|
||||||
{
|
{
|
||||||
Events::PlayBGM e;
|
Events::PlayBGM e;
|
||||||
e.Resource = "Sounds/BGM/WUB.mp3";
|
e.Resource = "Sounds/BGM/MainMenu.mp3";
|
||||||
e.Loop = true;
|
e.Loop = true;
|
||||||
EventBroker->Publish(e);
|
EventBroker->Publish(e);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-11
@@ -2,24 +2,16 @@
|
|||||||
#include "Sound.h"
|
#include "Sound.h"
|
||||||
|
|
||||||
|
|
||||||
Sound::Sound(std::string path, FMOD_SYSTEM* system)
|
Sound::Sound(std::string path, FMOD_SYSTEM* system, Components::SoundEmitter::SoundType type)
|
||||||
{
|
{
|
||||||
|
if(type == Components::SoundEmitter::SoundType::SOUND_2D)
|
||||||
std::string str = "";
|
|
||||||
for(int i = 7; i < 10; i++)
|
|
||||||
{
|
|
||||||
str += path[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(str == "BGM")
|
|
||||||
{
|
{
|
||||||
FMOD_System_CreateSound(system, path.c_str(), FMOD_2D, NULL, &m_Sound);
|
FMOD_System_CreateSound(system, path.c_str(), FMOD_2D, NULL, &m_Sound);
|
||||||
}
|
}
|
||||||
if (str == "SFX")
|
if(type == Components::SoundEmitter::SoundType::SOUND_3D)
|
||||||
{
|
{
|
||||||
FMOD_System_CreateSound(system, path.c_str(), FMOD_3D, NULL, &m_Sound);
|
FMOD_System_CreateSound(system, path.c_str(), FMOD_3D, NULL, &m_Sound);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Sound::~Sound()
|
Sound::~Sound()
|
||||||
|
|||||||
+2
-1
@@ -4,11 +4,12 @@
|
|||||||
#include "ResourceManager.h"
|
#include "ResourceManager.h"
|
||||||
#include <fmod.h>
|
#include <fmod.h>
|
||||||
#include <fmod_errors.h>
|
#include <fmod_errors.h>
|
||||||
|
#include <Components/SoundEmitter.h>
|
||||||
|
|
||||||
class Sound : public Resource
|
class Sound : public Resource
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Sound(std::string path, FMOD_SYSTEM* system);
|
Sound(std::string path, FMOD_SYSTEM* system, Components::SoundEmitter::SoundType type);
|
||||||
~Sound();
|
~Sound();
|
||||||
|
|
||||||
operator FMOD_SOUND*() const { return m_Sound; }
|
operator FMOD_SOUND*() const { return m_Sound; }
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf)
|
|||||||
|
|
||||||
void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm)
|
void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm)
|
||||||
{
|
{
|
||||||
rm->RegisterType("Sound", [this](std::string resourceName) { return new Sound(resourceName, this->m_System); });
|
rm->RegisterType("Sound3D", [this](std::string resourceName) { return new Sound(resourceName, this->m_System, Components::SoundEmitter::SoundType::SOUND_3D); });
|
||||||
|
rm->RegisterType("Sound2D", [this](std::string resourceName) { return new Sound(resourceName, this->m_System, Components::SoundEmitter::SoundType::SOUND_2D); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::SoundSystem::Update(double dt)
|
void Systems::SoundSystem::Update(double dt)
|
||||||
@@ -88,18 +89,6 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
|
|||||||
FMOD_SOUND* sound = m_Sounds[entity];
|
FMOD_SOUND* sound = m_Sounds[entity];
|
||||||
auto eTransform = m_World->GetComponent<Components::Transform>(entity);
|
auto eTransform = m_World->GetComponent<Components::Transform>(entity);
|
||||||
|
|
||||||
|
|
||||||
// if(emitter->type == emitter->BGM_SOUND)
|
|
||||||
// {
|
|
||||||
//
|
|
||||||
// EntityID listener = m_Listeners[0]; // "Pretty" ugly hack... Sets the transform Component for emitter to be same as first listener
|
|
||||||
// auto lTransform = m_World->GetComponent<Components::Transform>(listener);
|
|
||||||
// if(lTransform)
|
|
||||||
// {
|
|
||||||
// eTransform->Position = m_TransformSystem->AbsolutePosition(listener);
|
|
||||||
// eTransform->Velocity = lTransform->Velocity;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
glm::vec3 tPos = m_TransformSystem->AbsolutePosition(entity);
|
glm::vec3 tPos = m_TransformSystem->AbsolutePosition(entity);
|
||||||
FMOD_VECTOR ePos = {tPos.x, tPos.y, tPos.z};
|
FMOD_VECTOR ePos = {tPos.x, tPos.y, tPos.z};
|
||||||
|
|
||||||
@@ -145,14 +134,13 @@ bool Systems::SoundSystem::PlaySFX(const Events::PlaySFX &event)
|
|||||||
LOG_ERROR("FMOD: The Entity %i does not have a SoundEmitter-component, but is trying to play a sound", event.Emitter);FMOD_CHANNEL* channel = m_Channels[event.Emitter];
|
LOG_ERROR("FMOD: The Entity %i does not have a SoundEmitter-component, but is trying to play a sound", event.Emitter);FMOD_CHANNEL* channel = m_Channels[event.Emitter];
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Sound* sound = m_World->GetResourceManager()->Load<Sound>("Sound", event.Resource);
|
emitter->type = Components::SoundEmitter::SoundType::SOUND_3D;
|
||||||
|
Sound* sound = m_World->GetResourceManager()->Load<Sound>("Sound3D", event.Resource);
|
||||||
m_Sounds[event.Emitter] = *sound;
|
m_Sounds[event.Emitter] = *sound;
|
||||||
|
|
||||||
PlaySound(&m_Channels[event.Emitter], m_Sounds[event.Emitter], 1.0, event.Loop);
|
PlaySound(&m_Channels[event.Emitter], m_Sounds[event.Emitter], 1.0, event.Loop);
|
||||||
FMOD_System_Update(m_System);
|
FMOD_System_Update(m_System);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
FMOD_BOOL isPlaying = false;
|
FMOD_BOOL isPlaying = false;
|
||||||
FMOD_Channel_IsPlaying(m_Channels[event.Emitter], &isPlaying);
|
FMOD_Channel_IsPlaying(m_Channels[event.Emitter], &isPlaying);
|
||||||
if(!isPlaying)
|
if(!isPlaying)
|
||||||
@@ -168,9 +156,8 @@ bool Systems::SoundSystem::PlayBGM(const Events::PlayBGM &event)
|
|||||||
auto eTransform = m_World->AddComponent<Components::Transform>(emitter);
|
auto eTransform = m_World->AddComponent<Components::Transform>(emitter);
|
||||||
|
|
||||||
auto eComponent = m_World->AddComponent<Components::SoundEmitter>(emitter);
|
auto eComponent = m_World->AddComponent<Components::SoundEmitter>(emitter);
|
||||||
eComponent->type = eComponent->BGM_SOUND;
|
eComponent->type = Components::SoundEmitter::SoundType::SOUND_3D;
|
||||||
|
Sound* sound = m_World->GetResourceManager()->Load<Sound>("Sound2D", event.Resource);
|
||||||
Sound* sound = m_World->GetResourceManager()->Load<Sound>("Sound", event.Resource);
|
|
||||||
m_Sounds[emitter] = *sound;
|
m_Sounds[emitter] = *sound;
|
||||||
|
|
||||||
FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, *sound, false, &m_Channels[emitter]);
|
FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, *sound, false, &m_Channels[emitter]);
|
||||||
|
|||||||
Reference in New Issue
Block a user