'use strict'; var Player = require('../entities/player'); var Pill = require('../entities/pill'); var BonusPill = require('../entities/bonusPill'); var Wall = require('../entities/wall'); var PoisonPill = require('../entities/poisonPill'); var Hud = require('../entities/hud'); var CollisionMap = require('../entities/collisionMap') function Play() {} Play.prototype = { create: function() { this.readLevelFile(); this.addGameHud(); this.world.scale = {x:50, y:50}; //manually tweaked based on size of level file this.world.bounds = {x: -425, y:-25, width: this.game.width, height: this.game.height}; this.world.camera.setBoundsToWorld(); this.setupPlayerControls(); this.gameWon = false; }, 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; this.displayVictoryText(); this.game.time.events.add(5000, function() { this.game.state.start('play'); }, this); } this.pollPlayerInput(); }, 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(); this.map = new CollisionMap(); var levelText = this.game.cache.getText('level'); var splitRows = levelText.split('\n'); this.gameHeight = splitRows.length; this.gameWidth = splitRows.reduce(function(currentMax, nextRow) { return Math.max(currentMax, nextRow.trim().length); }, 0); for (var y=0; y 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); } }, pollKeyboardForPlayer: function(controls, player) { if (this.game.input.keyboard.isDown(controls.up)) { this.movePlayer(player, 0, -1); } if (this.game.input.keyboard.isDown(controls.down)) { this.movePlayer(player, 0, 1); } if (this.game.input.keyboard.isDown(controls.left)) { this.movePlayer(player, -1, 0); } if (this.game.input.keyboard.isDown(controls.right)) { this.movePlayer(player, 1, 0); } var poisonKey = this.game.input.keyboard.addKey(controls.poison); if (poisonKey.isDown && player.keyboardPoisonLastPressed < poisonKey.timeDown) { player.keyboardPoisonLastPressed = poisonKey.timeDown; this.togglePoisonPill(player); } }, togglePoisonPill: function(player) { if (player.hasPoisonPill) { player.poisonPillActive = !player.poisonPillActive; } }, movePlayer: function(player, deltaX, deltaY) { var newX = player.x + deltaX; var newY = player.y + deltaY; var placePoisonPill = player.hasPoisonPill && player.poisonPillActive && (Math.abs(this.respawnX-player.x)>1.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.map.checkMap(newX, newY) || !player.isMyTurn || player.moving) { return; } //special rules about moving around respawn area if (Math.abs(newX-this.respawnX)<=1 && Math.abs(newY-this.respawnY)<=1) { //cannot move into respawn area (only outwards) 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); } }, togglePlayerTurn: function() { for (var i=0; i this.playerB.score) { this.createVictoryText('RED WINS', 'a'); } else if (this.playerA.score < this.playerB.score) { this.createVictoryText('YELLOW WINS', 'b'); } else { var victoryColor = this.playerA.isMyTurn ? 'b' : 'a'; this.createVictoryText('DRAW', victoryColor); } }, createVictoryText: function(newText, winnerLetter) { this.victoryText = this.game.add.bitmapText(this.world.width/2/this.world.scale.x, 2, 'scorefont-'+winnerLetter, newText, 2); this.victoryText.position.x = (this.world.width/this.world.scale.x)/2 - 8 - this.victoryText.textWidth/2 - 0.5; }, shutdown: function() { function removeKeyCaptures(controls, keyboard) { for (var index in controls) { if (controls.hasOwnProperty(index)) { keyboard.removeKey(controls[index]); } } } removeKeyCaptures(this.playerAControls, this.game.input.keyboard); removeKeyCaptures(this.playerBControls, this.game.input.keyboard); removeKeyCaptures(this.controls, this.game.input.keyboard); } }; module.exports = Play;