//
// mObject - moving object class
//

//
// mObject_addRelationship()
// mObject_applyFriction()
// mObject_bounce()
// mObject_calcNewPos()
// mObject_setPos(newX,newY)
// mObject_setXPos(newX)
// mObject_setYPos(newY)
// mObject_setSpeed(newX,newY)
// mObject_setXSpeed(newX)
// mObject_setYSpeed(newY)
// mObject_update()
//
// mObject(initId,initX,initY)
//

function mObject_addRelationship(newRelationship)
{
   this.relationships[this.relationships.length]=newRelationship;
}

function mObject_applyFriction()
{
}

function mObject_bounce()
{
}

function mObject_addInertia(addX,addY)
{
   this.accumulatedForceX+=addX;
   this.accumulatedForceY+=addY;
}

function mObject_calcForce()
{
   this.accumulatedForceX=0.0;
   this.accumulatedForceY=0.0;

   if(this.relationships.length > 0)
   {
      for(var i=0;i<this.relationships.length;i++)
      {
         this.relationships[i].addForce(this);
      }

      this.inertiaX+=this.accumulatedForceX;
      this.inertiaY+=this.accumulatedForceY;
   }

   this.inertiaX=this.inertiaX*this.friction;
   this.inertiaY=this.inertiaY*this.friction;
}

function mObject_calcNewPos()
{
   this.projectedX=this.x+(this.inertiaX/this.weight);
   this.projectedY=this.y+(this.inertiaY/this.weight);
}

function mObject_getX()
{
   return this.x;
}

function mObject_getY()
{
   return this.y;
}

function mObject_setPos(newX,newY)
{
   this.projectedX=newX;
   this.projectedY=newY;
   this.x=newX;
   this.y=newY;
}

function mObject_setXPos(newX)
{
   this.projectedX=newX;
   this.x=newX;
}

function mObject_setYPos(newY)
{
   this.projectedY=newY;
   this.y=newY;
}

function mObject_setSpeed(newX,newY)
{
   this.inertiaX=newX*this.weight;
   this.inertiaY=newY*this.weight;
}

function mObject_setXSpeed(newX)
{
   this.inertiaX=newX*this.weight;
}

function mObject_setYSpeed(newY)
{
   this.inertiaY=newY*this.weight;
}

function mObject_update()
{
   this.x=this.projectedX;
   this.y=this.projectedY;
}

function mObject(initId,initX,initY)
{
   //initialise
   this.id=initId;
   this.x=0.0;
   this.y=0.0;
   this.projectedX=0.0;
   this.projectedY=0.0;
   this.weight=1.0;
   this.friction=1.0;
   this.inertiaX=0.0;
   this.inertiaY=0.0;

   if(initX!=null)
   {
      this.x=initX;
      this.projectedX=initX;
   }
   if(initY!=null)
   {
      this.y=initY;
      this.projectedY=initY;
   }

   this.relationships=new Array();

   var accumulatedForceX=0.0;
   var accumulatedForceY=0.0;

   // methods
   this.addRelationship=mObject_addRelationship;
   this.applyFriction=mObject_applyFriction;
   this.bounce=mObject_bounce;
   this.addInertia=mObject_addInertia;
   this.calcForce=mObject_calcForce;
   this.calcNewPos=mObject_calcNewPos;
   this.getX=mObject_getX;
   this.getY=mObject_getY;
   this.setPos=mObject_setPos;
   this.setXPos=mObject_setXPos;
   this.setYPos=mObject_setYPos;
   this.setSpeed=mObject_setSpeed;
   this.setXSpeed=mObject_setXSpeed;
   this.setYSpeed=mObject_setYSpeed;
   this.update=mObject_update;
}
