summaryrefslogtreecommitdiff
path: root/game/states/play.js
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2014-04-21 17:49:29 +0200
committerJustin Worthe <justin.worthe@gmail.com>2014-04-21 17:49:29 +0200
commita5ab29fd2d015f2db65d8062d2bb23222859b6d1 (patch)
tree4c53e3fcb2eb77126625a6c516bf77ebce3dcf6d /game/states/play.js
parenta9ba8a68d61110cca347e2a4823b25880bc94923 (diff)
Added tracking of where walls are, to prevent moving through
closes #3
Diffstat (limited to 'game/states/play.js')
-rw-r--r--game/states/play.js127
1 files changed, 88 insertions, 39 deletions
diff --git a/game/states/play.js b/game/states/play.js
index 6d542ca..c52b498 100644
--- a/game/states/play.js
+++ b/game/states/play.js
@@ -6,18 +6,36 @@ var Player = require('../prefabs/player');
function Play() {}
Play.prototype = {
+ addToMap: function(x, y) {
+ if (!this.map) {
+ this.map = [];
+ }
+ if (!this.map[x]) {
+ this.map[x] = [];
+ }
+
+ this.map[x][y] = true;
+ },
+ removeFromMap: function(x,y) {
+ if (!this.map || !this.map[x]) {
+ return;
+ }
+ this.map[x][y] = false;
+ },
+ checkMap: function(x,y) {
+ if (!this.map || !this.map[x]) {
+ return false;
+ }
+ return this.map[x][y];
+ },
preload: function() {
},
create: function() {
this.createWalls();
-// this.world.scale = {x:0.5, y:0.5};
- this.playerA = new Player(this.game, 100, 200, 'player-a', 0, {
- up: Phaser.Keyboard.UP,
- down: Phaser.Keyboard.DOWN,
- left: Phaser.Keyboard.LEFT,
- right: Phaser.Keyboard.RIGHT
- });
+ this.world.scale = {x:100, y:100};
+ this.playerA = new Player(this.game, 1, 2, 'player-a', 0);
this.game.add.existing(this.playerA);
+ this.addPlayerControls();
},
update: function() {
},
@@ -25,38 +43,69 @@ Play.prototype = {
this.walls = this.game.add.group();
this.walls.add(new Wall(this.game, 0,0));
- this.walls.add(new Wall(this.game, 100,0));
- this.walls.add(new Wall(this.game, 200,0));
- this.walls.add(new Wall(this.game, 300,0));
- this.walls.add(new Wall(this.game, 400,0));
- this.walls.add(new Wall(this.game, 500,0));
- this.walls.add(new Wall(this.game, 600,0));
- this.walls.add(new Wall(this.game, 700,0));
-
- this.walls.add(new Wall(this.game, 0,100));
- this.walls.add(new Wall(this.game, 700,100));
-
- this.walls.add(new Wall(this.game, 0,200));
- this.walls.add(new Wall(this.game, 200,200));
- this.walls.add(new Wall(this.game, 500,200));
- this.walls.add(new Wall(this.game, 700,200));
-
- this.walls.add(new Wall(this.game, 0,300));
- this.walls.add(new Wall(this.game, 200,300));
- this.walls.add(new Wall(this.game, 500,300));
- this.walls.add(new Wall(this.game, 700,300));
-
- this.walls.add(new Wall(this.game, 0,400));
- this.walls.add(new Wall(this.game, 700,400));
-
- this.walls.add(new Wall(this.game, 0,500));
- this.walls.add(new Wall(this.game, 100,500));
- this.walls.add(new Wall(this.game, 200,500));
- this.walls.add(new Wall(this.game, 300,500));
- this.walls.add(new Wall(this.game, 400,500));
- this.walls.add(new Wall(this.game, 500,500));
- this.walls.add(new Wall(this.game, 600,500));
- this.walls.add(new Wall(this.game, 700,500));
+ this.walls.add(new Wall(this.game, 1,0));
+ this.walls.add(new Wall(this.game, 2,0));
+ this.walls.add(new Wall(this.game, 3,0));
+ this.walls.add(new Wall(this.game, 4,0));
+ this.walls.add(new Wall(this.game, 5,0));
+ this.walls.add(new Wall(this.game, 6,0));
+ this.walls.add(new Wall(this.game, 7,0));
+
+ this.walls.add(new Wall(this.game, 0,1));
+ this.walls.add(new Wall(this.game, 7,1));
+
+ this.walls.add(new Wall(this.game, 0,2));
+ this.walls.add(new Wall(this.game, 2,2));
+ this.walls.add(new Wall(this.game, 5,2));
+ this.walls.add(new Wall(this.game, 7,2));
+
+ this.walls.add(new Wall(this.game, 0,3));
+ this.walls.add(new Wall(this.game, 2,3));
+ this.walls.add(new Wall(this.game, 5,3));
+ this.walls.add(new Wall(this.game, 7,3));
+
+ this.walls.add(new Wall(this.game, 0,4));
+ this.walls.add(new Wall(this.game, 7,4));
+
+ this.walls.add(new Wall(this.game, 0,5));
+ this.walls.add(new Wall(this.game, 1,5));
+ this.walls.add(new Wall(this.game, 2,5));
+ this.walls.add(new Wall(this.game, 3,5));
+ this.walls.add(new Wall(this.game, 4,5));
+ this.walls.add(new Wall(this.game, 5,5));
+ this.walls.add(new Wall(this.game, 6,5));
+ this.walls.add(new Wall(this.game, 7,5));
+
+ this.walls.forEach(function(wall) {
+ this.addToMap(wall.x, wall.y);
+ }, this);
+ },
+ addPlayerControls: function() {
+ var controls = {
+ up: Phaser.Keyboard.UP,
+ down: Phaser.Keyboard.DOWN,
+ left: Phaser.Keyboard.LEFT,
+ right: Phaser.Keyboard.RIGHT
+ };
+ this.game.input.keyboard.addKeyCapture([
+ controls.up,
+ controls.down,
+ controls.left,
+ controls.right
+ ]);
+
+ this.game.input.keyboard.addKey(controls.up).onDown.add(this.movePlayer.bind(this, this.playerA, 0, -1), this);
+ this.game.input.keyboard.addKey(controls.down).onDown.add(this.movePlayer.bind(this, this.playerA, 0, 1), this);
+ this.game.input.keyboard.addKey(controls.left).onDown.add(this.movePlayer.bind(this, this.playerA, -1, 0), this);
+ this.game.input.keyboard.addKey(controls.right).onDown.add(this.movePlayer.bind(this, this.playerA, 1, 0), this);
+ },
+ movePlayer: function(player, deltaX, deltaY) {
+ var newX = player.x + deltaX;
+ var newY = player.y + deltaY;
+
+ if (!this.checkMap(newX, newY)) {
+ player.move(newX, newY);
+ }
}
};