diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2014-04-21 17:14:01 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2014-04-21 17:14:01 +0200 |
commit | a9ba8a68d61110cca347e2a4823b25880bc94923 (patch) | |
tree | cb5c6e2924157554e339134e06f58faf6b98ab2b /game/prefabs/player.js | |
parent | 0347cdee4f8c0cdb90848f300ff831ab30fd3007 (diff) |
Added player with movement
Unfortunately, can currently go through walls.
re #3
Diffstat (limited to 'game/prefabs/player.js')
-rw-r--r-- | game/prefabs/player.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/game/prefabs/player.js b/game/prefabs/player.js new file mode 100644 index 0000000..8e3ac3d --- /dev/null +++ b/game/prefabs/player.js @@ -0,0 +1,66 @@ +'use strict'; + +var Player = function(game, x, y, key, frame, controls) { + 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); +}; + +Player.prototype = Object.create(Phaser.Sprite.prototype); +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) { + 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); + tween.start(); +}; + +Player.prototype.finishMovement = function() { + this.moving = false; +}; + +Player.prototype.canMoveToNewLocation = function(newX, newY) { + return true; +}; + +module.exports = Player; |