//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

if (!isObject(SpawnAreaRandomVelocityBehavior))
{
   %template = new BehaviorTemplate(SpawnAreaRandomVelocityBehavior);
   
   %template.friendlyName = "Spawn Area With Random Velocity";
   %template.behaviorType = "AI";
   %template.description  = "Spawns objects inside the area of this object with each spawned object having its own random velocity.";

   %template.addBehaviorField(object, "The object to clone", object, "", t2dSceneObject);
   %template.addBehaviorField(count, "The number of objects to clone (-1 for infinite)", int, 50);
   %template.addBehaviorField(spawnTime, "The time between spawns (seconds)", float, 2.0);
   %template.addBehaviorField(spawnVariance, "The variance in the spawn time (seconds)", float, 1.0);
   
   %spawnLocations = "Area" TAB "Edges" TAB "Center" TAB "Top" TAB "Bottom" TAB "Left" TAB "Right";
   %template.addBehaviorField(spawnLocation, "The are in which objects can be spawned", enum, "Area", %spawnLocations);
   
   %template.addBehaviorField(minAngle, "The minimum angle of the object's direction (degrees)", float, 0.0);
   %template.addBehaviorField(maxAngle, "The maximum angle of the object's direction (degrees)", float, 360.0);
   %template.addBehaviorField(minSpeed, "The minimum speed of the object (world units per second)", float, 10.0);
   %template.addBehaviorField(maxSpeed, "The maximum speed of the object (world units per second)", float, 25.0);
   %template.addBehaviorField(minRotationSpeed, "The minimum rotation speed of the object (degrees)", float, -90.0);
   %template.addBehaviorField(maxRotationSpeed, "The maximum rotation speed of the object (degrees)", float, 90.0);

 %template.addBehaviorField(scoreCount, "The number to modify the value by (-ve is ok)", int, 100);
   %template.addBehaviorField(counter, "The counter object", object, "", t2dSceneObject);


}

function SpawnAreaRandomVelocityBehavior::onAddToScene(%this, %scenegraph)
{
   %this.spawnCount = 0;
   %this.schedule(%this.spawnTime, "spawn");
}

function SpawnAreaRandomVelocityBehavior::spawn(%this)
{

if (!isObject(%this.counter))
      return;

   %counter = %this.counter.getBehavior("CounterBehavior");
   if (!isObject(%counter))
      return;

   %counter.ModifyCount(%this.scoreCount);

   if (!isObject(%this.object))
      return;


   
   %clone = %this.object.cloneWithBehaviors();
   %xPos = 0;
   %yPos = 0;
   %spawnLocation = %this.spawnLocation;
   %edges = "Top" TAB "Bottom" TAB "Left" TAB "Right";
   if (%spawnLocation $= "Edges")
      %spawnLocation = getField(%edges, getRandom(0, 3));
   
   switch$ (%spawnLocation)
   {
      case "Area":
         %xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
         %yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
      case "Center":
         %xPos = %this.owner.position.x;
         %yPos = %this.owner.position.y;
      case "Top":
         %xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
         %yPos = getWord(%this.owner.getAreaMin(), 1);
      case "Bottom":
         %xPos = getRandom(getWord(%this.owner.getAreaMin(), 0), getWord(%this.owner.getAreaMax(), 0));
         %yPos = getWord(%this.owner.getAreaMax(), 1);
      case "Left":
         %xPos = getWord(%this.owner.getAreaMin(), 0);
         %yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
      case "Right":
         %xPos = getWord(%this.owner.getAreaMax(), 0);
         %yPos = getRandom(getWord(%this.owner.getAreaMin(), 1), getWord(%this.owner.getAreaMax(), 1));
   }
   
   %clone.position = %xPos SPC %yPos;
   
   %direction = getRandom(%this.minAngle * 1000, %this.maxAngle * 1000);
   %speed = getRandom(%this.minSpeed * 1000, %this.maxSpeed * 1000);
   %clone.setLinearVelocityPolar(%direction * 0.001, %speed * 0.001);
   
   %rotation = getRandom(%this.minRotationSpeed * 1000, %this.maxRotationSpeed * 1000);
   %clone.setAngularVelocity(%rotation * 0.001);
   
   %this.spawnCount++;
   if (%this.spawnCount < %this.count)
   {
      %minTime = (%this.spawnTime - %this.spawnVariance) * 1000;
      %maxTime = (%this.spawnTime + %this.spawnVariance) * 1000;
      %spawnTime = getRandom(%minTime, %maxTime);
      %this.schedule(%spawnTime, "spawn");
   }
}
