diff options
-rw-r--r-- | game/prefabs/player.js | 41 | ||||
-rw-r--r-- | game/prefabs/wall.js | 1 | ||||
-rw-r--r-- | game/states/play.js | 127 |
3 files changed, 92 insertions, 77 deletions
diff --git a/game/prefabs/player.js b/game/prefabs/player.js index 8e3ac3d..a195f94 100644 --- a/game/prefabs/player.js +++ b/game/prefabs/player.js @@ -1,21 +1,10 @@ 'use strict'; -var Player = function(game, x, y, key, frame, controls) { +var Player = function(game, x, y, key, frame) { Phaser.Sprite.call(this, game, x, y, key, frame); this.moving = false; - - this.game.input.keyboard.addKeyCapture([ - controls.up, - controls.down, - controls.left, - controls.right - ]); - - this.game.input.keyboard.addKey(controls.up).onDown.add(this.moveUp, this); - this.game.input.keyboard.addKey(controls.down).onDown.add(this.moveDown, this); - this.game.input.keyboard.addKey(controls.left).onDown.add(this.moveLeft, this); - this.game.input.keyboard.addKey(controls.right).onDown.add(this.moveRight, this); + this.scale = {x: 0.01, y: 0.01}; }; Player.prototype = Object.create(Phaser.Sprite.prototype); @@ -24,31 +13,11 @@ Player.prototype.constructor = Player; Player.prototype.update = function() { }; -Player.prototype.moveUp = function() { - this.move(0, -100); -}; -Player.prototype.moveDown = function() { - this.move(0, 100); -}; -Player.prototype.moveLeft = function() { - this.move(-100, 0); -}; -Player.prototype.moveRight = function() { - this.move(100, 0); -}; - -Player.prototype.move = function(deltaX, deltaY) { +Player.prototype.move = function(newX, newY) { if (this.moving) { return; } - var newX = this.x + deltaX; - var newY = this.y + deltaY; - - if (!this.canMoveToNewLocation(newX, newY)) { - return; - } - this.moving = true; var tween = this.game.add.tween(this).to({x: newX, y: newY}, 500); tween.onComplete.add(this.finishMovement, this); @@ -59,8 +28,4 @@ Player.prototype.finishMovement = function() { this.moving = false; }; -Player.prototype.canMoveToNewLocation = function(newX, newY) { - return true; -}; - module.exports = Player; diff --git a/game/prefabs/wall.js b/game/prefabs/wall.js index 4b4b42a..11ca511 100644 --- a/game/prefabs/wall.js +++ b/game/prefabs/wall.js @@ -2,6 +2,7 @@ var Wall = function(game, x, y, frame) { Phaser.Sprite.call(this, game, x, y, 'wall', frame); + this.scale = {x: 0.01, y: 0.01}; }; Wall.prototype = Object.create(Phaser.Sprite.prototype); 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); + } } }; |