summaryrefslogtreecommitdiffstats
path: root/src/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/control')
-rw-r--r--src/control/MapContext.ts55
1 files changed, 51 insertions, 4 deletions
diff --git a/src/control/MapContext.ts b/src/control/MapContext.ts
index 48e8b9e..4e69408 100644
--- a/src/control/MapContext.ts
+++ b/src/control/MapContext.ts
@@ -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);
}
}
}