Wait and repeat
You want something to happen later (the shot reloading, the bomb exploding) or from time to time (an enemy shooting every second).
#include <brasa/brasa.hpp>using namespace brasa;
class Bomb : public Entity {public: void onSpawn() override { afterSeconds(3, [this] { destroy(); }); // explodes in 3 seconds every(30, [this] { visible = !visible; }); // blinks every half second }};BRASA_ENTITY(Bomb);How it works
Section titled “How it works”- The game runs at 60 steps per second.
after(60, ...)waits 1 second, andafter(30, ...)waits half. after(steps, ...)runs the lambda once, after that many steps.every(steps, ...)runs it always, every that many steps.afterSeconds(seconds, ...)isaftercounted in seconds.- The timers go away by themselves when the object is destroyed.
Variations
Section titled “Variations”Cancel. Every timer function returns a TimerId. Keep it to cancel later:
// in the class: TimerId blink_;blink_ = every(30, [this] { visible = !visible; });// ... later:cancel(blink_);visible = true;An interval that changes. A function that schedules itself again with after can change the interval each time, like the Spawner of the tutorial.
