diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-04 20:22:47 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-04 20:22:47 +0100 |
commit | dbc3a6aa120135c19a4a69cead0f895f4d8a16b9 (patch) | |
tree | 9d71a3053f31f78a9293469b80532c2f1f7d93b1 /src | |
parent | 884a5b700c6c924a0d85e656e83994069ee3f716 (diff) | |
download | rpgedit-dbc3a6aa120135c19a4a69cead0f895f4d8a16b9.tar rpgedit-dbc3a6aa120135c19a4a69cead0f895f4d8a16b9.zip |
controller: gamecontext: implement simple collision handling
Diffstat (limited to 'src')
-rw-r--r-- | src/controller/gamecontext.ts | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index c4b56b0..923727a 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -6,6 +6,7 @@ import { loadMap } from '../view/map'; import { Renderer } from '../view/renderer/renderer'; import { SpriteView } from '../view/sprite'; +import { LineSegment } from '../math/line'; import { getJSON } from '../util'; import { vec2 } from 'gl-matrix'; @@ -35,9 +36,16 @@ export class GameContext { private readonly input: DirectionHandler; - private readonly entityPos: vec2 = vec2.create(); + private readonly entityPos: vec2 = vec2.clone([6, 6]); private readonly entityMovement: vec2 = vec2.create(); + private readonly walls: LineSegment[] = [ + new LineSegment(vec2.fromValues(1, 1), vec2.fromValues(1, 0), 10), + new LineSegment(vec2.fromValues(11, 1), vec2.fromValues(0, 1), 10), + new LineSegment(vec2.fromValues(11, 11), vec2.fromValues(-1, 0), 10), + new LineSegment(vec2.fromValues(1, 11), vec2.fromValues(0, -1), 10), + ]; + private constructor( private readonly renderer: Renderer, private readonly mapView: SpriteView, @@ -62,7 +70,34 @@ export class GameContext { } private updateStep(): void { - vec2.scaleAndAdd(this.entityPos, this.entityPos, this.entityMovement, this.speed); + let move = new LineSegment(this.entityPos, this.entityMovement, this.speed); + const p = vec2.create(); + + let rescan = true; + + while (rescan) { + rescan = false; + + for (const w of this.walls) { + if (!w.collidesMove(move)) + continue; + + move.getP2(p); + w.projectPoint(p, p); + + const move2 = LineSegment.fromPoints(this.entityPos, p); + if (move2.l === 0) + return; + + if (!move.equals(move2)) { + move = move2; + rescan = true; + break; + } + } + } + + move.getP2(this.entityPos); } private update(time: number): void { |