summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2014-08-23 14:11:38 +0200
committerJustin Worthe <justin.worthe@gmail.com>2014-08-23 14:11:38 +0200
commite74950e005d89fe2fd8453a78d84f26e4e93dada (patch)
tree008c166bc66b45a1459c740e9f1da59b8e38c8f3
parent8c0a371916effa25ac77dc8a3420b95bd8b7a173 (diff)
Removed relying on arcade physics for overlap detection, added player respawn
-rw-r--r--game/prefabs/bonusPill.js7
-rw-r--r--game/prefabs/pill.js7
-rw-r--r--game/prefabs/player.js8
-rw-r--r--game/states/play.js31
4 files changed, 43 insertions, 10 deletions
diff --git a/game/prefabs/bonusPill.js b/game/prefabs/bonusPill.js
index 1b7ae56..dd8dfce 100644
--- a/game/prefabs/bonusPill.js
+++ b/game/prefabs/bonusPill.js
@@ -4,12 +4,15 @@ var BonusPill = function(game, x, y, frame) {
Phaser.Sprite.call(this, game, x, y, 'bonus-pill', frame);
this.scale = {x: 0.01, y: 0.01};
this.anchor = {x: 0.5, y: 0.5};
-
- this.game.physics.arcade.enableBody(this);
+
this.score = 10;
};
BonusPill.prototype = Object.create(Phaser.Sprite.prototype);
BonusPill.prototype.constructor = BonusPill;
+BonusPill.prototype.getBounds = function() {
+ return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2);
+};
+
module.exports = BonusPill;
diff --git a/game/prefabs/pill.js b/game/prefabs/pill.js
index 29bf7a2..13054b0 100644
--- a/game/prefabs/pill.js
+++ b/game/prefabs/pill.js
@@ -4,12 +4,15 @@ var Pill = function(game, x, y, frame) {
Phaser.Sprite.call(this, game, x, y, 'pill', frame);
this.scale = {x: 0.01, y: 0.01};
this.anchor = {x: 0.5, y: 0.5};
-
- this.game.physics.arcade.enableBody(this);
+
this.score = 1;
};
Pill.prototype = Object.create(Phaser.Sprite.prototype);
Pill.prototype.constructor = Pill;
+Pill.prototype.getBounds = function() {
+ return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2);
+};
+
module.exports = Pill;
diff --git a/game/prefabs/player.js b/game/prefabs/player.js
index 40fce38..e33b109 100644
--- a/game/prefabs/player.js
+++ b/game/prefabs/player.js
@@ -12,8 +12,6 @@ var Player = function(game, x, y, key, frame, soundKey) {
this.scale = {x: 0.01, y: 0.01};
this.anchor = {x: 0.5, y: 0.5};
- this.game.physics.arcade.enableBody(this);
-
this.score = 0;
this.maxScore = 1;
this.isMyTurn = false;
@@ -49,8 +47,8 @@ Player.prototype.update = function() {
Player.prototype.move = function(newX, newY, callback, callbackContext) {
this.moving = true;
var tween = this.game.add.tween(this).to({x: newX, y: newY}, 500);
- tween.onComplete.add(this.finishMovement, this);
tween.onComplete.add(callback, callbackContext);
+ tween.onComplete.add(this.finishMovement, this);
tween.start();
};
@@ -63,4 +61,8 @@ Player.prototype.finishMovement = function() {
this.moving = false;
};
+Player.prototype.getBounds = function() {
+ return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2);
+};
+
module.exports = Player;
diff --git a/game/states/play.js b/game/states/play.js
index 7ffcc3d..882882c 100644
--- a/game/states/play.js
+++ b/game/states/play.js
@@ -41,15 +41,17 @@ Play.prototype = {
this.setupPlayerControls();
- this.game.physics.startSystem(Phaser.Physics.ARCADE);
-
this.playerAScoreText = this.game.add.bitmapText(-0.1, -0.4, 'spaced-scorefont-a','0',2);
this.playerBScoreText = this.game.add.bitmapText(this.world.width/this.world.scale.x - 4.5, -0.4, 'spaced-scorefont-b','0',2);
this.gameWon = false;
},
update: function() {
- this.game.physics.arcade.overlap(this.players, this.pills, this.playerPillCollision, null, this);
+ this.checkForPlayerPillCollisions();
+
+ if (Phaser.Rectangle.intersects(this.playerA.getBounds(), this.playerB.getBounds())) {
+ this.playerPlayerCollision(this.playerA, this.playerB);
+ }
if (!this.gameWon && this.pills.total === 0) {
this.gameWon = true;
@@ -72,6 +74,21 @@ Play.prototype = {
this.pollPlayerInput();
},
+ checkForPlayerPillCollisions: function() {
+ var pillCollisions = [];
+ this.players.forEach(function(player) {
+ var playerBounds = player.getBounds();
+ this.pills.forEach(function(pill) {
+ var pillBounds = pill.getBounds();
+ if (Phaser.Rectangle.intersects(playerBounds, pillBounds)) {
+ pillCollisions.push({player:player, pill:pill});
+ }
+ }, this);
+ }, this);
+ for (var i=0; i<pillCollisions.length; i++) {
+ this.playerPillCollision(pillCollisions[i].player, pillCollisions[i].pill);
+ }
+ },
pollPlayerInput: function() {
if (this.game.input.gamepad.pad1.connected) {
this.pollAnalogStickForPlayer(this.game.input.gamepad.pad1, this.playerA);
@@ -221,6 +238,14 @@ Play.prototype = {
this.playerAScoreText.setText(this.playerA.score+'');
this.playerBScoreText.setText(this.playerB.score+'');
},
+ playerPlayerCollision: function(playerA, playerB) {
+ var eatenPlayer = playerA.isMyTurn ? playerB : playerA;
+
+ var respawnX = Math.ceil(this.map.length/2)-1;
+ var respawnY = Math.ceil(this.map[0].length/2)-1;
+
+ eatenPlayer.teleport(respawnX, respawnY);
+ },
togglePlayerTurn: function() {
this.updatePlayerTurn((this.playerTurn+1)%this.players.length);
},