Peoplemon  0.1.0
Peoplemon 3 game source documentation
Weather.cpp
Go to the documentation of this file.
1 #include <Core/Maps/Weather.hpp>
2 
3 #include <BLIB/Util/Random.hpp>
6 
7 #include "Weather/Base.hpp"
8 #include "Weather/Fog.hpp"
9 #include "Weather/Rain.hpp"
10 #include "Weather/Sandstorm.hpp"
11 #include "Weather/Snow.hpp"
12 #include "Weather/Sunny.hpp"
13 
14 namespace core
15 {
16 namespace map
17 {
18 namespace
19 {
20 const std::string Types[] = {"None",
21  "AllRandom",
22  "LightRain",
23  "LightRainThunder",
24  "HardRain",
25  "HardRainThunder",
26  "LightSnow",
27  "LightSnowThunder",
28  "HardSnow",
29  "HardSnowThunder",
30  "ThinFog",
31  "ThickFog",
32  "Sunny",
33  "Sandstorm",
34  "WaterRandom",
35  "SnowRandom",
36  "DesertRandom"};
37 
38 } // namespace
39 
41 : type(None)
42 , state(Continuous)
43 , stateTime(-1.f)
44 , owner(nullptr)
45 , systems(nullptr) {}
46 
47 Weather::~Weather() { weather.reset(); }
48 
50  systems = &sys;
51  owner = &m;
52 }
53 
54 void Weather::set(Type t, bool im) {
55  if (t != type) {
56  BL_LOG_INFO << "Set weather to " << Types[t];
57  type = t;
58  state = Stopping;
59  if (weather) {
60  weather->stop();
61  if (im) weather.release();
62  }
63  }
64 }
65 
66 Weather::Type Weather::getType() const { return type; }
67 
68 void Weather::update(float dt) {
69  if (weather) weather->update(dt);
70 
71  switch (state) {
72  case WaitingWeather:
73  stateTime -= dt;
74  if (stateTime <= 0.f) {
75  BL_LOG_INFO << "Stopping current weather";
76  stateTime = -1.f;
77  weather->stop();
78  state = Stopping;
79  }
80  break;
81 
82  case WaitingStopped:
83  stateTime -= dt;
84  if (stateTime <= 0.f) {
85  BL_LOG_INFO << "Starting new weather";
86  stateTime = bl::util::Random::get<float>(300.f, 600.f);
87  state = WaitingWeather;
88  makeWeather();
89  }
90  break;
91 
92  case Stopping:
93  if (!weather || weather->stopped()) {
94  if (weather) {
95  BL_LOG_INFO << "Weather stopped";
96  bl::event::Dispatcher::dispatch<event::WeatherStopped>({weather->type()});
97  weather.release();
98  }
99 
100  switch (type) {
101  case AllRandom:
102  case WaterRandom:
103  case SnowRandom:
104  case DesertRandom:
105  state = WaitingStopped;
106  stateTime = bl::util::Random::get<float>(200.f, 400.f);
107  break;
108  case None:
109  state = Continuous;
110  break;
111  default:
112  state = Continuous;
113  makeWeather();
114  break;
115  }
116  }
117  break;
118 
119  default:
120  break;
121  }
122 }
123 
124 void Weather::makeWeather() {
125  const auto makeRain = [this](bool hard, bool thunder) {
126  BL_LOG_INFO << "Created rain";
127  this->weather.reset(new weather::Rain(hard, thunder));
128  };
129  const auto makeSnow = [this](bool hard, bool thunder) {
130  BL_LOG_INFO << "Created snow";
131  this->weather.reset(new weather::Snow(hard, thunder));
132  };
133  const auto makeFog = [this](bool thick) {
134  BL_LOG_INFO << "Created fog";
135  this->weather.reset(new weather::Fog(thick));
136  };
137  const auto makeSunny = [this]() {
138  BL_LOG_INFO << "Created sunny";
139  this->weather.reset(new weather::Sunny());
140  };
141  const auto makeSandstorm = [this]() {
142  BL_LOG_INFO << "Created sandstorm";
143  this->weather.reset(new weather::Sandstorm());
144  };
145 
146  using Maker = std::function<void()>;
147  const Maker makers[] = {
148  [&makeFog]() { makeFog(true); }, // all
149  [&makeFog]() { makeFog(false); },
150  [&makeRain]() { makeRain(false, false); }, // rain
151  [&makeRain]() { makeRain(false, true); },
152  [&makeRain]() { makeRain(true, false); },
153  [&makeRain]() { makeRain(true, true); },
154  [&makeSnow]() { makeSnow(false, false); }, // snow
155  [&makeSnow]() { makeSnow(false, true); },
156  [&makeSnow]() { makeSnow(true, false); },
157  [&makeSnow]() { makeSnow(true, true); },
158  makeSunny, // desert
159  makeSandstorm,
160  };
161 
162  switch (type) {
163  case AllRandom:
164  makers[bl::util::Random::get<unsigned int>(0, 12)](); // [0, 11]
165  break;
166  case WaterRandom:
167  makers[bl::util::Random::get<unsigned int>(2, 6)](); //[2, 5]
168  break;
169  case SnowRandom:
170  makers[bl::util::Random::get<unsigned int>(6, 10)](); //[6, 9]
171  break;
172  case DesertRandom:
173  makers[bl::util::Random::get<unsigned int>(10, 12)](); // [10, 11]
174  break;
175  case LightRain:
176  makeRain(false, false);
177  break;
178  case LightRainThunder:
179  makeRain(false, true);
180  break;
181  case HardRain:
182  makeRain(true, false);
183  break;
184  case HardRainThunder:
185  makeRain(true, true);
186  break;
187  case LightSnow:
188  makeSnow(false, false);
189  break;
190  case LightSnowThunder:
191  makeSnow(false, true);
192  break;
193  case HardSnow:
194  makeSnow(true, false);
195  break;
196  case HardSnowThunder:
197  makeSnow(true, true);
198  break;
199  case ThinFog:
200  makeFog(false);
201  break;
202  case ThickFog:
203  makeFog(true);
204  break;
205  case Sunny:
206  makeSunny();
207  break;
208  case SandStorm:
209  makeSandstorm();
210  break;
211  default:
212  BL_LOG_WARN << "Unknown weather type: " << type;
213  break;
214  }
215 
216  if (weather) {
217  weather->start(systems->engine(), systems->render().getMainRenderTarget(), *owner);
218  bl::event::Dispatcher::dispatch<event::WeatherStarted>({weather->type()});
219  }
220 }
221 
222 } // namespace map
223 } // namespace core
Core classes and functionality for both the editor and game.
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
Type
The type of weather.
Definition: Weather.hpp:38
@ HardSnowThunder
Hard snow with thunder.
Definition: Weather.hpp:67
@ Sunny
A very sunny day with pulsating light.
Definition: Weather.hpp:76
@ ThinFog
Thin fog covers the area.
Definition: Weather.hpp:70
@ SandStorm
A sandstorm ravages you.
Definition: Weather.hpp:79
@ HardRain
Hard rain with no thunder.
Definition: Weather.hpp:52
@ HardRainThunder
Hard rain with thunder.
Definition: Weather.hpp:55
@ LightRainThunder
Light rain with thunder.
Definition: Weather.hpp:49
@ None
No weather will occur.
Definition: Weather.hpp:40
@ WaterRandom
Periodically triggers one of LightRain, HardRain, LightRainThunder, HardRainThunder.
Definition: Weather.hpp:82
@ LightSnowThunder
Light snow with thunder.
Definition: Weather.hpp:61
@ LightRain
Light rain with no thunder.
Definition: Weather.hpp:46
@ SnowRandom
Periodically triggers one of LightSnow, HardSnow, LightSnowThunder, HardSnowThunder.
Definition: Weather.hpp:85
@ ThickFog
Thick fog obscures everything.
Definition: Weather.hpp:73
@ LightSnow
Light snow with no thunder.
Definition: Weather.hpp:58
@ DesertRandom
Periodically triggers one of Sunny, Sandstorm.
Definition: Weather.hpp:88
@ HardSnow
Hard snow with no thunder.
Definition: Weather.hpp:64
@ AllRandom
All types of weather may occur over time.
Definition: Weather.hpp:43
Weather()
Initializes the weather system with None.
Definition: Weather.cpp:40
void update(float dt)
Updates the current weather.
Definition: Weather.cpp:68
void activate(system::Systems &systems, Map &map)
Activates the weather system.
Definition: Weather.cpp:49
~Weather()
Terminates active weather.
Definition: Weather.cpp:47
Type getType() const
Returns the current type of weather.
Definition: Weather.cpp:66
void set(Type type, bool immediate=false)
Sets the current weather type.
Definition: Weather.cpp:54
bl::rc::RenderTarget & getMainRenderTarget()
Returns the engine observer that the main game is rendering to.
Definition: Render.hpp:55
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Definition: Systems.cpp:35
Render & render()
Returns the render system.
Definition: Systems.cpp:87
Weather type for rainy days. Handles light and hard rain and owns thunder if need be.
Definition: Rain.hpp:34