function Player( ID )
{
    this.ID = ID;
    this.gold = STARTING_GOLD;
    this.maxGold = 100000;
    this.coreX = 0;
    this.coreZ = 0;
    this.gate1X = 0;
    this.gate1Z = 0;
    this.gate2X = 0;
    this.gate2Z = 0;

    this.coreMaxHP = 500;
    this.coreHP = this.coreMaxHP;

    this.lasttime = Date.now();
}



Player.prototype.restart = function()
{
    this.gold = STARTING_GOLD;
    this.maxGold = 100000;

    this.coreMaxHP = 500;
    this.coreHP = this.coreMaxHP;

    this.lasttime = Date.now();

    // obstruct the core position
    if ( this.ID == 1 )
    {
        map.setObstructed( 0 , 4 , CORE );
        map.setObstructed( 0 , 5 , CORE );
        map.setObstructed( 1 , 4 , CORE );
        map.setObstructed( 1 , 5 , CORE );
    }
    else
    {
        map.setObstructed( 22 , 4 , CORE );
        map.setObstructed( 22 , 5 , CORE );
        map.setObstructed( 23 , 4 , CORE );
        map.setObstructed( 23 , 5 , CORE );
    }

    this.healthBar.refill();

    if ( this.ID == 1 )
    {
        document.getElementById("health1").style.width = ((this.coreHP/this.coreMaxHP)*200)+"px";
    }
    else
    {
        document.getElementById("health2").style.width = ((this.coreHP/this.coreMaxHP)*200)+"px";
    }
}



Player.prototype.update = function()
{
    // a small default income
    if ( Date.now() > this.lasttime+3000 )
    {
        this.giveGold( 1 );
        this.lasttime = Date.now();
    }
}



Player.prototype.spawnCore = function( scene , xPosition )
{
    var pthis = this;
    this.model = null;
    loadModel( "models/core.glb" , "core" , scene , function( model )
    {
        pthis.model = model;
        pthis.model.position.x = xPosition;
        pthis.healthBar = new HealthBar( scene , pthis.model , 1 , 0.1 , -1 );
    } );


    if ( xPosition == -5.5 )
    {
        map.setObstructed( 0 , 4 , CORE );
        map.setObstructed( 0 , 5 , CORE );
        map.setObstructed( 1 , 4 , CORE );
        map.setObstructed( 1 , 5 , CORE );
        this.coreX = -5.5;
        this.coreZ = 0;
        this.gate1X = -3;
        this.gate1Z = -1.25;
        this.gate2X = -3;
        this.gate2Z = 1.25;
    }
    else
    {
        map.setObstructed( 22 , 4 , CORE );
        map.setObstructed( 22 , 5 , CORE );
        map.setObstructed( 23 , 4 , CORE );
        map.setObstructed( 23 , 5 , CORE );
        this.coreX = 5.5;
        this.coreZ = 0;
        this.gate1X = 3;
        this.gate1Z = -1.25;
        this.gate2X = 3;
        this.gate2Z = 1.25;
    }
}



Player.prototype.giveGold = function( amount )
{
    if ( this.gold < this.maxGold )
    {
        this.gold += amount;
        if ( this.gold > this.maxGold ) this.gold = this.maxGold;
        if ( this == player )
        {
            // document.getElementById("playerGold").innerHTML = this.gold+"/"+this.maxGold;
            document.getElementById("playerGold").innerHTML = this.gold;
        }
    }
}



Player.prototype.takeGold = function( amount )
{
    if ( this.gold >= amount )
    {
        this.gold -= amount;
        if ( this == player )
        {
            if ( document.getElementById("playerGold") )
            {
                // document.getElementById("playerGold").innerHTML = this.gold+"/"+this.maxGold;
                document.getElementById("playerGold").innerHTML = this.gold;
            }
        }
    }
}



Player.prototype.damageCore = function( dmg )
{
    this.coreHP -= dmg;
    if ( this.coreHP <= 0 )
    {
        this.coreHP = 0;
        console.log( "PLAYER "+this.ID+" HAS BEEN DEFEATED" );
        showGameOver( this.ID );
        b_paused = true;
    }
    this.healthBar.update( this.coreHP , this.coreMaxHP );

    if ( this.ID == 1 )
    {
        document.getElementById("health1").style.width = ((this.coreHP/this.coreMaxHP)*200)+"px";
    }
    else
    {
        document.getElementById("health2").style.width = ((this.coreHP/this.coreMaxHP)*200)+"px";
    }
}