//********************************************
// Looping path shape
//********************************************

function PathShapeData::onAdd(%this,%obj)
{
   // do whatever you want with that shape, e.g. play animations:
   %obj.playThread(0,"root");
}
function PathShapeData::onNode(%this,%theshape,%node) {
   echo("PathShape::onNode(" @ %this @ "," SPC %theshape @ "," SPC %node@")");
   if(%theshape.path.isLooping && (%node == %theshape.loopNode)) {
       //echo("At end of path, setting position back to starting node.");
	   %theshape.setPosition(0);
   }
   
   // pause for 5 seconds at each node
   %theshape.setState( "stop" );
   %theshape.playThread(0,"root"); 
   %this.thisShape = %theshape; 
   
   if(!%theshape.path.getObject(%node).stopOnNode)
   {
      %theshape.playThread(0,"run"); 
      %theshape.setState( "forward" );  
   }
}

function PathShapeData::pushForward(%this)
{
   %this.thisShape.setState("forward" );    
}

function PathShape::followPath(%this,%path) {
   echo("PathShape::followPath(" @ %this @ "," SPC %path @")");

   %this.path = %path;
   if (!(%this.speed = %path.speed))
      %this.speed = 5;

   if (%path.isLooping) {
      %this.loopNode = %path.getCount();  
   } else {
      %this.loopNode = -1;
   }

   //echo("PathShape::followPath() loopNode == ", %this.loopNode);
   
   %this.playThread(0,"run");
   
   %this.pushPath(%path);   
   %this.popFront(); // This pop removes initial DUMMY node.
}

function PathShape::pushPath(%this,%path) {
   //echo("PathShape::pushPath(" @ %this @ "," SPC %path @")");
   for (%i = 0; %i < %path.getCount(); %i++) {
      %this.pushNode(%path.getObject(%i));
   }
   // If looping, push the starting node on to end to make this a loop
   if(%this.path.isLooping) {
       %this.pushNode(%path.getObject(0));
   }
}

function PathShape::pushNode(%this,%node)
{
   if (!(%speed = %node.speed))
      %speed = %this.speed;
   if ((%type = %node.type) $= "")
      %type = "Normal";
   if ((%smoothing = %node.smoothing) $= "")
      %smoothing = "Linear";

   %this.pushBack(%node.getTransform(),%speed,%type,%smoothing);
}




function Hare::onAdd(%this)
{
   // The mission editor invokes this method when it wants to create
   echo("dude");
   %this.playThread(0,"root");
}