Implement collision handling

This commit is contained in:
Matthias Schiffer 2016-01-07 21:01:02 +01:00
parent a09b58b54c
commit 6c896c3f23
3 changed files with 91 additions and 38 deletions

View file

@ -19,7 +19,18 @@ export default class MapContext {
playerEntity: EntityPosition;
collision: number[][];
constructor(private map: MapData, private inputHandler: InputHandler) {
this.collision = new Array(map.width);
for (let i = 0; i < map.width; i++) {
this.collision[i] = new Array(map.height);
for (let j = 0; j < map.height; j++)
this.collision[i][j] = map.collision[j][i] == '0' ? 0 : 1;
}
this.playerEntity = new EntityPosition(
new Entity('square'),
new Position(8, 8),
@ -28,6 +39,12 @@ export default class MapContext {
this.addEntity(this.playerEntity);
this.addEntity(new EntityPosition(
new Entity('square'),
new Position(10, 10),
Direction.East
));
this.view = new MapView(
map,
this.entities,
@ -41,24 +58,49 @@ export default class MapContext {
});
}
private incCollision(p: Position) {
if (this.collision[p.x] && !_.isUndefined(this.collision[p.x][p.y]))
this.collision[p.x][p.y]++;
}
private decCollision(p: Position) {
if (this.collision[p.x] && !_.isUndefined(this.collision[p.x][p.y]))
this.collision[p.x][p.y]--;
}
private collides(p: Position): boolean {
if (this.collision[p.x] && !_.isUndefined(this.collision[p.x][p.y]))
return (this.collision[p.x][p.y] > 0);
else
return true;
}
private addEntity(entity: EntityPosition) {
this.entities.push(entity);
this.incCollision(entity.position);
}
private addTransition(entity: EntityPosition, dest: Position, start: number, dur: number) {
entity.transition = new Transition(start, start+dur, entity.position, dest);
this.incCollision(dest);
}
private finishTransition(entity: EntityPosition) {
this.decCollision(entity.position);
entity.position = entity.transition.dest;
entity.transition = null;
}
private updateState(time: number): boolean {
var ret = false;
while (true) {
var origTime = time;
let origTime = time;
if (this.playerEntity.transition && this.playerEntity.transition.end <= time) {
origTime = this.playerEntity.transition.end;
@ -69,7 +111,7 @@ export default class MapContext {
if (this.playerEntity.transition)
return true;
var dir: Direction = null;
let dir: Direction = null;
if (this.inputHandler.keys[InputHandler.Up])
dir = Direction.North;
@ -84,8 +126,13 @@ export default class MapContext {
return ret;
this.playerEntity.direction = dir;
this.addTransition(this.playerEntity, this.playerEntity.position.translate(dir, 1),
origTime, 250);
ret = true;
let dest = this.playerEntity.position.translate(dir, 1);
if (this.collides(dest))
return true;
this.addTransition(this.playerEntity, dest, origTime, 250);
}
}
}

View file

@ -9,12 +9,18 @@ interface Input {
export default class MapData {
tiles: {[key: string]: {file: string}};
public collision: string[];
public layers: string[][][];
collision: string[];
layers: string[][][];
width: number;
height: number;
constructor(data: Input) {
this.tiles = data.tiles;
this.collision = data.collision;
this.layers = data.layers;
this.height = this.collision.length;
this.width = this.collision[0].length;
}
}

View file

@ -5,38 +5,38 @@
">": {"rotate": 0, "file": "road_right"}
},
"collision": [
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111",
"11111111111111111111111111111111"
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000110000000000000000000",
"00000000000000000000000000000000"
],
"layers": [
[