Initial timer implementation.

This commit is contained in:
2026-07-24 08:27:55 +02:00
parent 211bcdc621
commit 2bd88c9a1b
+59
View File
@@ -0,0 +1,59 @@
package game
// Purpose of Timer: a small reusable piece of state that tracks:
// "how much time has passed of a set duration,"
// (spawning, cooldowns, etc.) can ask "has my interval finished yet"
// without each system hand-rolling its own countdown variable.
Timer :: struct {
active: bool,
fired: bool,
loop: bool,
elapsed: f32,
duration: f32
}
init_timer :: proc(duration: f32, looping:bool) -> Timer {
timer := Timer{
active = true,
loop = looping,
duration = duration,
}
return timer
}
reset_timer :: proc(timer: ^Timer) {
timer.active = true
timer.fired = false
timer.elapsed = 0
}
// returns true when the timer has just fired this frame
update_timer :: proc(timer: ^Timer, dt: f32) -> bool {
if timer.active {
// increment the timer
timer.elapsed += dt
if timer.elapsed >= timer.duration {
timer.fired = true
// reset timer now
// @NOTE:
// dont set to 0 use the timers duration
// accounts leftover overshoot time isn't lost
// keeps long-term timing accurate rather than drifting
timer.elapsed -= timer.duration
// Deactivate 1 shot timers
if timer.loop == false {
timer.active = false
}
return true
}
}
return false
}