From 7dfbd52a082b7a33c5f81b26da859830675223ba Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Wed, 28 May 2014 15:20:25 +0200 Subject: Added cleanup and drawing of game result --- game/states/play.js | 69 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 23 deletions(-) (limited to 'game/states/play.js') 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); } }; -- cgit v1.2.3