Created
November 11, 2022 08:50
-
-
Save takumifukasawa/b0dfa91c5030cded88a13cbdc4bbb66a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TimeSkipper { | |
targetFPS; | |
#callback; | |
#lastTime; | |
constructor(targetFPS, callback) { | |
this.targetFPS = targetFPS; | |
this.#callback = callback; | |
} | |
// time [sec] | |
start(time) { | |
this.#lastTime = time; | |
} | |
// time [sec] | |
exec(time) { | |
const interval = 1 / this.targetFPS; | |
if((time - interval) >= this.#lastTime) { | |
const elapsedTime = time - this.#lastTime; | |
const n = Math.floor(elapsedTime / interval); | |
const deltaTime = interval * n; | |
this.#lastTime += deltaTime; | |
this.#callback(this.#lastTime, deltaTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment