Thrive
sound_source_system.h
1 #pragma once
2 
3 #include "engine/component.h"
4 #include "engine/system.h"
5 #include "engine/touchable.h"
6 
7 #include <list>
8 #include <memory>
9 
10 namespace luabind {
11 class scope;
12 }
13 
14 namespace thrive {
15  class SoundEmitter;
16 }
17 
18 namespace thrive {
19 
23 class Sound {
24 
25 public:
26 
52  static luabind::scope
53  luaBindings();
54 
58  enum PlayState {
59  Play,
60  Pause,
61  Stop
62  };
63 
67  struct Properties : public Touchable {
68  PlayState playState = PlayState::Stop;
69  bool loop = false;
70  float volume = 1.0f;
71  float maxDistance = 25.0f;
72  float rolloffFactor = 0.4f;
73  float referenceDistance = 5.0f;
74  uint8_t priority = 0;
75  };
76 
80  Sound();
81 
90  Sound(
91  std::string name,
92  std::string filename
93  );
94 
100  std::string
101  filename() const;
102 
108  void
109  load(
111  );
112 
118  std::string
119  name() const;
120 
124  void
125  pause();
126 
130  void
131  play();
132 
136  void
137  stop();
138 
145  storage() const;
146 
151 
155  SoundEmitter* m_sound = nullptr;
156 private:
157 
158  std::string m_filename;
159 
160  std::string m_name;
161 };
162 
168  COMPONENT(SoundSource)
169 
170 public:
171 
188  static luabind::scope
189  luaBindings();
190 
201  Sound*
202  addSound(
203  std::string name,
204  std::string filename
205  );
206 
212  void
213  removeSound(
214  std::string name
215  );
216 
224  void
225  playSound(
226  std::string name
227  );
228 
229 
240  void
241  interpose(
242  std::string name,
243  int fadeTime
244  );
245 
253  void
254  queueSound(
255  std::string name
256  );
257 
261  void
263 
264 
265  void
266  load(
268  ) override;
269 
270 
272  storage() const override;
273 
278 
285 
290 
291 
292 private:
293 
294  friend class SoundSourceSystem;
295 
296  std::list<Sound*> m_addedSounds;
297 
298  std::list<Sound*> m_removedSounds;
299 
300  Sound* m_autoActiveSound = nullptr;
301  Sound* m_queuedSound = nullptr;
302  int m_autoSoundCountdown = 0;
303  bool m_isTransitioningAuto = false;
304  bool m_shouldInteruptPlaying = false;
305 
306  std::unordered_map<std::string, std::unique_ptr<Sound>> m_sounds;
307 
308 };
309 
310 
314 class SoundSourceSystem : public System {
315 
316 public:
317 
326  static luabind::scope
327  luaBindings();
328 
333 
338 
339  void
340  activate() override;
341 
342  void
343  deactivate() override;
344 
349  void init(GameState* gameState) override;
350 
354  void shutdown() override;
355 
359  void update(int, int) override;
360 
361 private:
362 
363  struct Implementation;
364  std::unique_ptr<Implementation> m_impl;
365 };
366 
367 }
368 
369 
TouchableValue< bool > m_autoLoop
Whether this source auto loops.
Definition: sound_source_system.h:284
StorageContainer storage() const override
Serializes the component.
Definition: sound_source_system.cpp:285
Properties m_properties
Properties.
Definition: sound_source_system.h:150
Helper class for keeping track of changing data.
Definition: touchable.h:18
PlayState
Play mode of the sound.
Definition: sound_source_system.h:58
void load(const StorageContainer &storage) override
Loads the component.
Definition: sound_source_system.cpp:265
TouchableValue< float > m_volumeMultiplier
Volume multiplier that is applied to all associated sounds.
Definition: sound_source_system.h:289
void playSound(std::string name)
Plays a sound This is equivalent to playing the sound directly.
Definition: sound_source_system.cpp:236
Sound()
Default constructor for loading.
Definition: sound_source_system.cpp:57
static luabind::scope luaBindings()
Lua bindings.
Definition: sound_source_system.cpp:190
void stop()
Stops the sound during the next frame.
Definition: sound_source_system.cpp:117
void shutdown() override
Shuts the system down.
Definition: sound_source_system.cpp:503
Base class for components.
Definition: component.h:31
SoundSourceSystem()
Constructor.
Definition: sound_source_system.cpp:452
A system handles one specific part of the game.
Definition: system.h:23
void init(GameState *gameState) override
Initializes the system.
Definition: sound_source_system.cpp:493
void activate() override
Called by GameState::activate()
Definition: sound_source_system.cpp:463
void deactivate() override
Called by GameState::deactivate()
Definition: sound_source_system.cpp:478
void pause()
Pauses the sound during the next frame.
Definition: sound_source_system.cpp:110
Definition: sound_emitter.h:19
Definition: bullet_to_ogre_system.h:5
void load(const StorageContainer &storage)
Loads a sound from storage.
Definition: sound_source_system.cpp:79
std::string filename() const
The file that the sound is playing.
Definition: sound_source_system.cpp:73
std::string name() const
The name of the sound.
Definition: sound_source_system.cpp:97
Definition: bullet_ogre_conversion.h:6
A component for sound sources.
Definition: sound_source_system.h:167
Sound * addSound(std::string name, std::string filename)
Adds a new sound.
Definition: sound_source_system.cpp:213
GameState * gameState() const
Returns the system's game state.
Definition: system.cpp:183
StorageContainer storage() const
Constructs a storage container for serialization.
Definition: sound_source_system.cpp:124
Represents a distinct set of active systems and entities.
Definition: game_state.h:41
TouchableValue< bool > m_ambientSoundSource
Whether this source as an ambient soundsource with no 3d position.
Definition: sound_source_system.h:277
Creates, updates and removes sounds.
Definition: sound_source_system.h:314
Sound properties.
Definition: sound_source_system.h:67
void update(int, int) override
Updates the system.
Definition: sound_source_system.cpp:510
Represents a single sound.
Definition: sound_source_system.h:23
~SoundSourceSystem()
Destructor.
Definition: sound_source_system.cpp:459
void interpose(std::string name, int fadeTime)
Interrupts the current ambient sound and interposes a new one Does nothing for non-ambient sound sour...
Definition: sound_source_system.cpp:243
static luabind::scope luaBindings()
Lua bindings.
Definition: sound_source_system.cpp:306
void removeSound(std::string name)
Removes a sound by name.
Definition: sound_source_system.cpp:227
void interruptPlaying()
Stops playing of all songs.
Definition: sound_source_system.cpp:260
static luabind::scope luaBindings()
Lua bindings.
Definition: sound_source_system.cpp:29
void play()
Starts (or resumes) playing the sound.
Definition: sound_source_system.cpp:103
void queueSound(std::string name)
Queues up an ambient sound to be played after the current one Does nothing for non-ambient sound sour...
Definition: sound_source_system.cpp:253
A key-value storage for serialization.
Definition: serialization.h:36
SoundEmitter * m_sound
Pointer to internal sound.
Definition: sound_source_system.h:155