controller: gamecontext: count actual game time
This commit is contained in:
parent
3c83dade65
commit
426b2c37ff
1 changed files with 6 additions and 4 deletions
|
@ -41,7 +41,8 @@ export class GameContext implements CollidableGroup {
|
||||||
return [await loadMap(renderer, map), mkCollision(map.collision)];
|
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 tick = 10; // ms per tick
|
||||||
private readonly maxSpeed = 0.04; // movement per tick
|
private readonly maxSpeed = 0.04; // movement per tick
|
||||||
|
@ -93,7 +94,7 @@ export class GameContext implements CollidableGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateTime(time: number): number {
|
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;
|
this.time = time;
|
||||||
|
|
||||||
return diff;
|
return diff;
|
||||||
|
@ -157,8 +158,9 @@ export class GameContext implements CollidableGroup {
|
||||||
this.updateStep();
|
this.updateStep();
|
||||||
}
|
}
|
||||||
|
|
||||||
private render = (time: number) => {
|
private render = (curTime: number) => {
|
||||||
this.update(Math.round(time / this.tick));
|
const time = curTime - this.initTime;
|
||||||
|
this.update(time);
|
||||||
|
|
||||||
this.renderer.setCenter(this.player.pos);
|
this.renderer.setCenter(this.player.pos);
|
||||||
this.renderer.clear();
|
this.renderer.clear();
|
||||||
|
|
Reference in a new issue