More generic collision handling
This commit is contained in:
parent
9be9f8e739
commit
fd59aaa55c
4 changed files with 78 additions and 57 deletions
|
@ -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)) {
|
||||
|
|
Reference in a new issue