From a840990bdcabf45fb0d377478ba0ab27222434ae Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Mon, 23 Dec 2024 18:32:29 +0100 Subject: initial commit --- src/engine/clock.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/engine/clock.js (limited to 'src/engine/clock.js') 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() +} -- cgit v1.2.3