Download bobs game/src/core/GameLoop.js from Snapkitty/sov-kernel-monster: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/bobs%20game/src/core/GameLoop.js
- Command line
-
hf download 'hf://Snapkitty/sov-kernel-monster/bobs game/src/core/GameLoop.js'
-
curl -L -o GameLoop.js https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/bobs%20game/src/core/GameLoop.js
1.05 kB
| export class GameLoop { | |
| constructor({ fixedUpdate, update, render, fixedStep = 1 / 60 }) { | |
| this.fixedUpdate = fixedUpdate; | |
| this.update = update; | |
| this.render = render; | |
| this.fixedStep = fixedStep; | |
| this.accumulator = 0; | |
| this.lastTime = 0; | |
| this.running = false; | |
| this.frame = time => this.tick(time); | |
| } | |
| start() { | |
| if (this.running) return; | |
| this.running = true; | |
| this.lastTime = performance.now(); | |
| requestAnimationFrame(this.frame); | |
| } | |
| stop() { | |
| this.running = false; | |
| } | |
| tick(time) { | |
| if (!this.running) return; | |
| const delta = Math.min(0.05, Math.max(0, (time - this.lastTime) / 1000)); | |
| this.lastTime = time; | |
| this.accumulator = Math.min(this.accumulator + delta, this.fixedStep * 5); | |
| while (this.accumulator >= this.fixedStep) { | |
| this.fixedUpdate(this.fixedStep); | |
| this.accumulator -= this.fixedStep; | |
| } | |
| this.update(delta); | |
| this.render(this.accumulator / this.fixedStep); | |
| requestAnimationFrame(this.frame); | |
| } | |
| } | |