summaryrefslogtreecommitdiffstats
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
parenta09b58b54c19673d3b699b069d0c7683053e27d3 (diff)
downloadrpgedit-6c896c3f23895582967aadb8d379d617d757f2fd.tar
rpgedit-6c896c3f23895582967aadb8d379d617d757f2fd.zip
Implement collision handling
-rw-r--r--src/control/MapContext.ts55
-rw-r--r--src/model/MapData.ts10
-rw-r--r--static/resources/map/test.json64
3 files changed, 91 insertions, 38 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);
}
}
}
diff --git a/src/model/MapData.ts b/src/model/MapData.ts
index 54894c0..14f2586 100644
--- a/src/model/MapData.ts
+++ b/src/model/MapData.ts
@@ -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;
}
}
diff --git a/static/resources/map/test.json b/static/resources/map/test.json
index 12b1894..e76b195 100644
--- a/static/resources/map/test.json
+++ b/static/resources/map/test.json
@@ -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": [
[