class Raft 
{
    constructor()
    {
        this.x = -5.5+Math.random()*22;
        this.targetX = Math.random()*11;
        if ( Math.random() > 0.5 )
        {
            this.z = 20;
            this.targetZ = 3;
        }
        else
        {
            this.z = -17;
            this.targetZ = 0;
        }
        this.speed = 1.5;
        this.b_moving = true;
        // console.log( this.x+" , "+this.z+" -> "+this.targetX+" , "+this.targetZ );
        this.population = 5 + Math.floor( Math.random()*4 ) + game.raftCounter*2;

        // this.cube = new Sparrow.Cube( engine , -0.2 , -0.2 , -0.2 , 0.4 , 0.4 , 0.4 );
        // this.cube.setTranslation( this.x , 0 , this.z );

        this.model = Sparrow.ModelLoader.loadGLTF( engine , "models/raft.gltf" );
        this.model.setTranslation( this.x , 0 , this.z );
    }

    delete()
    {
        this.model.delete();
    }

    update( dt )
    {
        if ( this.b_moving )
        {
            var dx = this.targetX - this.x;
            var dz = this.targetZ - this.z;

            // var angle = Math.atan( dz / dx );
            var angle;
            if ( dx >= 0 )
            {
                angle = Math.atan( dz/dx ) + Math.PI/2;
            }
            else
            {
                angle = Math.atan( dz/dx ) + Math.PI + Math.PI/2;
            }

            this.x += Math.sin( angle )*this.speed*dt;
            this.z -= Math.cos( angle )*this.speed*dt;

            this.model.setTranslation( this.x , 0 , this.z );

            if ( Math.abs(dx) < 0.1 && Math.abs(dz) < 0.1 )
            {
                this.b_moving = false;
                game.removeRaft( this );
            }
        }
    }

    draw()
    {
        // this.cube.draw();
        this.model.draw();
    }
}