function Turret( x , z , player , enemyPlayer )
{
    this.building = new Building( x , z , player , TURRET );
    this.building.setHP( 50 );
    this.enemyPlayer = enemyPlayer;
    this.lasttime = Date.now();

    this.range = 1.5;
    this.damage = 5;
}



Turret.prototype.update = function()
{
    this.building.update();
    if ( this.building.isDead() ) return;

    if ( Date.now() > this.lasttime+1500 )
    {
        // look for enemy entities in range
        for ( var i = 0 ; i < entities.length ; ++i )
        {
            if ( entities[i].player.ID == this.enemyPlayer.ID )
            {
                if ( entities[i].b_alive )
                {
                    var distance = BABYLON.Vector2.Distance( entities[i].position , this.building.worldPosition );
                    if ( distance <= this.range*this.range )
                    {
                        entities[i].takeDamage( this.damage );
                        // #TODO maybe change this to projectiles, atm it's not very clear the turret is attacking something
                        break;
                    }
                }
            }
        }
        this.lasttime = Date.now();
    }
}



Turret.prototype.build = function( scene )
{
    var pthis = this;
    this.model = null;
    loadModel( "models/turret.glb" , "turret" , scene , function( model )
    {
        pthis.model = model;
        pthis.model.position.x = pthis.building.worldPosition.x;
        pthis.model.position.z = pthis.building.worldPosition.y;
        pthis.building.healthBar = new HealthBar( scene , pthis.model , 0.4 , 0.03 , -1.1 );
        for ( var i = 0 ; i < pthis.model._children.length ; ++i )
        {
            pthis.model._children[i].name = pthis.building.ID;
        }
    } );
}



Turret.prototype.remove = function()
{
    this.building.b_modelRemoved = true;
    this.model.dispose();
}