'use strict'; var Player = require('../prefabs/player'); var Pill = require('../prefabs/pill'); var BonusPill = require('../prefabs/bonusPill'); var Wall = require('../prefabs/wall'); function Play() {} Play.prototype = { 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]; }, preload: function() { }, create: function() { this.readLevelFile(); this.world.scale = {x:50, y:50}; this.world.bounds = {x: -25, y:-25, width: this.game.width, height: this.game.height}; this.world.camera.setBoundsToWorld(); this.setupPlayerControls(); this.playerAScoreText = this.game.add.bitmapText(-0.1, -0.4, 'spaced-scorefont-a','0',2); this.playerBScoreText = this.game.add.bitmapText(this.world.width/this.world.scale.x - 4.5, -0.4, 'spaced-scorefont-b','0',2); this.gameWon = false; }, update: function() { this.checkForPlayerPillCollisions(); if (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(); }, 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); } }, readLevelFile: function() { this.pills = this.game.add.group(); this.players = this.game.add.group(); this.walls = this.game.add.group(); var levelText = this.game.cache.getText('level'); var splitRows = levelText.split('\n'); for (var x=0; x