Peoplemon  0.1.0
Peoplemon 3 game source documentation
Clock.cpp
Go to the documentation of this file.
1 #include <Core/Systems/Clock.hpp>
2 
6 #include <cmath>
7 
8 namespace core
9 {
10 namespace system
11 {
13 : owner(owner)
14 , currentTime(12, 0, 1)
15 , residual(0.f) {}
16 
17 const Clock::Time& Clock::now() const { return currentTime; }
18 
19 void Clock::update(std::mutex&, float dt, float, float, float) {
20  residual += dt;
21  const float minutes = std::floor(residual);
22  if (minutes > 0) {
23  residual -= minutes; // 1 real second = 1 game minute
24  currentTime.addMinutes(static_cast<unsigned int>(minutes + 0.1f));
25  bl::event::Dispatcher::dispatch<event::TimeChange>({currentTime});
26  }
27 }
28 
29 void Clock::set(const Time& n) {
30  currentTime = n;
31  bl::event::Dispatcher::dispatch<event::TimeChange>({currentTime});
32 }
33 
35 : day(0)
36 , hour(12)
37 , minute(0) {}
38 
39 Clock::Time::Time(unsigned int h, unsigned int m, unsigned int d)
40 : day(d)
41 , hour(h)
42 , minute(m) {}
43 
44 void Clock::Time::addMinutes(unsigned int m) {
45  const unsigned int nm = minute + m;
46  const unsigned int h = nm / 60;
47  minute = nm % 60;
48  if (h > 0) addHours(h);
49 }
50 
51 void Clock::Time::addHours(unsigned int h) {
52  const unsigned int nh = hour + h;
53  const unsigned int d = nh / 24;
54  hour = nh % 24;
55  day += d;
56 }
57 
58 bool Clock::Time::operator==(const Time& right) const {
59  return minute == right.minute && hour == right.hour &&
60  (right.day == 0 || day == 0 || day == right.day);
61 }
62 
63 bool Clock::Time::operator<(const Time& right) const {
64  if (day != 0 && right.day != 0) {
65  if (day < right.day) return true;
66  if (day > right.day) return false;
67  }
68  if (hour < right.hour)
69  return true;
70  else if (hour == right.hour)
71  return minute < right.minute;
72  return false;
73 }
74 
75 bool Clock::Time::operator>(const Time& right) const {
76  if (day != 0 && right.day != 0) {
77  if (day < right.day) return false;
78  if (day > right.day) return true;
79  }
80  if (hour > right.hour)
81  return true;
82  else if (hour == right.hour)
83  return minute > right.minute;
84  return false;
85 }
86 
88  save.gameSave.clock.time = &currentTime;
89 }
90 
92  bl::event::Dispatcher::dispatch<event::TimeChange>({currentTime});
93 }
94 
95 void Clock::init(bl::engine::Engine&) {
96  bl::event::Dispatcher::subscribe(this);
97  currentTime = {};
98 }
99 
100 } // namespace system
101 } // namespace core
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
file::GameSave & gameSave
Game save being initialized.
Definition: GameSave.hpp:24
Fired when a game save is loaded. Fired after the load is complete.
Definition: GameSave.hpp:46
struct core::file::GameSave::ClockPointers clock
system::Clock::Time * time
Definition: GameSave.hpp:68
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
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
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
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47