TGB/Tutorials/Asteroids/Section5/SpawnArea
From TDN
[edit] Asteroids TutorialWritten for TGB Version: 1.6 |
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Modified Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------
if (!isObject(SpawnAreaBehavior))
{
%template = new BehaviorTemplate(SpawnAreaBehavior);
%template.friendlyName = "Spawn Area";
%template.behaviorType = "AI";
%template.description = "Spawns objects inside the area of this object";
%template.addBehaviorField(object, "The object to clone", object, "", t2dSceneObject);
%template.addBehaviorField(count, "The number of objects to clone", int, 50);
%template.addBehaviorField(addOnRespawn, "The number of objects to add in each wave of respawns", int, 1);
%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);
}
function SpawnAreaBehavior::onAddToScene(%this, %scenegraph)
{
%this.asteroidsRemoved = 0;
%this.spawnCount = 0;
%this.schedule(%this.spawnTime * 100, "spawn");
}
function SpawnAreaBehavior::spawn(%this)
{
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;
%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");
}
}
function SpawnAreaBehavior::update(%this, %object)
{
%this.asteroidsRemoved = %this.asteroidsRemoved + 1;
%totalSpawns = %object.spawnOnRemoveCount * %this.count;
if (%this.asteroidsRemoved == %totalSpawns)
{
%this.asteroidsRemoved = 0;
%this.count = %this.count + %this.addOnRespawn;
%this.spawnCount = 0;
%this.schedule(%this.spawnTime * 1000, "spawn");
}
}



