diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-05 19:59:39 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-05 19:59:39 +0100 |
commit | fd59aaa55c39c583af86f4dc059cbe9cedd085d2 (patch) | |
tree | 7e19b71ce1ecdf616f230ecd271400141b3e6503 /src/controller | |
parent | 9be9f8e73940f1a38410575557fcd57cc620a8f4 (diff) | |
download | rpgedit-fd59aaa55c39c583af86f4dc059cbe9cedd085d2.tar rpgedit-fd59aaa55c39c583af86f4dc059cbe9cedd085d2.zip |
More generic collision handling
Diffstat (limited to 'src/controller')
-rw-r--r-- | src/controller/gamecontext.ts | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index cfdcda5..4ae449a 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -6,7 +6,9 @@ import { loadMap } from '../view/map'; import { Renderer } from '../view/renderer/renderer'; import { SpriteView } from '../view/sprite'; +import { Collidable } from '../math/collision'; import { LineSegment, Movement } from '../math/line'; +import { Point } from '../math/point'; import { getJSON } from '../util'; import { vec2 } from 'gl-matrix'; @@ -24,8 +26,8 @@ export class GameContext { ); } - private static mkCollision(collision: Collision[]): LineSegment[] { - const ret: LineSegment[] = []; + private static mkCollision(collision: Collision[]): Collidable[] { + const ret: Collidable[] = []; for (const c of collision) { switch (c.type) { @@ -39,13 +41,18 @@ export class GameContext { ret.push(LineSegment.fromPoints(vec2.clone(prev), vec2.clone(v))); prev = v; } + + for (const v of c.vertices) { + ret.push(new Point(vec2.clone(v))); + prev = v; + } } } return ret; } - private static async loadMap(renderer: Renderer): Promise<[SpriteView, LineSegment[]]> { + private static async loadMap(renderer: Renderer): Promise<[SpriteView, Collidable[]]> { const map = new MapData(await getJSON('resources/map/test.json')); return [await loadMap(renderer, map), this.mkCollision(map.collision)]; } @@ -53,7 +60,7 @@ export class GameContext { private time: number|null = null; private readonly tick = 10; // ms per tick - private readonly speed = 0.05; // movement per tick + private readonly speed = 0.04; // movement per tick private readonly maxSkip = 20; // maximum ticks to process in a single render step private readonly input: DirectionHandler; @@ -67,7 +74,7 @@ export class GameContext { private readonly renderer: Renderer, private readonly mapView: SpriteView, private readonly entity: SpriteView, - private readonly collision: LineSegment[], + private readonly collision: Collidable[], ) { this.input = new DirectionHandler(); this.input.addListener((v) => { @@ -102,7 +109,7 @@ export class GameContext { const move = new Movement(this.entityPos, dest); for (const c of this.collision) { - if (!c.collidesMoveCircle(dest2, move, this.collisionRadius)) + if (!c.collide(dest2, move, this.collisionRadius)) continue; if (!vec2.exactEquals(dest, dest2)) { |