diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-11 00:15:24 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-11 00:40:46 +0100 |
commit | 426b2c37ff72d8de40f716365c8301449acfdfcb (patch) | |
tree | 4c58f706be13e859231b3435c6eacf37e3c6fcc7 | |
parent | 3c83dade65cb00cd003139d14106e7283881ef80 (diff) | |
download | rpgedit-426b2c37ff72d8de40f716365c8301449acfdfcb.tar rpgedit-426b2c37ff72d8de40f716365c8301449acfdfcb.zip |
controller: gamecontext: count actual game time
-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(); |