Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
timers.hpp
Go to the documentation of this file.
1
4#pragma once
8
9#include <algorithm>
10#include <cstdint>
11#include <vector>
12
13namespace aegis2d::ecs {
14
22struct Timer {
24 float period = 1.0F;
26 float left = 1.0F;
28 bool repeat = true;
30 bool running = true;
32 bool done = false;
33
37 [[nodiscard]] static constexpr Timer every(float seconds) noexcept {
38 return Timer{.period = seconds, .left = seconds, .repeat = true};
39 }
40
44 [[nodiscard]] static constexpr Timer after(float seconds) noexcept {
45 return Timer{.period = seconds, .left = seconds, .repeat = false};
46 }
47};
48
56inline std::uint32_t advance(Timer& timer, float seconds) noexcept {
57 if (!timer.running || timer.done) {
58 return 0;
59 }
60 timer.left -= std::max(seconds, 0.0F);
61 if (timer.left > 0.0F) {
62 return 0;
63 }
64 if (!timer.repeat) {
65 timer.done = true;
66 timer.left = 0.0F;
67 return 1;
68 }
69 if (timer.period <= 0.0F) {
70 timer.left = 0.0F;
71 return 1;
72 }
73 std::uint32_t fires = 0;
74 while (timer.left <= 0.0F) {
75 timer.left += timer.period;
76 ++fires;
77 }
78 return fires;
79}
80
91inline void fire_timers(Registry& registry, const Tick& tick, std::vector<Entity>& fired) {
92 fired.clear();
93 const float delta = tick.delta();
94 registry.view<Timer>().each([delta, &fired](Entity entity, Timer& timer) {
95 for (std::uint32_t fires = advance(timer, delta); fires > 0; --fires) {
96 fired.push_back(entity);
97 }
98 });
99}
100
101} // namespace aegis2d::ecs
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
View< Components... > view()
View on the entities that own all the listed components.
Definition registry.hpp:222
Generational entity handle: an index into the component pools plus a generation.
Entity-Component-System used by client and server.
Definition animation.hpp:13
void fire_timers(Registry &registry, const Tick &tick, std::vector< Entity > &fired)
Advances every Timer by one tick and lists the entities whose timer fired.
Definition timers.hpp:91
std::uint32_t advance(Animation &animation, float seconds) noexcept
Moves an animation forward and tells which frame it shows.
Definition animation.hpp:21
Registry: owns the entities and one component pool per component type.
One simulation step, as the systems see it.
Definition system.hpp:23
constexpr float delta() const noexcept
Duration of the step in seconds, to scale values given per second.
Definition system.hpp:32
Fires every period seconds (a tower that shoots, a wave every 30 s), or once after a delay (a bomb,...
Definition timers.hpp:22
bool running
false while paused: the time does not run.
Definition timers.hpp:30
static constexpr Timer after(float seconds) noexcept
A timer that fires once, seconds from now.
Definition timers.hpp:44
bool done
true once a one-shot timer has fired; it never fires again.
Definition timers.hpp:32
float left
Seconds before the next fire.
Definition timers.hpp:26
float period
Seconds between two fires of a repeating timer; not used by a one-shot one.
Definition timers.hpp:24
bool repeat
true to fire every period, false to fire once.
Definition timers.hpp:28
static constexpr Timer every(float seconds) noexcept
A timer that fires every seconds, the first time seconds from now.
Definition timers.hpp:37
Systems and the scheduler that updates them in order, once per tick.