From 426b2c37ff72d8de40f716365c8301449acfdfcb Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 11 Nov 2018 00:15:24 +0100 Subject: controller: gamecontext: count actual game time --- src/controller/gamecontext.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/controller') 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(); -- cgit v1.2.3