Peoplemon  0.1.0
Peoplemon 3 game source documentation
LightingSystem.hpp
Go to the documentation of this file.
1 #ifndef CORE_MAPS_LIGHTINGSYSTEM_HPP
2 #define CORE_MAPS_LIGHTINGSYSTEM_HPP
3 
4 #include <BLIB/Containers/Grid.hpp>
5 #include <BLIB/Containers/ObjectPool.hpp>
6 #include <BLIB/Events.hpp>
7 #include <BLIB/Render/Lighting/Scene2DLighting.hpp>
8 #include <BLIB/Serialization/Binary.hpp>
10 #include <Core/Events/Weather.hpp>
11 #include <Core/Maps/Light.hpp>
12 #include <SFML/Graphics.hpp>
13 #include <unordered_map>
14 #include <vector>
15 
16 namespace core
17 {
18 namespace map
19 {
27 : public bl::event::Listener<event::TimeChange, event::WeatherStarted, event::WeatherStopped,
28  event::Thundered> {
29 public:
31  using Handle = std::uint16_t;
32 
34  static constexpr Handle None = 0;
35 
40 
44  virtual ~LightingSystem() = default;
45 
51  void addLight(const Light& light);
52 
59  void updateLight(Handle handle, const Light& value);
60 
67  Handle getClosestLight(const sf::Vector2i& position);
68 
76  const Light& getLight(Handle handle) const;
77 
84  void removeLight(Handle light);
85 
89  bool lightsAreOn() const;
90 
97  void setAmbientLevel(std::uint8_t lowLightLevel, std::uint8_t highLightLevel);
98 
104  std::uint8_t getMinLightLevel() const;
105 
111  std::uint8_t getMaxLightLevel() const;
112 
119  void adjustForSunlight(bool adjust);
120 
126  bool adjustsForSunlight() const;
127 
133  void activate(bl::rc::lgt::Scene2DLighting& sceneLighting);
134 
139  void subscribe();
140 
145  void unsubscribe();
146 
151  void clear();
152 
158  void update(float dt);
159 
165  void setColorTint(const glm::vec3& tint);
166 
172  virtual void observe(const event::TimeChange& timeChange) override;
173 
179  virtual void observe(const event::WeatherStarted& event) override;
180 
186  virtual void observe(const event::WeatherStopped& event) override;
187 
191  virtual void observe(const event::Thundered&) override;
192 
193 private:
194  std::vector<Light> rawLights;
195  std::uint8_t minLevel;
196  std::uint8_t maxLevel;
197  std::uint8_t sunlight;
198  glm::vec3 tint;
199 
200  bl::rc::lgt::Scene2DLighting* sceneLighting;
201  std::vector<bl::rc::lgt::Light2D> activeLights;
202  bool lightsActive;
203 
204  float levelRange;
205  float sunlightFactor;
206  int weatherModifier;
207  int targetWeatherModifier;
208  float weatherResidual;
209  float thunderTime;
210 
211  std::uint8_t computeAmbient() const;
212  void addLightToScene(const Light& light);
213  void updateAmbientLighting();
214 
215  friend struct bl::serial::SerializableObject<LightingSystem>;
216 };
217 
218 } // namespace map
219 } // namespace core
220 
221 namespace bl
222 {
223 namespace serial
224 {
225 template<>
226 struct SerializableObject<core::map::LightingSystem> : public SerializableObjectBase {
228 
229  SerializableField<1, LS, std::vector<core::map::Light>> lights;
230  SerializableField<2, LS, std::uint8_t> low;
231  SerializableField<3, LS, std::uint8_t> high;
232  SerializableField<4, LS, std::uint8_t> sun;
233 
235  : SerializableObjectBase("LightingSystem")
236  , lights("lights", *this, &LS::rawLights, SerializableFieldBase::Required{})
237  , low("low", *this, &LS::minLevel, SerializableFieldBase::Required{})
238  , high("high", *this, &LS::maxLevel, SerializableFieldBase::Required{})
239  , sun("sun", *this, &LS::sunlight, SerializableFieldBase::Required{}) {}
240 };
241 
242 } // namespace serial
243 } // namespace bl
244 
245 #endif
Core classes and functionality for both the editor and game.
Basic event for when the game time changes.
Definition: TimeChange.hpp:24
Fired when weather starts.
Definition: Weather.hpp:15
Fired when weather stops.
Definition: Weather.hpp:33
Fired when thunder happens.
Definition: Weather.hpp:51
Represents a renderable light in a Map.
Definition: Light.hpp:16
System for handling lighting in Maps. Manages all the lights and performs rendering....
virtual ~LightingSystem()=default
Destroys the lighting system.
std::uint16_t Handle
Handle representing a light in the map.
std::uint8_t getMaxLightLevel() const
Returns the maximum ambient light level of this map.
void setColorTint(const glm::vec3 &tint)
Sets a color tint to apply to the ambient lighting.
std::uint8_t getMinLightLevel() const
Returns the minimum ambient light level of this map.
LightingSystem()
Initializes the lighting system with no lights.
const Light & getLight(Handle handle) const
Returns the value of the light with the given handle. Invalid handles return a default value (a light...
void addLight(const Light &light)
Adds a light to the map and to the persistent map file.
void update(float dt)
Updates the lighting system.
void adjustForSunlight(bool adjust)
Set whether or not the light level is adjusted based on time of day. If not adjusting for sunlight th...
void unsubscribe()
Unsubscribes the lighting system from the event dispatcher.
void setAmbientLevel(std::uint8_t lowLightLevel, std::uint8_t highLightLevel)
The ambient light band. 0 is full darkness and 255 is full brightness.
void updateLight(Handle handle, const Light &value)
Updates the light with the given handle to the new information.
bool lightsAreOn() const
Returns whether lights are being shown or not based on how dark it is.
Handle getClosestLight(const sf::Vector2i &position)
Returns a handle to the light closest to the given position.
static constexpr Handle None
Special handle indicating that no light is referenced.
void removeLight(Handle light)
Remove the given light from the system and optionally persist the removal.
void activate(bl::rc::lgt::Scene2DLighting &sceneLighting)
Adds all lights into the scene.
void subscribe()
Subscribes the lighting system to time events for ambient light level.
virtual void observe(const event::TimeChange &timeChange) override
Updates the light level based on the new current time.
bool adjustsForSunlight() const
Returns whether or not this map is adjusted for time of day.
void clear()
Clears all lights from the map, including the persisted light data.
SerializableField< 1, LS, std::vector< core::map::Light > > lights