Peoplemon  0.1.0
Peoplemon 3 game source documentation
Clock.hpp
Go to the documentation of this file.
1 #ifndef CORE_SYSTEMS_CLOCK_HPP
2 #define CORE_SYSTEMS_CLOCK_HPP
3 
4 #include <BLIB/Engine/System.hpp>
5 #include <BLIB/Serialization.hpp>
6 #include <BLIB/Util/NonCopyable.hpp>
8 #include <iomanip>
9 #include <ostream>
10 
11 namespace core
12 {
13 namespace system
14 {
15 class Systems;
16 
22 class Clock
23 : private bl::util::NonCopyable
24 , public bl::engine::System
25 , public bl::event::Listener<event::GameSaveInitializing, event::GameSaveLoaded> {
26 public:
32  struct Time {
34  unsigned int day;
35 
37  unsigned int hour;
38 
40  unsigned int minute;
41 
46  Time();
47 
55  Time(unsigned int hour, unsigned int minute, unsigned int day = 0);
56 
62  void addMinutes(unsigned int minutes);
63 
69  void addHours(unsigned int hours);
70 
77  bool operator==(const Time& right) const;
78 
86  bool operator<(const Time& right) const;
87 
95  bool operator>(const Time& right) const;
96 
97  friend std::ostream& operator<<(std::ostream& stream, const Time& time) {
98  stream << std::setfill('0') << std::setw(2) << time.hour << ":" << std::setfill('0')
99  << std::setw(2) << time.minute;
100  return stream;
101  }
102  };
103 
109  Clock(Systems& owner);
110 
114  virtual ~Clock() = default;
115 
121  const Time& now() const;
122 
128  void set(const Time& time);
129 
133  virtual void observe(const event::GameSaveInitializing& save) override;
134 
138  virtual void observe(const event::GameSaveLoaded& load) override;
139 
140 private:
141  Systems& owner;
142  Time currentTime;
143  float residual;
144 
145  virtual void init(bl::engine::Engine&) override;
146  virtual void update(std::mutex&, float dt, float, float, float) override;
147 };
148 
149 } // namespace system
150 } // namespace core
151 
152 namespace bl
153 {
154 namespace serial
155 {
156 template<>
157 struct SerializableObject<core::system::Clock::Time> : public SerializableObjectBase {
159 
160  SerializableField<1, T, unsigned int> day;
161  SerializableField<2, T, unsigned int> hour;
162  SerializableField<3, T, unsigned int> minute;
163 
165  : SerializableObjectBase("ClockTime")
166  , day("day", *this, &T::day, SerializableFieldBase::Required{})
167  , hour("hour", *this, &T::hour, SerializableFieldBase::Required{})
168  , minute("minute", *this, &T::minute, SerializableFieldBase::Required{}) {}
169 };
170 
171 } // namespace serial
172 } // namespace bl
173 
174 #endif
Core classes and functionality for both the editor and game.
Fired when the game is saving or loading. Allows systems to hook in their data.
Definition: GameSave.hpp:22
Fired when a game save is loaded. Fired after the load is complete.
Definition: GameSave.hpp:46
Simple time keeping systems. Tracks in game time and date based on real elapsed play time.
Definition: Clock.hpp:25
const Time & now() const
Returns the current in game time.
Definition: Clock.cpp:17
virtual void observe(const event::GameSaveInitializing &save) override
Adds saved clock data to the save file.
Definition: Clock.cpp:87
virtual ~Clock()=default
Destroys the system.
Clock(Systems &owner)
Initializes the clock system.
Definition: Clock.cpp:12
void set(const Time &time)
Sets the current time.
Definition: Clock.cpp:29
Simple struct representing a point in time.
Definition: Clock.hpp:32
unsigned int minute
Current minute of the hour. In range [0, 59].
Definition: Clock.hpp:40
Time()
Construct a new Time at noon of day 0.
Definition: Clock.cpp:34
unsigned int day
Number of days elapsed in game since beginning. Starts at 1.
Definition: Clock.hpp:34
bool operator<(const Time &right) const
Checks to see if a given time appears later than the current time. Counts days unless either day is 0...
Definition: Clock.cpp:63
void addHours(unsigned int hours)
Adds the number of hours to this time. Wraps days.
Definition: Clock.cpp:51
friend std::ostream & operator<<(std::ostream &stream, const Time &time)
Definition: Clock.hpp:97
unsigned int hour
Current hour of the day, in range [0, 23].
Definition: Clock.hpp:37
bool operator>(const Time &right) const
Checks to see if a given time appears earlier than the current time. Counts days unless either day is...
Definition: Clock.cpp:75
void addMinutes(unsigned int minutes)
Adds the amount of minutes to this time. Wraps hours and days.
Definition: Clock.cpp:44
bool operator==(const Time &right) const
Checks if two times are the same. Compares days as well unless either day is 0.
Definition: Clock.cpp:58
SerializableField< 1, T, unsigned int > day
Definition: Clock.hpp:160
SerializableField< 2, T, unsigned int > hour
Definition: Clock.hpp:161
SerializableField< 3, T, unsigned int > minute
Definition: Clock.hpp:162
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47