From a5ab29fd2d015f2db65d8062d2bb23222859b6d1 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 21 Apr 2014 17:49:29 +0200 Subject: Added tracking of where walls are, to prevent moving through closes #3 --- game/states/play.js | 127 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 88 insertions(+), 39 deletions(-) (limited to 'game/states/play.js') 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); + } } }; -- cgit v1.2.3