summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2014-04-21 17:14:01 +0200
committerJustin Worthe <justin.worthe@gmail.com>2014-04-21 17:14:01 +0200
commita9ba8a68d61110cca347e2a4823b25880bc94923 (patch)
treecb5c6e2924157554e339134e06f58faf6b98ab2b
parent0347cdee4f8c0cdb90848f300ff831ab30fd3007 (diff)
Added player with movement
Unfortunately, can currently go through walls. re #3
-rw-r--r--game/prefabs/player.js66
-rw-r--r--game/prefabs/wall.js2
-rw-r--r--game/states/play.js9
-rw-r--r--game/states/preload.js1
4 files changed, 76 insertions, 2 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;
diff --git a/game/prefabs/wall.js b/game/prefabs/wall.js
index 56c7e90..4b4b42a 100644
--- a/game/prefabs/wall.js
+++ b/game/prefabs/wall.js
@@ -2,8 +2,6 @@
var Wall = function(game, x, y, frame) {
Phaser.Sprite.call(this, game, x, y, 'wall', frame);
-
- // initialize your prefab here
};
Wall.prototype = Object.create(Phaser.Sprite.prototype);
diff --git a/game/states/play.js b/game/states/play.js
index ef55d1f..6d542ca 100644
--- a/game/states/play.js
+++ b/game/states/play.js
@@ -1,6 +1,7 @@
'use strict';
var Wall = require('../prefabs/wall');
+var Player = require('../prefabs/player');
function Play() {}
@@ -10,11 +11,19 @@ Play.prototype = {
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.game.add.existing(this.playerA);
},
update: function() {
},
createWalls: function() {
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));
diff --git a/game/states/preload.js b/game/states/preload.js
index c8b22ad..7154c8f 100644
--- a/game/states/preload.js
+++ b/game/states/preload.js
@@ -13,6 +13,7 @@ Preload.prototype = {
this.load.onLoadComplete.addOnce(this.onLoadComplete, this);
this.load.setPreloadSprite(this.asset);
this.load.image('wall', 'assets/images/wall.svg');
+ this.load.image('player-a', 'assets/images/player-a.svg');
},
create: function() {
this.asset.cropEnabled = false;