From 41c0981eedf6933daeada7ece55234c0da525996 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 24 Aug 2014 11:48:17 +0200 Subject: Added dropping and eating of poison pills --- game/prefabs/player.js | 4 ++++ game/states/play.js | 34 +++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/game/prefabs/player.js b/game/prefabs/player.js index fb63707..e1cdc60 100644 --- a/game/prefabs/player.js +++ b/game/prefabs/player.js @@ -19,6 +19,7 @@ var Player = function(game, x, y, key, frame, soundKey) { this.hasPoisonPill = true; this.poisonPillActive = false; + this.lastTween = null; this.scoreSound = game.sound.add(soundKey); @@ -54,6 +55,8 @@ Player.prototype.move = function(newX, newY, callback, callbackContext) { tween.onComplete.add(callback, callbackContext); tween.onComplete.add(this.finishMovement, this); + this.lastTween = tween; + tween.start(); }; @@ -77,6 +80,7 @@ Player.prototype.teleport = function(newX, newY) { Player.prototype.finishMovement = function() { this.moving = false; + this.lastTween = null; }; Player.prototype.getBounds = function() { diff --git a/game/states/play.js b/game/states/play.js index 5461c1b..ec0ddae 100644 --- a/game/states/play.js +++ b/game/states/play.js @@ -261,6 +261,13 @@ Play.prototype = { movePlayer: function(player, deltaX, deltaY) { var newX = player.x + deltaX; var newY = player.y + deltaY; + var placePoisonPill = player.hasPoisonPill && player.poisonPillActive; + + var posionPillX = player.x; + var posionPillY = player.y; + if (placePoisonPill) { + player.hasPoisonPill = false; + } if (this.checkMap(newX, newY) || !player.isMyTurn || player.moving) { return; @@ -289,10 +296,22 @@ Play.prototype = { } if (newX === intermediateX && newY === intermediateY) { - player.move(newX, newY, this.togglePlayerTurn, this); + player.move(newX, newY, function() { + if (placePoisonPill) { + this.poisonPills.add(new PoisonPill(this.game, posionPillX, posionPillY)); + player.poisonPillActive = false; + } + this.togglePlayerTurn(); + }, this); } else { - player.multistepMove(intermediateX, intermediateY, teleportX, teleportY, newX, newY, this.togglePlayerTurn, this); + player.multistepMove(intermediateX, intermediateY, teleportX, teleportY, newX, newY, function() { + if (placePoisonPill) { + this.poisonPills.add(new PoisonPill(this.game, posionPillX, posionPillY)); + player.poisonPillActive = false; + } + this.togglePlayerTurn(); + }, this); } }, playerPillCollision: function(player, pill) { @@ -304,12 +323,17 @@ Play.prototype = { this.playerBScoreText.setText(this.playerB.score+''); }, playerPoisonPillCollision: function(player, poisonPill) { - poisonPill.destroy(); - var respawnX = Math.ceil(this.gameWidth/2)-1; var respawnY = Math.ceil(this.gameHeight/2)-1; - player.teleport(respawnX, respawnY); + if (player.lastTween) { + player.lastTween.onComplete.add(player.teleport.bind(player, respawnX, respawnY), player); + } + else { + player.teleport(respawnX, respawnY); + } + + poisonPill.destroy(); }, playerPlayerCollision: function(playerA, playerB) { var eatenPlayer = playerA.isMyTurn ? playerB : playerA; -- cgit v1.2.3