Peoplemon  0.1.0
Peoplemon 3 game source documentation
Thunder.cpp
Go to the documentation of this file.
1 #include "Thunder.hpp"
2 
3 #include <BLIB/Audio/AudioSystem.hpp>
4 #include <BLIB/Events.hpp>
5 #include <BLIB/Util/Random.hpp>
7 #include <Core/Properties.hpp>
8 #include <Core/Resources.hpp>
9 #include <cmath>
10 
11 namespace core
12 {
13 namespace map
14 {
15 namespace weather
16 {
17 namespace
18 {
19 float computeAlpha(float time) {
20  if (time <= 0.5f) {
21  const float x = time - 0.2f;
22  return -1000.f * x * x + 225;
23  }
24  const float x = (time - 0.5f) - 0.2f;
25  return -550.f * x * x + 200;
26 }
27 } // namespace
28 
29 Thunder::Thunder(bool e, bool f)
30 : enabled(e)
31 , minInterval(f ? Properties::FrequentThunderMinInterval() :
32  Properties::InfrequentThunderMinInterval())
33 , maxInterval(f ? Properties::FrequentThunderMaxInterval() :
34  Properties::InfrequentThunderMaxInterval())
35 , timeSinceLastThunder(0.f)
36 , stopping(false) {
37  if (e) { sound = bl::audio::AudioSystem::getOrLoadSound(Properties::ThunderSoundFile()); }
38 }
39 
40 Thunder::~Thunder() { bl::audio::AudioSystem::stopSound(sound, 0.5f); }
41 
42 void Thunder::stop() { stopping = true; }
43 
44 void Thunder::update(float dt) {
45  if (enabled) {
46  timeSinceLastThunder += dt;
47  if (!stopping) {
48  if (bl::util::Random::get<float>(minInterval, maxInterval) <= timeSinceLastThunder) {
49  timeSinceLastThunder = 0.f;
50  bl::audio::AudioSystem::playSound(sound);
51  bl::event::Dispatcher::dispatch<event::Thundered>({});
52  }
53  }
54  }
55 }
56 
57 } // namespace weather
58 } // namespace map
59 } // namespace core
Core classes and functionality for both the editor and game.
Wrapper around bl::engine::Configuration. Provides application configuration in variables that may be...
Definition: Properties.hpp:28
static const std::string & ThunderSoundFile()
Definition: Properties.cpp:365
void stop()
Prevents further thunder and fades out current strike if present.
Definition: Thunder.cpp:42
void update(float dt)
Update the thunder.
Definition: Thunder.cpp:44
~Thunder()
Stops the thunder sound.
Definition: Thunder.cpp:40
Thunder(bool enabled, bool frequent)
Construct a new Thunder object.
Definition: Thunder.cpp:29