/**
 * Player.js
 *
 * Foofighter.js — Player entity
 *
 * This class represents the data model and display entity for a
 * Player's startship in the game environment.
 */

;(function( FooFighter ){

'use strict';

function Player ( gameState, group ) {
    this.gameState = gameState;
    this.config = {};
    this.sprite = {};
    this.movementVelocity = 250;
    this.displayStates = {
        neutral: 'player.png',
        left: 'playerLeft.png',
        right: 'playerRight.png'
    };
    this.fireTimer = 250;
    this.canFire = true;
    this.group = group;
    this.config.laser = {
        lifespan: 1500,
        velocity: -500,
        fireTimer: 250
    };
    this.bindEvents();
}

var proto = Player.prototype;


proto.bindEvents = function(){
    var vent = this.gameState.vent;
    vent.on('create', this.create.bind(this));
    vent.on('update', function(){
        this.movementHandler();
        this.weaponHandler();
    }.bind(this));
};


proto.create = function(){
    var game = this.gameState.game;
    this.sprite = game.add.sprite(
        game.world.centerX, game.world.centerY,
        'sprites', this.displayStates.neutral
    );
    this.sprite.anchor = {
        x: 0.5,
        y: 0.5
    };
    this.group.add(this.sprite);
};


proto.movementHandler = function(){
    var game = this.gameState.game,
        magnitude = this.movementVelocity,
        modifier = 1,
        axis,
        cursors = this.gameState.cursors;

    this.sprite.body.velocity.setTo(0, 0);

    if ( cursors.up.isDown ) {
        modifier = -1;
        axis = 'y';
    } else if ( cursors.down.isDown ) {
        modifier = 1;
        axis = 'y';
    } else if (cursors.left.isDown) {
        this.sprite.loadTexture('sprites', this.displayStates.left);
        modifier = -1;
        axis = 'x';
    } else if (cursors.right.isDown) {
        this.sprite.loadTexture('sprites', this.displayStates.right);
        modifier = 1;
        axis = 'x';
    } else {
        this.sprite.loadTexture('sprites', this.displayStates.neutral);
    }

    this.sprite.body.velocity[axis] = (modifier * magnitude);
};


proto.weaponHandler = function(){
    var game = this.gameState.game,
        cursors = this.gameState.cursors,
        keyboard = this.gameState.keyboard,
        laser;

    if ( !this.canFire ) {
        return false;
    }

    if ( keyboard.isDown(Phaser.Keyboard.SPACEBAR) ) {
        this.createLaser();
        this.gameState.score += 10;
        this.canFire = false;
        setTimeout(function(){
            this.canFire = true;
        }.bind(this), this.config.laser.fireTimer);
    }
};


proto.createLaser = function(){
    var game = this.gameState.game,
        velocity = this.config.laser.velocity,
        lifespan = this.config.laser.lifespan,
        laser;
    laser = game.add.sprite(
        this.sprite.body.x + this.sprite.body.width/2,
        this.sprite.body.y - 10,
        'sprites', 'laserGreen.png'
    );
    laser.anchor = {
        x: 0.5,
        y: 0.5
    };
    laser.body.velocity.y = velocity;
    laser.lifespan = lifespan;
};


FooFighter.Player = Player;


})(FooFighter);

@@ -591,32 +591,8 @@
 = %7B%0A
-        lifespan: 1500,%0A
     
@@ -2712,55 +2712,8 @@
 ty,%0A
-        lifespan = this.config.laser.lifespan,%0A
     
@@ -2994,28 +2994,119 @@
 ser.
-lifespan = lifespan;
+events.onOutOfBounds.add(this.resetLaser, laser);%0A%7D;%0A%0A%0Aproto.resetLaser = function ( laser ) %7B%0A    laser.kill()
 %0A%7D;%0A

use out of bounds event to remove laser