diff options
Diffstat (limited to 'src/controller')
-rw-r--r-- | src/controller/gamecontext.ts | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index d6d02d1..9c25c6b 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -41,7 +41,8 @@ export class GameContext implements CollidableGroup { return [await loadMap(renderer, map), mkCollision(map.collision)]; } - private time: number|null = null; + private readonly initTime: number = performance.now(); + private time = 0; private readonly tick = 10; // ms per tick private readonly maxSpeed = 0.04; // movement per tick @@ -93,7 +94,7 @@ export class GameContext implements CollidableGroup { } private updateTime(time: number): number { - const diff = this.time !== null ? time - this.time : 0; + const diff = Math.round(time / this.tick) - Math.round(this.time / this.tick); this.time = time; return diff; @@ -157,8 +158,9 @@ export class GameContext implements CollidableGroup { this.updateStep(); } - private render = (time: number) => { - this.update(Math.round(time / this.tick)); + private render = (curTime: number) => { + const time = curTime - this.initTime; + this.update(time); this.renderer.setCenter(this.player.pos); this.renderer.clear(); |