diff options
Diffstat (limited to 'src/engine/clock.js')
-rw-r--r-- | src/engine/clock.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/engine/clock.js b/src/engine/clock.js new file mode 100644 index 0000000..e817863 --- /dev/null +++ b/src/engine/clock.js @@ -0,0 +1,26 @@ +import { Engine } from "../engine.js" + +export { Engine } from "../engine.js" +const fps = 60 + +/* cap the game's fps */ +let msPrev = window.performance.now() +const msPerFrame = 1000 / fps +function tick() { + window.requestAnimationFrame(tick) + + const msNow = window.performance.now() + const msPassed = msNow - msPrev + + if (msPassed < msPerFrame) return + + const excessTime = msPassed % msPerFrame + msPrev = msNow - excessTime + + Engine.frame++ + Engine.updateEngine() +} + +export function startClock() { + tick() +} |