function Play() {

}

Play.prototype = {
  preload: function() {

    this.game.load.tilemap('track', 'assets/tiles.json', null, Phaser.Tilemap.TILED_JSON);
    this.game.load.image('tiles', 'assets/tiles.png');
    this.game.load.image('car', 'assets/car.png');
    this.game.load.image('goodie', 'assets/goodie.png'); 
    this.game.load.image('particle', 'assets/particle.png');
    this.game.load.bitmapFont('font', 'assets/font.png', 'assets/font.xml');
   
  },
  create: function() {

    this.map = this.game.add.tilemap('track');
    this.map.addTilesetImage('tiles');
    this.map.setCollision(1);
    this.map.setTileIndexCallback(8, this.hitFinishingLine, this);
    this.map.setTileLocationCallback(6, 8, 1, 1, this.hitHalf, this);
    this.layer = this.map.createLayer('tilelayer1');
    this.layer.resizeWorld();


    this.game.input.keyboard.addKeyCapture([Phaser.Keyboard.SPACEBAR]);
    this.game.input.keyboard.addKeyCapture([Phaser.Keyboard.UP]);
    this.game.input.keyboard.addKeyCapture([Phaser.Keyboard.RIGHT]);
    this.game.input.keyboard.addKeyCapture([Phaser.Keyboard.LEFT]);
    this.handbrakeKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    this.cursors = this.game.input.keyboard.createCursorKeys();
    
    this.game.physics.startSystem(Phaser.Physics.ARCADE);

    this.car =  new Car(this.game, 0, 0);
    this.game.add.existing(this.car);

    //this.game.physics.arcade.enable(this.layer);
    
    this.game.camera.follow(this.car);

    this.lapCount = 0;
    this.half = false;
    this.isStarted = false;

    this.emitter = this.game.add.emitter(game.world.centerX, 0, 400);
    this.emitter.makeParticles('particle');

    this.emitter.setRotation(0, 0);
    this.emitter.setScale(0.1, 1, 0.1, 1, 6000, Phaser.Easing.Quintic.Out);
    

    this.lapText = this.game.add.bitmapText(50, 50, 'font','', 64);
    this.lapText.fixedToCamera = true;
    this.lapText.setText('Lap: ' +  this.lapCount + "/5");

    this.lapTimeText = this.game.add.bitmapText(50, 120, 'font','', 32);
    this.lapTimeText.fixedToCamera = true;

    this.lapTimes = new Array();

    

  },
  update: function(){
    this.game.physics.arcade.overlap(this.car, this.layer,this.resetCar, null, this);
    this.car.drive();
    this.checkKeys();

    
  },

  resetCar: function(){
    this.car.resetStartPosition();
    this.half = false;
  },

  render:function(){
    //this.game.debug.bodyInfo(this.car, 32, 320);
    //this.game.debug.body(this.car);
    //this.game.debug.body(this.layer);
  },

  
  checkKeys: function() {

    if(this.cursors.up.isDown){
      this.car.accel();
    }

    if(this.cursors.down.isDown){
      this.car.brake();
    }

    if(this.cursors.left.isDown){
      this.car.steerLeft();
    }

    if(this.cursors.right.isDown){
      this.car.steerRight();
    }

    if(this.handbrakeKey.isDown){
      this.car.isHandbrake=true;
      if(!this.emitter.on)
        this.emitter.start(false, 800, 10);
        this.emitter.emitX = this.car.body.x;
        this.emitter.emitY = this.car.body.y;
    }else{
      this.car.isHandbrake=false;
      this.emitter.on = false;
    }

  },

  hitFinishingLine: function(sprite, tile){

    if(!this.isStarted){
      this.isStarted = true;
      this.startTimer();
    }

    if (this.lapCount<5 && this.car.body.velocity.x > 0 && this.half){
      this.lapCount ++;
      this.half = false;
      this.lapText.setText('Lap: ' + this.lapCount +"/5");
      this.lapDate = new Date();
      this.lapTime = this.lapDate.getTime();
      this.lapTimes.push(this.lapTime);

      var currentLapTime = this.lapTimes[this.lapTimes.length-1]-this.lapTimes[this.lapTimes.length-2]+" ms\n";

      this.lapTimeText.setText(this.lapTimeText.text + currentLapTime);
    }
      
    return false;
  },

  hitHalf: function(sprite, tile){
    if(this.car.body.velocity.x < 0 && !this.half){
        this.half = true;
    }
  },

  startTimer: function(){
    this.startDate = new Date();
    this.startTime = this.startDate.getTime();
    this.lapTimes.push(this.startTime);
  }



};