'use strict'; var Wall = require('../prefabs/wall'); var Player = require('../prefabs/player'); var Pill = require('../prefabs/pill'); 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.createWalls(); this.createPills(); this.createPlayers(); this.world.scale = {x:100, y:100}; this.world.bounds = {x: -50, y:-50, width: this.game.width, height: this.game.height}; this.world.camera.setBoundsToWorld(); this.addPlayerControls(); this.game.physics.startSystem(Phaser.Physics.ARCADE); }, update: function() { this.game.physics.arcade.overlap(this.players, this.pills, this.playerPillCollision, null, this); if (this.pills.total === 0) { if (this.playerA.score > this.playerB.score) { console.log("PLAYER A WINS!"); } else if (this.playerA.score < this.playerB.score) { console.log("PLAYER B WINS!"); } else { console.log("THIS GAME WAS A DRAW!") } this.game.state.start('play'); } }, createWalls: function() { this.walls = this.game.add.group(); this.walls.add(new Wall(this.game, 0,0)); this.walls.add(new Wall(this.game, 1,0)); this.walls.add(new Wall(this.game, 2,0)); this.walls.add(new Wall(this.game, 3,0)); this.walls.add(new Wall(this.game, 4,0)); this.walls.add(new Wall(this.game, 5,0)); this.walls.add(new Wall(this.game, 6,0)); this.walls.add(new Wall(this.game, 7,0)); this.walls.add(new Wall(this.game, 0,1)); this.walls.add(new Wall(this.game, 7,1)); this.walls.add(new Wall(this.game, 0,2)); this.walls.add(new Wall(this.game, 2,2)); this.walls.add(new Wall(this.game, 5,2)); this.walls.add(new Wall(this.game, 7,2)); this.walls.add(new Wall(this.game, 0,3)); this.walls.add(new Wall(this.game, 2,3)); this.walls.add(new Wall(this.game, 5,3)); this.walls.add(new Wall(this.game, 7,3)); this.walls.add(new Wall(this.game, 0,4)); this.walls.add(new Wall(this.game, 7,4)); this.walls.add(new Wall(this.game, 0,5)); this.walls.add(new Wall(this.game, 1,5)); this.walls.add(new Wall(this.game, 2,5)); this.walls.add(new Wall(this.game, 3,5)); this.walls.add(new Wall(this.game, 4,5)); this.walls.add(new Wall(this.game, 5,5)); this.walls.add(new Wall(this.game, 6,5)); this.walls.add(new Wall(this.game, 7,5)); this.walls.forEach(function(wall) { this.addToMap(wall.x, wall.y); }, this); }, createPills: function() { this.pills = this.game.add.group(); this.pills.add(new Pill(this.game, 1,1)); this.pills.add(new Pill(this.game, 2,1)); this.pills.add(new Pill(this.game, 3,1)); this.pills.add(new Pill(this.game, 4,1)); this.pills.add(new Pill(this.game, 5,1)); this.pills.add(new Pill(this.game, 6,1)); this.pills.add(new Pill(this.game, 3,2)); this.pills.add(new Pill(this.game, 4,2)); this.pills.add(new Pill(this.game, 3,3)); this.pills.add(new Pill(this.game, 4,3)); this.pills.add(new Pill(this.game, 1,4)); this.pills.add(new Pill(this.game, 2,4)); this.pills.add(new Pill(this.game, 3,4)); this.pills.add(new Pill(this.game, 4,4)); this.pills.add(new Pill(this.game, 5,4)); this.pills.add(new Pill(this.game, 6,4)); }, createPlayers: function() { this.players = this.game.add.group(); this.playerA = new Player(this.game, 1, 2, 'player-a', 0); this.playerB = new Player(this.game, 6, 2, 'player-b', 0); this.players.add(this.playerA); this.players.add(this.playerB); }, addPlayerControls: function() { var playerAControls = { up: Phaser.Keyboard.W, left: Phaser.Keyboard.A, down: Phaser.Keyboard.S, right: Phaser.Keyboard.D } var playerBControls = { up: Phaser.Keyboard.UP, left: Phaser.Keyboard.LEFT, down: Phaser.Keyboard.DOWN, right: Phaser.Keyboard.RIGHT }; function addKeyCaptures(controls, keyboard) { for (var index in controls) { if (controls.hasOwnProperty(index)) { keyboard.addKeyCapture(playerAControls[index]); } } } addKeyCaptures(playerAControls, this.game.input.keyboard); addKeyCaptures(playerBControls, this.game.input.keyboard); this.game.input.keyboard.addKey(playerAControls.up).onDown.add(this.movePlayer.bind(this, this.playerA, 0, -1), this); this.game.input.keyboard.addKey(playerAControls.down).onDown.add(this.movePlayer.bind(this, this.playerA, 0, 1), this); this.game.input.keyboard.addKey(playerAControls.left).onDown.add(this.movePlayer.bind(this, this.playerA, -1, 0), this); this.game.input.keyboard.addKey(playerAControls.right).onDown.add(this.movePlayer.bind(this, this.playerA, 1, 0), this); this.game.input.keyboard.addKey(playerBControls.up).onDown.add(this.movePlayer.bind(this, this.playerB, 0, -1), this); this.game.input.keyboard.addKey(playerBControls.down).onDown.add(this.movePlayer.bind(this, this.playerB, 0, 1), this); this.game.input.keyboard.addKey(playerBControls.left).onDown.add(this.movePlayer.bind(this, this.playerB, -1, 0), this); this.game.input.keyboard.addKey(playerBControls.right).onDown.add(this.movePlayer.bind(this, this.playerB, 1, 0), this); }, movePlayer: function(player, deltaX, deltaY) { var newX = player.x + deltaX; var newY = player.y + deltaY; if (!this.checkMap(newX, newY)) { player.move(newX, newY); } }, playerPillCollision: function(player, pill) { player.score++; pill.destroy(); console.log('A: ' + this.playerA.score + ' B: ' + this.playerB.score); } }; module.exports = Play;