controller: gamecontext: implement simple collision handling
This commit is contained in:
parent
884a5b700c
commit
dbc3a6aa12
1 changed files with 37 additions and 2 deletions
|
@ -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 {
|
||||
|
|
Reference in a new issue