Skip to main content

TimerManager.ixx File

Manager for game timers and timer command handling. More...

Included Headers

#include <cassert> #include <memory> #include <span> #include <vector> #include <stdexcept> #include <ranges> #include <helios.engine.common> #include <helios.core.types> #include <helios.engine.runtime.world.UpdateContext> #include <helios.engine.mechanics.timing.types.GameTimerId> #include <helios.engine.mechanics.timing.GameTimer> #include <helios.engine.mechanics.timing.commands> #include <helios.util.Guid> #include <helios.engine.runtime.world.GameWorld> #include <helios.engine.mechanics.timing.types>

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

classTimerManager

Manager that owns game timers and processes timer control commands. More...

Description

Manager for game timers and timer command handling.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file TimerManager.ixx
3 * @brief Manager for game timers and timer command handling.
4 */
5module;
6
7#include <cassert>
8#include <memory>
9#include <span>
10#include <vector>
11#include <stdexcept>
12#include <ranges>
13
14export module helios.engine.mechanics.timing.TimerManager;
15
16import helios.engine.mechanics.timing.types;
17import helios.engine.mechanics.timing.commands;
18
19import helios.engine.mechanics.timing.GameTimer;
20
21import helios.engine.mechanics.timing.types.GameTimerId;
22
23import helios.engine.runtime.world.UpdateContext;
24
25import helios.engine.runtime.world.GameWorld;
26
27import helios.core.types;
28import helios.util.Guid;
29import helios.engine.common;
30
36
38
39 /**
40 * @brief Manager that owns game timers and processes timer control commands.
41 *
42 *
43 * Pending control commands are collected via submit() and applied during
44 * flush() at the beginning of each frame.
45 *
46 * @see GameTimer
47 * @see TimerCommandHandler
48 * @see Manager
49 */
51
52 /**
53 * @brief Collection of game timers managed by this manager.
54 */
55 std::vector<GameTimer> gameTimers_;
56
57 /**
58 * @brief Pending timer control contexts to be applied during flush.
59 */
60 std::vector<TimerControlContext> pendingControlContexts_;
61
62 /**
63 * @brief Checks whether a timer with the given id exists.
64 *
65 * @param timerId The id to look up.
66 *
67 * @return True if a timer with the given id is registered.
68 */
69 [[nodiscard]] bool has(const GameTimerId timerId) noexcept {
70 return getGameTimer(timerId) != nullptr;
71 }
72
73 /**
74 * @brief Looks up a timer by its id.
75 *
76 * @param timerId The id to search for.
77 *
78 * @return Pointer to the timer, or nullptr if not found.
79 */
80 GameTimer* getGameTimer(const GameTimerId timerId) {
81 const auto timer = std::ranges::find_if(
82 gameTimers_,
83 [&](const auto& gameTimer) {
84 return gameTimer.gameTimerId() == timerId;
85 });
86
87 if (timer == gameTimers_.end()) {
88 return nullptr;
89 }
90 return &*timer;
91 }
92
93 /**
94 * @brief Creates and appends a new timer.
95 *
96 * @param gameTimerId The id for the new timer.
97 *
98 * @return Reference to the newly created timer.
99 */
100 GameTimer& add(const GameTimerId gameTimerId) noexcept {
101 gameTimers_.emplace_back(GameTimer(gameTimerId));
102
103 return gameTimers_.back();
104 }
105
106 public:
108
109 /**
110 * @brief Registers a new game timer.
111 *
112 * Asserts that no timer with the given id already exists.
113 *
114 * @param gameTimerId The unique id for the new timer.
115 *
116 * @return Reference to the newly created GameTimer.
117 */
118 GameTimer& addGameTimer(GameTimerId gameTimerId) noexcept {
119 assert(!has(gameTimerId) && "GameTimer with GameTimerId already registered");
120
121 return add(gameTimerId);
122 }
123
124 /**
125 * @brief Returns a pointer to the timer with the given id.
126 *
127 * @param gameTimerId The id to look up.
128 *
129 * @return Pointer to the GameTimer, or nullptr if not found.
130 */
131 [[nodiscard]] GameTimer* gameTimer(const GameTimerId gameTimerId) noexcept {
132 return getGameTimer(gameTimerId);
133 }
134
135 /**
136 * @brief Returns a span over all registered timers.
137 *
138 * @return A span of GameTimer instances.
139 */
140 [[nodiscard]] std::span<GameTimer> gameTimers() noexcept {
141 return gameTimers_;
142 }
143
144 /**
145 * @brief Applies all pending timer control contexts.
146 *
147 * For each pending context, the corresponding timer's state is updated.
148 * The pending list is cleared after processing.
149 *
150 * @param gameWorld Reference to the game world.
151 * @param updateContext Reference to the current update context.
152 */
153 void flush(
155 ) noexcept {
156
157 for (const auto& controlContext : pendingControlContexts_) {
158 auto* timer = gameTimer(controlContext.gameTimerId);
159 if (timer) {
160 if (controlContext.resetElapsed) {
161 timer->reset(controlContext.timerState);
162 } else {
163 timer->setState(controlContext.timerState);
164 }
165 }
166 }
167 pendingControlContexts_.clear();
168 }
169
170 /**
171 * @brief Enqueues a timer control command for deferred processing.
172 *
173 * @param timerControlCommand The command to enqueue.
174 *
175 * @return True if the command was accepted.
176 */
177 bool submit(const TimerControlCommand timerControlCommand) noexcept {
178 pendingControlContexts_.push_back(timerControlCommand.timerControlContext());
179 return true;
180 };
181
182 /**
183 * @brief Registers this manager as the timer command handler in the game world.
184 *
185 * @param gameWorld The game world to register with.
186 */
188 gameWorld.template registerCommandHandler<TimerControlCommand>(*this);
189 }
190
191 /**
192 * @brief Resets all managed timers.
193 */
194 void reset() {
195 for (auto& gameTimer : gameTimers_) {
196 gameTimer.reset();
197 }
198 }
199 };
200
201}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.