From f58ea140ae6d6f0038e429ee47319ad3c9a74de1 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 22 May 2014 07:50:56 +0200 Subject: Added turn mechanic to game re #10 --- game/prefabs/player.js | 3 ++- game/states/boot.js | 2 +- game/states/play.js | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/game/prefabs/player.js b/game/prefabs/player.js index b520947..0f6cb18 100644 --- a/game/prefabs/player.js +++ b/game/prefabs/player.js @@ -10,6 +10,7 @@ var Player = function(game, x, y, key, frame) { this.game.physics.arcade.enableBody(this); this.score = 0; + this.isMyTurn = false; }; Player.prototype = Object.create(Phaser.Sprite.prototype); @@ -19,7 +20,7 @@ Player.prototype.update = function() { }; Player.prototype.move = function(newX, newY) { - if (this.moving) { + if (this.moving || !this.isMyTurn) { return; } diff --git a/game/states/boot.js b/game/states/boot.js index 91cbb25..80b7d4b 100644 --- a/game/states/boot.js +++ b/game/states/boot.js @@ -5,7 +5,7 @@ function Boot() { Boot.prototype = { preload: function() { - this.load.image('preloader', 'assets/preloader.gif'); + this.load.image('preloader', 'assets/images/preloader.gif'); }, create: function() { this.game.input.maxPointers = 1; diff --git a/game/states/play.js b/game/states/play.js index 84237a6..96188e5 100644 --- a/game/states/play.js +++ b/game/states/play.js @@ -135,6 +135,8 @@ Play.prototype = { this.playerB = new Player(this.game, 6, 2, 'player-b', 0); this.players.add(this.playerA); this.players.add(this.playerB); + + this.updatePlayerTurn(0); }, addPlayerControls: function() { var playerAControls = { @@ -174,8 +176,9 @@ Play.prototype = { var newX = player.x + deltaX; var newY = player.y + deltaY; - if (!this.checkMap(newX, newY)) { + if (!this.checkMap(newX, newY) && player.isMyTurn) { player.move(newX, newY); + this.togglePlayerTurn(); } }, playerPillCollision: function(player, pill) { @@ -184,6 +187,16 @@ Play.prototype = { this.playerAScoreText.setText(this.playerA.score+''); this.playerBScoreText.setText(this.playerB.score+''); + }, + togglePlayerTurn: function() { + this.updatePlayerTurn((this.playerTurn+1)%this.players.length); + }, + updatePlayerTurn: function(newPlayerTurn) { + this.playerTurn = newPlayerTurn; + for (var i=0; i