summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@entelect.co.za>2014-05-28 15:20:25 +0200
committerJustin Worthe <justin.worthe@entelect.co.za>2014-05-28 15:20:25 +0200
commit7dfbd52a082b7a33c5f81b26da859830675223ba (patch)
treeb25ed92dbe6ce33e47e219d51e5d3485680c1878 /game
parentf58ea140ae6d6f0038e429ee47319ad3c9a74de1 (diff)
Added cleanup and drawing of game result
Diffstat (limited to 'game')
-rw-r--r--game/states/play.js69
-rw-r--r--game/states/preload.js3
2 files changed, 48 insertions, 24 deletions
diff --git a/game/states/play.js b/game/states/play.js
index 96188e5..a68b068 100644
--- a/game/states/play.js
+++ b/game/states/play.js
@@ -44,24 +44,32 @@ Play.prototype = {
this.game.physics.startSystem(Phaser.Physics.ARCADE);
- this.playerAScoreText = this.game.add.bitmapText(-0.1, -0.4, 'scorefont','0',1);
- this.playerBScoreText = this.game.add.bitmapText(this.world.width/100 - 2.1, -0.4, 'scorefont','0',1);
+ this.playerAScoreText = this.game.add.bitmapText(-0.1, -0.4, 'spaced-scorefont','0',1);
+ this.playerBScoreText = this.game.add.bitmapText(this.world.width/this.world.scale.x - 2.1, -0.4, 'spaced-scorefont','0',1);
+
+ this.victoryText = this.game.add.bitmapText(this.world.width/2/this.world.scale.x, 2, 'scorefont', '', 1);
+
+ this.gameWon = false;
},
update: function() {
this.game.physics.arcade.overlap(this.players, this.pills, this.playerPillCollision, null, this);
- if (this.pills.total === 0) {
-
+ if (!this.gameWon && this.pills.total === 0) {
+ this.gameWon = true;
if (this.playerA.score > this.playerB.score) {
- console.log("PLAYER A WINS!");
+ this.setVictoryText("PLAYER A WINS");
}
else if (this.playerA.score < this.playerB.score) {
- console.log("PLAYER B WINS!");
+ this.setVictoryText("PLAYER B WINS");
}
else {
- console.log("THIS GAME WAS A DRAW!")
+ this.setVictoryText("DRAW");
}
- this.game.state.start('play');
+
+ var self = this;
+ setTimeout(function() {
+ self.game.state.start('play');
+ }, 5000);
}
},
createWalls: function() {
@@ -139,13 +147,13 @@ Play.prototype = {
this.updatePlayerTurn(0);
},
addPlayerControls: function() {
- var playerAControls = {
+ this.playerAControls = {
up: Phaser.Keyboard.W,
left: Phaser.Keyboard.A,
down: Phaser.Keyboard.S,
right: Phaser.Keyboard.D
}
- var playerBControls = {
+ this.playerBControls = {
up: Phaser.Keyboard.UP,
left: Phaser.Keyboard.LEFT,
down: Phaser.Keyboard.DOWN,
@@ -155,22 +163,22 @@ Play.prototype = {
function addKeyCaptures(controls, keyboard) {
for (var index in controls) {
if (controls.hasOwnProperty(index)) {
- keyboard.addKeyCapture(playerAControls[index]);
+ keyboard.addKeyCapture(controls[index]);
}
}
}
- addKeyCaptures(playerAControls, this.game.input.keyboard);
- addKeyCaptures(playerBControls, this.game.input.keyboard);
-
- this.game.input.keyboard.addKey(playerAControls.up).onDown.add(this.movePlayer.bind(this, this.playerA, 0, -1), this);
- this.game.input.keyboard.addKey(playerAControls.down).onDown.add(this.movePlayer.bind(this, this.playerA, 0, 1), this);
- this.game.input.keyboard.addKey(playerAControls.left).onDown.add(this.movePlayer.bind(this, this.playerA, -1, 0), this);
- this.game.input.keyboard.addKey(playerAControls.right).onDown.add(this.movePlayer.bind(this, this.playerA, 1, 0), this);
-
- this.game.input.keyboard.addKey(playerBControls.up).onDown.add(this.movePlayer.bind(this, this.playerB, 0, -1), this);
- this.game.input.keyboard.addKey(playerBControls.down).onDown.add(this.movePlayer.bind(this, this.playerB, 0, 1), this);
- this.game.input.keyboard.addKey(playerBControls.left).onDown.add(this.movePlayer.bind(this, this.playerB, -1, 0), this);
- this.game.input.keyboard.addKey(playerBControls.right).onDown.add(this.movePlayer.bind(this, this.playerB, 1, 0), this);
+ addKeyCaptures(this.playerAControls, this.game.input.keyboard);
+ addKeyCaptures(this.playerBControls, this.game.input.keyboard);
+
+ this.game.input.keyboard.addKey(this.playerAControls.up).onDown.add(this.movePlayer.bind(this, this.playerA, 0, -1), this);
+ this.game.input.keyboard.addKey(this.playerAControls.down).onDown.add(this.movePlayer.bind(this, this.playerA, 0, 1), this);
+ this.game.input.keyboard.addKey(this.playerAControls.left).onDown.add(this.movePlayer.bind(this, this.playerA, -1, 0), this);
+ this.game.input.keyboard.addKey(this.playerAControls.right).onDown.add(this.movePlayer.bind(this, this.playerA, 1, 0), this);
+
+ this.game.input.keyboard.addKey(this.playerBControls.up).onDown.add(this.movePlayer.bind(this, this.playerB, 0, -1), this);
+ this.game.input.keyboard.addKey(this.playerBControls.down).onDown.add(this.movePlayer.bind(this, this.playerB, 0, 1), this);
+ this.game.input.keyboard.addKey(this.playerBControls.left).onDown.add(this.movePlayer.bind(this, this.playerB, -1, 0), this);
+ this.game.input.keyboard.addKey(this.playerBControls.right).onDown.add(this.movePlayer.bind(this, this.playerB, 1, 0), this);
},
movePlayer: function(player, deltaX, deltaY) {
var newX = player.x + deltaX;
@@ -197,6 +205,21 @@ Play.prototype = {
this.players.children[i].isMyTurn = (i === this.playerTurn);
}
console.log("Player " + this.playerTurn + "'s turn");
+ },
+ setVictoryText: function(newText) {
+ this.victoryText.setText(newText);
+ this.victoryText.position.x = this.world.width/2/this.world.scale.x - this.victoryText.textWidth/2 - 0.5;
+ },
+ shutdown: function() {
+ this.game.input.keyboard.removeKey(this.playerAControls.up);
+ this.game.input.keyboard.removeKey(this.playerAControls.down);
+ this.game.input.keyboard.removeKey(this.playerAControls.left);
+ this.game.input.keyboard.removeKey(this.playerAControls.right);
+
+ this.game.input.keyboard.removeKey(this.playerBControls.up);
+ this.game.input.keyboard.removeKey(this.playerBControls.down);
+ this.game.input.keyboard.removeKey(this.playerBControls.left);
+ this.game.input.keyboard.removeKey(this.playerBControls.right);
}
};
diff --git a/game/states/preload.js b/game/states/preload.js
index 28420f7..c24174a 100644
--- a/game/states/preload.js
+++ b/game/states/preload.js
@@ -17,7 +17,8 @@ Preload.prototype = {
this.load.image('player-b', 'assets/images/player-b.svg');
this.load.image('pill', 'assets/images/pill.svg');
- this.load.bitmapFont('scorefont', 'assets/fonts/scorefont.png', 'assets/fonts/scorefont.fnt', undefined, 10);
+ this.load.bitmapFont('spaced-scorefont', 'assets/fonts/scorefont.png', 'assets/fonts/scorefont.fnt', undefined, 10);
+ this.load.bitmapFont('scorefont', 'assets/fonts/scorefont.png', 'assets/fonts/scorefont.fnt');
},
create: function() {
this.asset.cropEnabled = false;