Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
AudioComponent.cpp
Go to the documentation of this file.
1#include "scene/components/AudioComponent.h"
2#include "scene/GameObject.h"
3
4namespace eng
5{
6 void AudioComponent::LoadProperties(const nlohmann::json& json)
7 {
8 if (json.contains("audio"))
9 {
10 auto& clipsObject = json["audio"];
11 for (auto& clip : clipsObject)
12 {
13 std::string name = clip.value("name", "noname");
14 std::string path = clip.value("path", "");
15 auto audio = Audio::Load(path);
16 if (audio)
17 {
18 float volume = clip.value("volume", 1.0f);
19 audio->SetVolume(volume);
20 RegisterAudio(name, audio);
21 }
22 }
23 }
24 }
25
26 void AudioComponent::Update(float deltaTime)
27 {
28 auto pos = m_owner->GetWorldPosition();
29 for (auto& clip : m_clips)
30 {
31 if (clip.second->IsPlaying())
32 {
33 clip.second->SetPosition(pos);
34 }
35 }
36 }
37
38 void AudioComponent::RegisterAudio(const std::string& name, std::shared_ptr<Audio>& clip)
39 {
40 m_clips[name] = clip;
41 }
42
43 void AudioComponent::Play(const std::string& name, bool loop)
44 {
45 auto it = m_clips.find(name);
46 if (it != m_clips.end())
47 {
48 if (it->second)
49 {
50 it->second->Play(loop);
51 }
52 }
53 }
54
55 void AudioComponent::Stop(const std::string& name)
56 {
57 auto it = m_clips.find(name);
58 if (it != m_clips.end())
59 {
60 if (it->second)
61 {
62 it->second->Stop();
63 }
64 }
65 }
66
67 bool AudioComponent::IsPlaying(const std::string& name)
68 {
69 auto it = m_clips.find(name);
70 if (it != m_clips.end())
71 {
72 if (it->second)
73 {
74 return it->second->IsPlaying();
75 }
76 }
77
78 return false;
79 }
80}
void LoadProperties(const nlohmann::json &json) override
void Play(const std::string &name, bool loop=false)
void Stop(const std::string &name)
void RegisterAudio(const std::string &name, std::shared_ptr< Audio > &clip)
void Update(float deltaTime) override
bool IsPlaying(const std::string &name)
static std::shared_ptr< Audio > Load(const std::string &path)
Definition Audio.cpp:72
GameObject * m_owner
Definition Component.h:33
glm::vec3 GetWorldPosition() const