Timers
using Clockwork.Utilities;
There are so many ways a game framework can implement timers. Clockwork takes a new, elegant approach. Timers act like entities, and sync with a scene's timing systems. Timers pause with the scene. Timers speed up and slow down with the scene. Timers are set and go, and then destroy themselves when they are finished. They can even be inherited from for when you need the same timer, over and over.
Trigger Timer
The trigger timer is simply a timer. It runs for a given amount of time, and then it triggers an event and destroys itself:
TriggerTimer waveTimer = scene.AddEntity(new TriggerTimer(waveSeconds));
waveTimer.Triggered += NextWave;
waveTimer.RemoveOnTriggered = false;
waveTimer.Start();
void NextWave()
{
// trigger next wave
waveTimer.Start(); // restart timer
}
You can also call Skip() to immediately trigger the timer. The OnTriggered() method is virtual, so subclasses can override it instead of subscribing to the event.
Fire Timer
The fire timer is a timer that fires over and over and over. They keep track of sub-frame time, in case your timer is ever faster than your frame rate. It's great for repetitive tasks, like shooting projectiles from a weapon:
FireTimer fireTimer = scene.AddEntity(new FireTimer(cooldownSeconds));
fireTimer.Fired += ShootProjectile;
fireTimer.Start();
void ShootProjectile()
{
// create a projectile here!
}
The cooldown time can be changed at runtime through fireTimer.CooldownTime. The OnFired() method is virtual for subclassing.