Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
AudioComponent.h
Go to the documentation of this file.
1/**
2 * @file AudioComponent.h
3 * @ingroup mnd_components
4 * @brief GameObject component that plays audio clips by name.
5 *
6 * Attach to any mnd::GameObject to give it a small library of named
7 * mnd::Audio clips. Each frame the component pushes the GameObject's
8 * world position to every clip so 3D spatialisation works without
9 * extra plumbing.
10 *
11 * @code
12 * auto *src = enemy->AddComponent<mnd::AudioComponent>();
13 * auto growl = mnd::Audio::Load("sfx/growl.wav");
14 * src->RegisterAudio("growl", growl);
15 * src->Play("growl", true); // looping
16 * @endcode
17 */
18
19#pragma once
20#include "audio/Audio.h"
21#include "scene/Component.h"
22
23namespace mnd
24{
25/**
26 * @ingroup mnd_components
27 * @brief Plays one or more named audio clips, attached to a GameObject.
28 */
30{
32public:
33 /// Read clips and properties from a serialised scene JSON entry.
34 void LoadProperties(const nlohmann::json &json) override;
35 /// Push the owner's world position to every registered clip.
36 void Update(float deltaTime) override;
37
38 /// Add a clip under @p name; later calls reference it by that key.
39 void RegisterAudio(const std::string &name, std::shared_ptr<Audio> &clip);
40 /// Start the named clip; @p loop true loops forever.
41 void Play(const std::string &name, bool loop = false);
42 /// Stop the named clip if it is currently playing.
43 void Stop(const std::string &name);
44 /// True iff the named clip is currently playing.
45 bool IsPlaying(const std::string &name);
46
47private:
48 std::unordered_map<std::string, std::shared_ptr<Audio>> m_clips;
49};
50} // namespace mnd
Plays one or more named audio clips, attached to a GameObject.
void RegisterAudio(const std::string &name, std::shared_ptr< Audio > &clip)
Add a clip under name; later calls reference it by that key.
void LoadProperties(const nlohmann::json &json) override
Read clips and properties from a serialised scene JSON entry.
bool IsPlaying(const std::string &name)
True iff the named clip is currently playing.
void Play(const std::string &name, bool loop=false)
Start the named clip; loop true loops forever.
void Update(float deltaTime) override
Push the owner's world position to every registered clip.
void Stop(const std::string &name)
Stop the named clip if it is currently playing.
#define COMPONENT(ComponentClass)
Definition Component.h:105