ridder
Version:
A straightforward game engine for simple data-driven games in JavaScript
24 lines (23 loc) • 586 B
JavaScript
import { getDeltaTime } from "./state.js";
/**
* Create a new timer data structure.
*/
export function timer() {
return { elapsed: 0 };
}
/**
* Advance the timer by the time since the last frame, returns `true` if the timer has reached the duration this frame.
*/
export function tickTimer(t, duration) {
if (duration <= 0 || t.elapsed >= duration) {
return false;
}
t.elapsed = Math.min(t.elapsed + getDeltaTime(), duration);
return t.elapsed === duration;
}
/**
* Reset the timer back to zero.
*/
export function resetTimer(t) {
t.elapsed = 0;
}