Timer
Waiting three seconds before spawning a wave, or firing an event once a cooldown expires, shouldn't require manually accumulating delta time in a variable every frame.
Overview
Timer is contained in the header "remake2d/time.hpp". It counts down toward a set limit, and emits a signal once
elapsed, without needing to be polled manually every frame.
Methods
void limit(fmax) noexcept; // set limit, converse in seconds
void limit(time::Second) noexcept; // set limit
fmax limit(void) const noexcept; // get limit
void start(void) noexcept; // start or restart the timer
void stop(void) noexcept; // stop and reset
void pause(void) noexcept; // pause without resetting
void resume(void) noexcept; // resume after pause
void repeat(bool) noexcept; // auto-restart on timeout
bool isActive(void) const noexcept; // check if currently running
bool isElapsed(void) const noexcept; // check if timeout occurred
fmax elapsedTime(void) const noexcept; // current elapsed time
_TimerSignal<> onTimeout;
Usage
Creating a timer
A limit can be passed directly at construction, or set afterwards:
Starting and controlling
Start resets the elapsed time and begins counting; pause and resume freeze and unfreeze it without losing progress:
Reacting to timeout
onTimeout fires once when the timer reaches its limit, which is where most of the actual logic belongs rather than
polling isElapsed every frame:
Repeating
Repeat makes a timer restart itself automatically as soon as it elapses, useful for anything on a fixed interval, like a wave spawner or a periodic heal tick: