From TDN
// --------------------------------------------------------------------
// initData()
//
// This funciton will initialize all the base data structures for our game
// --------------------------------------------------------------------
function initData()
{
$playerTeam = Circles;
$creationCount = 100;
// lets initialize our data structure
new ScriptObject(gameData);
// lets initialize our team objects
gameData.circles = new t2dSceneObject(Circles);
gameData.squares = new t2dSceneObject(Squares);
//Layer Sets
$collision::playerLayer = 5;
$collision::mapLayer = 30;
$collision::baseLayer = 29;
//Group Sets
$collision::playerGroup = 5;
$collision::mapGroup = 10; // Remember we set this earlier?
$collision::baseGroup = 15;
}
// --------------------------------------------------------------------
// t2dSceneObject::setSelectionName()
//
// This function sets the "selectionName"
// --------------------------------------------------------------------
function t2dSceneObject::setSelectionName(%this, %name)
{
%this.name = %name;
}
// --------------------------------------------------------------------
// gameData::spawnObject()
//
// This funciton will spawn our object if we pass the correct values:
// %type = a class object
// %team = a team object
// %pos = a spawning position
// %name = a name to set the object to
// --------------------------------------------------------------------
function gameData::spawnObject(%this, %type, %team, %pos, %name)
{
// lets piece together our call to create a new t2dStaticSprite
%eval = "%obj = new t2dStaticSprite(" @ %name @ ") { config = " @ %type @ "; sceneGraph = $strategyScene; };";
eval(%eval);
// set the position of the object
%obj.setPosition(%pos);
// set the team of the object, using getId() to get the team object's
// internal ID
%obj.team = %team.getId();
// set the objects type
%obj.type = %type;
// set the objects name
%obj.name = %name;
// set the objects name
%obj.setSelectionName(%type.getName());
}
//
// Entity::onAdd, this function is like an init function, but for the entity class. (%this is the object before the dot.)
// E.G. %this = $dave where $dave is an Entity and you do $dave.onAdd();
//
function Entity::onLevelLoaded(%obj, %scene)
{
// set the object hit points to the max hit points
%obj.hitPoints = %obj.hitPointsMax;
// create a label name using the name plus the string "Label"
%labelName = %obj.name @ "Label";
// create the label object name
%labelObjName = %labelName @ "Obj";
// create the label object
%labelObj = new t2dSceneObject(%labelObjName) { sceneGraph = %scene; };
// mount the object just above the sprite
%labelObj.mount(%obj, "0 -1");
// create the whole label name
%wholeLabelName = %labelname @ "Whole";
// lets create the gui objects that will serve as the label, the hit points bar,
// and the border for the hit points bar
new GuiControl(%wholeLabelName) {
profile = "GuiDefaultNoSelectNoBorderProfile";
horizSizing = "right";
vertSizing = "bottom";
fitParentWidth = "0";
fitParentHeight = "0";
position = "-1 -1";
extent = "50 25";
minExtent = "8 2";
visible = "1";
};
new GuiTextCtrl(%labelName) {
profile = "GuiLabel" @ %obj.team.getName() @ "TextProfile";
horizSizing = "right";
vertSizing = "bottom";
fitParentWidth = "0";
fitParentHeight = "0";
position = "0 -2";
extent = "50 12";
minExtent = "8 2";
visible = "1";
text = %obj.name;
maxLength = "255";
};
// grab the scale factor
%scaleFactor = %obj.hitPointBarScaleFactor;
// create the border bar name
%borderBar = %obj.name @ "HitPointsBorderBar";
new GuiBitmapCtrl(%borderBar) {
profile = "GuiDefaultNoSelectProfile";
horizSizing = "right";
vertSizing = "bottom";
fitParentWidth = "0";
fitParentHeight = "0";
position = "0 8";
extent = mFloor(%obj.hitPointsMax/%scaleFactor) SPC "4";
minExtent = "0 2";
visible = "1";
wrap = "0";
};
%bar = %obj.name @ "HitPointsBar";
new GuiBitmapCtrl(%bar) {
profile = "GuiDefaultNoSelectProfile";
horizSizing = "right";
vertSizing = "bottom";
fitParentWidth = "0";
fitParentHeight = "0";
position = "0 8";
extent = mFloor(%obj.hitPointsMax/%scaleFactor) SPC "4";
minExtent = "0 2";
visible = "1";
bitmap = "Strategy/data/images/Bar";
wrap = "0";
};
// now we want to add our GUIs to the main GUI
%wholeLabelName.add(%labelName);
%wholeLabelName.add(%borderBar);
%wholeLabelName.add(%bar);
// then we store all the important Ids and values on our object
%obj.labelName = %labelName.getId();
%obj.hitPointsBorderBar = %borderBar.getId();
%obj.hitPointsBar = %bar.getId();
%obj.labelObj = %labelObj;
%obj.labelGui = %wholeLabelName.getId();
// attach our GUI to our label object
%labelObj.attachGui(%wholeLabelName, SceneWindow2D);
return %obj;
}
//
// Entity::delete. This deletes the labels for the entity.
//
function Entity::onLevelEnded(%obj, %scene)
{
%labelName = %obj.name @ "Label";
%labelObjName = %labelName @ "Obj";
%wholeLabelName = %labelname @ "Whole";
%borderBar = %obj.name @ "HitPointsBorderBar";
%bar = %obj.name @ "HitPointsBar";
%labelName.delete();
%labelObjName.delete();
%wholeLabelName.delete();
%borderBar.delete();
%bar.delete();
}
//
// Base::onLevelLoaded
//
function Base::onLevelLoaded(%this, %scene)
{
%this.team.base = %this;
}
// --------------------------------------------------------------------
// gameData::createObject()
//
// This is the initial function in our unit creation function chain,
// this function will initalize the process and then call
// gameData::creatingObject() which will repeat until the creation process
// is finished
// --------------------------------------------------------------------
function gameData::createObject(%this, %type, %team)
{
// check if this is the player's team, that way we can handle
// things such as the progress bar gui, etc
if(%team == $playerTeam)
{
%isPlayer = true;
} else
{
%isPlayer = false;
}
// then lets check if we're already creating something
// we want to make sure we can only create one thing at a time
if(%team.isCreating)
{
// lets return out so we don't do anything
return;
}
// now we set that we have started creating something
%team.isCreating = true;
// initiate the creating count value that we will use during the
// creation process
%team.creatingCount = 0;
// now we call the repeatable function to create the object
// we pass %isPlayer so we don't have to do this check again
// for the progress bar updates during creation
%this.creatingObject(%type, %team, %isPlayer);
}
// --------------------------------------------------------------------
// gameData::creatingObject()
//
// This is a re peatable function that will call itself until creation
// is done, this allows us to make unit creation a process that can have
// a linked update and pogress bar
// --------------------------------------------------------------------
function gameData::creatingObject(%this, %type, %team, %isPlayer)
{
// increment the team's creating count to represent progress
// this is based off of a value in the object's type...
// 1 will go through at a normal speed, 2 will go through at double
// the speed, etc
%team.creatingCount = %team.creatingCount + %type.creationTime;
// if we haven't reached the creation time then we want to continue
// creating the object with a schedule passing all viable information
if(%team.creatingCount <= $creationCount)
{
%this.schedule(100, "creatingObject", %type, %team, %isPlayer);
} else
{
// this means we have finished the creation time of the object
// so we need to increment the team's object type count
%eval = "%team.base." @ %type.class.getName() @ "Count++;";
eval(%eval);
// now lets spawn the actual object
%this.spawnUnit(%type, %team, %type.getName());
// set the isCreating to false so we can continue on with something else
%team.isCreating = false;
}
}
// --------------------------------------------------------------------
// gameData::spawnUnit()
//
// This is a small function that in turn calls our gameData::spawnObject()
// function
// --------------------------------------------------------------------
function gameData::spawnUnit(%this, %type, %team, %name)
{
// lets grab the bases' position and divide it up into X and Y
%pos = %team.base.getPosition();
%posX = getWord(%pos, 0);
%posY = getWord(%pos, 1);
// now lets add 10 to the Y position so it will spawn our units
// just below the base
%posY = %posY + 10;
// lets peice together our positions again
%pos = %posX SPC %posY;
// then we call spawnObject() passing it some of the same values along
// with our new values
%this.spawnObject(%type, %team, %pos, %name);
}