diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-01 00:44:49 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-01 00:44:49 +0100 |
commit | 22b06efe2c2646a01535b5bbf5f3cc57de81ad0d (patch) | |
tree | c6d886e3d50418cf1e73911e4df4234d3507a6bb | |
parent | 1ae0884a7376a87a2df4a77d1ba861cd58a4dda7 (diff) | |
download | rpgedit-22b06efe2c2646a01535b5bbf5f3cc57de81ad0d.tar rpgedit-22b06efe2c2646a01535b5bbf5f3cc57de81ad0d.zip |
controller: more GameContext cleanup, introduce ticks
-rw-r--r-- | src/controller/gamecontext.ts | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index 4d86abf..792e160 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -12,18 +12,13 @@ import { vec2 } from 'gl-matrix'; export class GameContext { public static async load(renderer: Renderer): Promise<GameContext> { - const [ - mapView, - entity, - ] = await Promise.all([ - GameContext.loadMap(renderer), - loadSimpleEntity(renderer, 'simple_square'), - ]); + const mapView = this.loadMap(renderer); + const entity = loadSimpleEntity(renderer, 'simple_square'); return new GameContext( renderer, - mapView, - entity, + await mapView, + await entity, ); } @@ -34,7 +29,9 @@ export class GameContext { private time: number|null = null; - private readonly speed = 1 / 100; + private readonly tick = 10; // ms per tick + private readonly speed = 0.05; // movement per tick + private readonly maxSkip = 20; // maximum ticks to process in a single render step private readonly input: DirectionHandler; @@ -64,17 +61,24 @@ export class GameContext { return diff; } + private updateStep(): void { + vec2.scaleAndAdd(this.entityPos, this.entityPos, this.entityMovement, this.speed); + } + private update(time: number): void { - const diff = this.updateTime(time); + const diff = Math.min(this.maxSkip, this.updateTime(time)); - if (vec2.sqrLen(this.entityMovement) > 0) - vec2.scaleAndAdd(this.entityPos, this.entityPos, this.entityMovement, diff * this.speed); - else + if (vec2.sqrLen(this.entityMovement) === 0) { this.renderer.snapToGrid(this.entityPos, this.entityPos); + return; + } + + for (let i = 0; i < diff; i++) + this.updateStep(); } private render = (time: number) => { - this.update(time); + this.update(Math.round(time / this.tick)); this.renderer.setCenter(this.entityPos); this.renderer.clear(); |