//= require vendor/phaser.min.js
//= require firebase.js
//= require modals.js
//= require game/board.js
//= require game/characters.js
//= require game/images.js
//= require game/features.js

CANVAS_WIDTH = 833;
CANVAS_HEIGHT = 715;
CANVAS_OFFSET = 60;
MAX_SCORE = 10;
DEFAULT_LIVES = 3;
player1 = false;
player2 = false;
currentPlayer = false;
VELOCITY = 200;

var score = 0;
var lives = DEFAULT_LIVES;
var apples = [];
var cherry;
var speedUp;
var slowDown;
var person;
var ghost;
var scoreText, livesText;
var starOne, starTwo;
var map;
var layer;
var cursors;
var music;
var fb;
var instructions = 'Here are the rules of the game.<br /><br /><b>Player1:</b><br /><i>'
                 + 'Controls</i> - Move your character with the arrow keys.<br /><i>Objective'
                 + '</i> - Collect all apples and powerups, or eat all the ghosts. The choice'
                 + ' is yours.<br /><br /><b>Player2</b>:<br /><i>Controls</i> - Select which'
                 + ' ghost you want to control by pressing numbers 1 through 4. Then, move your'
                 + ' character with the arrow keys.<br /><i>Objective</i> - Eat the hero until'
                 + ' the hero has no lives left.<br /><br />Need a moment? Press <b>P</b> to'
                 + ' pause and <b>R</b> to resume.';

var aboutUs      = 'Hello! Welcome to <i>Haunted</i>! We are a team of 5 members that are'
                 + ' currently pursuing our passion for coding @ Dev Bootcamp: David Sin, Rootul'
                 + ' Patel, Sid Patel, Cassie Moy, and Julius Jung. We hope you enjoy playing'
                 + 'this game as much as we enjoyed creating it. Check out our <a id="aboutus" '
                 + 'href="https://github.com/red-spotted-newts-2014/haunted">GitHub Repo</a>!';

$(document).ready(function() {
  roomSession = $("#room-session").val();
  if (typeof roomSession !== "undefined") {
    fb = new firebase.firebaseSetup(roomSession);
  }

  $("#instructions-button").click(modals.instructions);

  $("#aboutus-button").click(modals.aboutUs);

  $("#chat-form").submit(firebase.preMessage);

  fb.chat.on("child_added", firebase.recieveMessage);

  document.onkeydown = function (e) {
    if(e.which == 80) {
      fb.pause.set(true);
    } else if (e.which == 82) {
      fb.pause.set(false);
    }
  }

  fb.pause.on("value", function(snapshot) {
    game.paused = snapshot.val();
  });

  fb.player2.on("value", function(snapshot) {
    if (snapshot.val()) {
      fb.pause.set(false);
    }
  });

  fb.player1.on("value", function(snapshot) {
    if (snapshot.val() != true) {
      modals.urlModal(roomSession);
    }
    else {
      modals.confirmPlayerModal();
    }
  })

});

var game = new Phaser.Game( CANVAS_WIDTH, CANVAS_HEIGHT, Phaser.AUTO, 'pac', { preload: preload, create: create, update: update } );

function preload() {
  loadImages();
  game.load.tilemap('map', '/tilemap.json', null, Phaser.Tilemap.TILED_JSON);
  //game.load.audio('music', '/music.mp3');

};

function create() {
  game.physics.startSystem(Phaser.Physics.ARCADE);

  board.createBoard();
  characters.createCharacters();

  var gamePhysicsArray = [person, ghost, apples, cherry, speedUp, slowDown, starOne, starTwo];

  for (var i = 0; i < gamePhysicsArray.length; i++) {
    game.physics.arcade.enable(gamePhysicsArray[i]);
  }

  [person, ghost].forEach(function(character){
    character.body.collideWorldBounds = true;
  })

  cursors = game.input.keyboard.createCursorKeys();

}

function update() {

  game.physics.arcade.collide(person,layer);
  game.physics.arcade.collide(ghost,layer);

  board.createOverlaps();

  livesText.text = 'lives: ' + lives;
  scoreText.text = 'score: ' + score;

  features.pacmanCoordinates();
  features.ghostCoordinates();
}

@@ -601,61 +601,17 @@
  = '
-Here are the rules of the game.%3Cbr /%3E%3Cbr /%3E%3Cb%3EPlayer1
+%3Cb%3EPacMan
 :%3C/b
@@ -621,33 +621,18 @@
 r /%3E
-%3Ci%3E
 '%0A  
-               
 + '
+%3Ci%3E
 Cont
@@ -680,32 +680,39 @@
 rrow keys.%3Cbr /%3E
+'%0A  + '
 %3Ci%3EObjective'%0A  
@@ -711,157 +711,72 @@
 tive
-'%0A                 + '%3C/i%3E - Collect all apples and powerups, or eat all the ghosts. The choice'%0A                 + ' is yours.%3Cbr /%3E%3Cbr /%3E%3Cb%3EPlayer2
+%3C/i%3E - Collect all the apples to win.%3Cbr /%3E%3Cbr /%3E'%0A  + '%3Cb%3EGhost
 %3C/b%3E
@@ -782,16 +782,23 @@
 %3E:%3Cbr /%3E
+'%0A  + '
 %3Ci%3EContr
@@ -811,139 +811,17 @@
 %3E - 
-Select which'%0A                 + ' ghost you want to control by pressing numbers 1 through 4. Then, move your'%0A                 + '
+Move your
  cha
@@ -853,16 +853,23 @@
 s.%3Cbr /%3E
+'%0A  + '
 %3Ci%3EObjec
@@ -887,78 +887,55 @@
 Eat 
-the hero until'%0A                 + ' the hero has no lives left.%3Cbr /%3E
+PacMan until he has no more lives.%3Cbr /%3E'%0A  + '
 %3Cbr 
@@ -968,38 +968,16 @@
 P%3C/b%3E to
-'%0A                 + '
  pause a
@@ -1014,21 +1014,16 @@
 aboutUs 
-     
 = 'Hello
@@ -1070,16 +1070,23 @@
 am of 5 
+'%0A  + '
 members 
@@ -1093,38 +1093,16 @@
 that are
-'%0A                 + '
  current
@@ -1152,16 +1152,23 @@
 otcamp: 
+'%0A  + '
 David Si
@@ -1176,38 +1176,16 @@
 , Rootul
-'%0A                 + '
  Patel, 
@@ -1246,37 +1246,22 @@
 joy 
+'%0A  + '
 playing
-'%0A                 + '
 this
@@ -1333,31 +1333,16 @@
 utus%22 '%0A
-               
   + 'hre

fix about us and instructions