var Turret = function(game, sprite) {
    this.game = game;
    this.sprite = sprite;
    this.bulletTime = this.game.time.now;
    this.alignToCursor();
};

Turret.BULLET_SPEED = 400;
Turret.FIRE_RATE = 2;

Turret.prototype.alignToCursor = function() {
    var targetAngle = this.game.math.angleBetween(
        this.sprite.x, this.sprite.y,
        this.game.input.activePointer.x, this.game.input.activePointer.y
    );
    this.sprite.rotation = targetAngle - 90 * Math.PI / 180;
}

Turret.prototype.fireBullet = function(bullets) {

    //  To avoid them being allowed to fire too fast we set a time limit
    if (this.game.time.now > this.bulletTime)
    {
        //  Grab the first bullet we can from the pool
        var bullet = bullets.getFirstExists(false);

        if (bullet)
        {
            //  And fire it
            bullet.reset(player.x, player.y + 8);
            bullet.rotation = this.sprite.rotation;
            this.game.physics.arcade.velocityFromRotation(bullet.rotation + 90 / 180 * Math.PI, Turret.BULLET_SPEED, bullet.body.velocity);
            this.bulletTime = this.game.time.now +  1000/Turret.FIRE_RATE;
        }
        return bullet;
    }
    return null;
}