Skip to main content

GameTimer.ixx File

A game timer that tracks elapsed time and supports state transitions. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacemechanics

High-level gameplay systems and components for game logic. More...

namespacetiming

Game timer management system. More...

Classes Index

classGameTimer

A game timer identified by a GameTimerId. More...

Description

A game timer that tracks elapsed time and supports state transitions.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file GameTimer.ixx
3 * @brief A game timer that tracks elapsed time and supports state transitions.
4 */
5module;
6
7export module helios.engine.mechanics.timing.GameTimer;
8
9import helios.engine.mechanics.timing.types.GameTimerId;
10
11import helios.engine.mechanics.timing.types;
12
15
17
18 /**
19 * @brief A game timer identified by a GameTimerId.
20 *
21 * Tracks elapsed time while in the Running state. Each update increments
22 * a revision counter that observers can use to detect changes.
23 *
24 * @see TimerManager
25 * @see GameTimerBindingComponent
26 */
27 class GameTimer {
28
29 /**
30 * @brief Monotonically increasing revision counter, incremented on each update.
31 */
32 TimerRevision timerRevision_{};
33
34 /**
35 * @brief Unique identifier for this timer.
36 */
37 GameTimerId gameTimerId_;
38
39 /**
40 * @brief Accumulated elapsed time in seconds.
41 */
42 float elapsed_{};
43
44 /**
45 * @brief Current state of the timer.
46 */
47 TimerState timerState_{};
48
49 /**
50 * @brief Optional duration limit in seconds.
51 */
52 float duration_ = 0.0f;
53
54 public:
55
56 /**
57 * @brief Constructs a GameTimer with the given identifier.
58 *
59 * @param gameTimerId The unique identifier for this timer.
60 */
62 : gameTimerId_{gameTimerId} {}
63
64 /**
65 * @brief Returns the timer identifier.
66 *
67 * @return The GameTimerId assigned to this timer.
68 */
70 return gameTimerId_;
71 }
72
73 /**
74 * @brief Resets the elapsed time to zero and sets timer state to Undefined.
75 *
76 * @param state The new TimerState the timer should be resetted to.
77 */
78 void reset(const TimerState state = TimerState::Undefined) noexcept {
79 elapsed_ = 0.0f;
81 }
82
83 /**
84 * @brief Resets timer by resetting its internal counter and
85 * setting its state to TimerState::Running.
86 *
87 * @see reset
88 */
89 void restart() noexcept {
91 }
92
93 /**
94 * @brief Cancels this timer by setting its state to TimerState::Cancelled.
95 *
96 * @see reset
97 */
98 void cancel() noexcept {
100 }
101
102 /**
103 * @brief Sets the duration limit.
104 *
105 * @param duration Duration in seconds.
106 */
107 void setDuration(float duration) {
108 duration_ = duration;
109 }
110
111 /**
112 * @brief Returns the duration limit.
113 *
114 * @return The duration in seconds.
115 */
116 [[nodiscard]] float duration() const noexcept {
117 return duration_;
118 }
119
120 /**
121 * @brief Returns the accumulated elapsed time.
122 *
123 * @return Elapsed time in seconds.
124 */
125 [[nodiscard]] float elapsed() const noexcept {
126 return elapsed_;
127 }
128
129 /**
130 * @brief Returns the current timer state.
131 *
132 * @return The current TimerState.
133 */
134 [[nodiscard]] TimerState state() const noexcept {
135 return timerState_;
136 }
137
138 /**
139 * @brief Sets the timer state.
140 *
141 * @param state The new TimerState.
142 */
143 void setState(const TimerState state) noexcept {
144 timerState_ = state;
145 timerRevision_++;
146 }
147
148 /**
149 * @brief Returns whether this timer should accumulate time.
150 *
151 * @return True if the timer is in the Running state.
152 */
153 [[nodiscard]] bool shouldUpdate() const noexcept {
154 return (timerState_ == TimerState::Running);
155 }
156
157 /**
158 * @brief Returns whether this timer's internal state represents the Finished-state.
159 *
160 * @return True if the timer is finished.
161 */
162 [[nodiscard]] bool isFinished() const noexcept {
163 return (timerState_ == TimerState::Finished);
164 }
165
166 /**
167 * @brief Returns the current revision counter.
168 *
169 * @return The TimerRevision value.
170 */
171 [[nodiscard]] TimerRevision timerRevision() const noexcept {
172 return timerRevision_;
173 }
174
175 /**
176 * @brief Advances the timer by the given frame time.
177 *
178 * Only accumulates time when the timer is in the Running state.
179 * Increments the revision counter on each successful update.
180 *
181 * @param ft Frame time in seconds.
182 */
183 void update(const float ft) {
184 if (!shouldUpdate() || ft == 0.0f) {
185 return;
186 }
187 timerRevision_++;
188 elapsed_ += ft;
189 }
190
191 };
192
193}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.