summaryrefslogtreecommitdiffstats
path: root/src/control
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2016-01-07 21:01:02 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2016-01-07 21:01:02 +0100
commit6c896c3f23895582967aadb8d379d617d757f2fd (patch)
treee27c3ed856dff953960d94c10632846d5a501853 /src/control
parenta09b58b54c19673d3b699b069d0c7683053e27d3 (diff)
downloadrpgedit-6c896c3f23895582967aadb8d379d617d757f2fd.tar
rpgedit-6c896c3f23895582967aadb8d379d617d757f2fd.zip
Implement collision handling
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);
}
}
}