'use strict'; var Player = require('../prefabs/player'); var Pill = require('../prefabs/pill'); var BonusPill = require('../prefabs/bonusPill'); var Wall = require('../prefabs/wall'); var PoisonPill = require('../prefabs/poisonPill'); var Hud = require('../prefabs/hud'); function Play() {} Play.prototype = { preload: function() { }, create: function() { this.readLevelFile(); this.world.scale = {x:50, y:50}; this.world.bounds = {x: -425, y:-25, width: this.game.width, height: this.game.height}; this.world.camera.setBoundsToWorld(); this.setupPlayerControls(); this.gameWon = false; this.hudA = new Hud(this.game, this.playerA, this.gameWidth-0.5, -0.5, 'spaced-scorefont-a', 'keys-a'); this.hudB = new Hud(this.game, this.playerB, -8.5, -0.5, 'spaced-scorefont-b', 'keys-b'); }, update: function() { this.checkForPlayerPillCollisions(); this.checkForPlayerPoisonPillCollisions(); if (this.playerA.canBeEaten && this.playerB.canBeEaten && 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; if (this.playerA.score > this.playerB.score) { this.setVictoryText('RED WINS', 'a'); } else if (this.playerA.score < this.playerB.score) { this.setVictoryText('YELLOW WINS', 'b'); } else { var victoryColor = this.playerA.isMyTurn ? 'b' : 'a'; this.setVictoryText('DRAW', victoryColor); } var self = this; setTimeout(function() { self.game.state.start('play'); }, 5000); } this.pollPlayerInput(); }, addToMap: function(x, y) { if (!this.map) { this.map = []; } if (!this.map[x]) { this.map[x] = []; } this.map[x][y] = true; }, removeFromMap: function(x,y) { if (!this.map || !this.map[x]) { return; } this.map[x][y] = false; }, checkMap: function(x,y) { if (!this.map || !this.map[x]) { return false; } return this.map[x][y]; }, 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 0.2) { this.movePlayer(player, 1, 0); } if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.2) { this.movePlayer(player, 0, -1); } if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.2) { this.movePlayer(player, 0, 1); } var poisonButton = pad.getButton(Phaser.Gamepad.XBOX360_A); if (poisonButton.isDown && player.gamepadPoisonLastPressed < poisonButton.timeDown) { player.gamepadPoisonLastPressed = poisonButton.timeDown; this.togglePoisonPill(player); } }, readLevelFile: function() { this.pills = this.game.add.group(); this.players = this.game.add.group(); this.walls = this.game.add.group(); this.poisonPills = this.game.add.group(); var levelText = this.game.cache.getText('level'); var splitRows = levelText.split('\n'); for (var y=0; y= this.gameWidth) { this.gameWidth = wall.x+1; } if (wall.y >= this.gameHeight) { this.gameHeight = wall.y+1; } }, this); this.respawnX = Math.ceil(this.gameWidth/2)-1; this.respawnY = Math.ceil(this.gameHeight/2)-1; this.playerB.isMyTurn = true; }, setupPlayerControls: function() { this.playerBControls = { up: Phaser.Keyboard.W, left: Phaser.Keyboard.A, down: Phaser.Keyboard.S, right: Phaser.Keyboard.D, poison: Phaser.Keyboard.Q }; this.playerAControls = { up: Phaser.Keyboard.UP, left: Phaser.Keyboard.LEFT, down: Phaser.Keyboard.DOWN, right: Phaser.Keyboard.RIGHT, poison: Phaser.Keyboard.ENTER }; this.controls = { reset: Phaser.Keyboard.ESC }; function addKeyCaptures(controls, keyboard) { for (var index in controls) { if (controls.hasOwnProperty(index)) { keyboard.addKeyCapture(controls[index]); } } } addKeyCaptures(this.playerAControls, this.game.input.keyboard); addKeyCaptures(this.playerBControls, this.game.input.keyboard); addKeyCaptures(this.controls, this.game.input.keyboard); this.game.input.gamepad.start(); this.game.input.keyboard.addKey(this.playerBControls.poison).onDown.add(this.togglePoisonPill.bind(this, this.playerB), this); this.game.input.keyboard.addKey(this.playerAControls.poison).onDown.add(this.togglePoisonPill.bind(this, this.playerA), this); this.game.input.keyboard.addKey(this.controls.reset).onDown.add(function() { this.game.state.start('play'); },this); }, togglePoisonPill: function(player) { if (player.hasPoisonPill) { player.poisonPillActive = !player.poisonPillActive; } }, moveActivePlayer: function(deltaX, deltaY) { var activePlayer = null; for (var i=0; i1.1 || Math.abs(this.respawnY-player.y)>1.1); //cannot move into walls, when it isn't your turn, or when you're already moving if (this.checkMap(newX, newY) || !player.isMyTurn || player.moving) { return; } if (Math.abs(newX-this.respawnX)<=1 && Math.abs(newY-this.respawnY)<=1) { //cannot move into respawn area if (Math.abs(newX-this.respawnX)= this.gameWidth) { newX = 0; teleportX = -1; } if (newX < 0) { newX = this.gameWidth-1; teleportX = this.gameWidth; } if (newY >= this.gameHeight) { newY = 0; teleportY = -1; } if (newY < 0) { newY = this.gameHeight-1; teleportY = this.gameHeight; } if (newX === intermediateX && newY === intermediateY) { 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, function() { if (placePoisonPill) { this.poisonPills.add(new PoisonPill(this.game, posionPillX, posionPillY)); player.poisonPillActive = false; } this.togglePlayerTurn(); }, this); } }, playerPillCollision: function(player, pill) { player.score += pill.score; pill.destroy(); player.scoreSound.play(); }, playerPoisonPillCollision: function(player, poisonPill) { if (player.lastTween) { player.lastTween.onComplete.add(player.teleport.bind(player, this.respawnX, this.respawnY), player); } else { player.teleport(this.respawnX, this.respawnY); } poisonPill.destroy(); player.respawnSound.play(); }, playerPlayerCollision: function(playerA, playerB) { var eatenPlayer = playerA.isMyTurn ? playerB : playerA; if (eatenPlayer.lastTween) { eatenPlayer.lastTween.onComplete.add(eatenPlayer.teleport.bind(eatenPlayer, this.respawnX, this.respawnY), eatenPlayer); } else { eatenPlayer.teleport(this.respawnX, this.respawnY); } eatenPlayer.canBeEaten = false; eatenPlayer.respawnSound.play(); }, togglePlayerTurn: function() { for (var i=0; i