TGB/Tutorials/Asteroids/Section6

From TDN


Asteroids Tutorial

Written for TGB Version: 1.6


Section 6

Having added defined waves of asteroid spawns, now the appropriate notification of each wave via a text object will be shown to the player.


Starting with Behaviors


As has been typical for the last couple of sections, first we will edit and add new behaviors. Since the spawning of waves is handled by the spawn area behavior, let's start there. Add to the bottom of the spawnArea.cs this function:

function SpawnAreaBehavior::messageTextObject(%this)
{
   %this.textObject.displayLevelText();
}


Now, in the behavior template section at the top, add the following field:

%template.addBehaviorField(textObject, "The object which displays level text", object, "", t2dSceneObject);


Under that in the onAddToScene function, add this line:

%this.schedule(%this.spawnTime * 100, "messageTextObject");


Then add a similar line to the update function after the spawn schedule inside the if{}:

%this.schedule(%this.spawnTime * 1000, "messageTextObject");


Note: It is not a mistake between the 2 schedules above, onAddToScene does not wait the full time in seconds. (If you are a bit confused, the schedule command takes time in milliseconds so normally we need to multiply the float values specified in the behavior fields by 1000.)

Create a new script file. Call it:

gameText.cs

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------

if (!isObject(GameTextBehavior))
{
   %template = new BehaviorTemplate(GameTextBehavior);
   
   %template.friendlyName = "Game Text";
   %template.behaviorType = "GUI";
   %template.description  = "Displays text between levels, game over, etc.";

   %template.addBehaviorField(fadeIncrement, "The smoothness of the text fade in/out", int, 30);
   %template.addBehaviorField(fadeLength, "The length of time in seconds the text should fade", float, 2.0);
   %template.addBehaviorField(displayLength, "The length of time in seconds the text should be displayed", float, 1.0);
}

function GameTextBehavior::onAddToScene(%this)
{
   %this.levelNumber = 0;
   %this.startPosition = %this.owner.getPosition();
   %this.fadeLength = %this.fadeLength * 1000;
   %this.displayLength = %this.displayLength * 1000;
}

function GameTextBehavior::displayLevelText(%this)
{
   %this.levelNumber = %this.levelNumber++;
   %this.owner.text = "Level" SPC %this.levelNumber;
   %this.owner.setBlendAlpha(0);
   %this.owner.setPosition(0, 0);
   %this.fadeInText(1, %this.fadeLength, 1);
}

function GameTextBehavior::fadeInText(%this, %toAlpha, %time, %toggle)
{
   if(%time > %this.fadeIncrement)
   {
      %alpha = %this.owner.getBlendAlpha();
      %updatesRemaining = %time / %this.fadeIncrement;
      %alpha += (%toAlpha - %alpha) / %updatesRemaining;
      %this.owner.setBlendAlpha(%alpha);
      %this.schedule(%this.fadeIncrement, "fadeInText", %toAlpha, %time - %this.fadeIncrement, %toggle);
   }else
   {
      %this.owner.setBlendAlpha(%toAlpha);
      
      if (%toggle == 1)
         %this.schedule(%this.displayLength, "removeLevelText");
   }
}

function GameTextBehavior::removeLevelText(%this)
{
   %this.fadeOutText(0, %this.fadeLength);
}

function GameTextBehavior::fadeOutText(%this, %toAlpha, %time)
{
   if(%time > %this.fadeIncrement)
   {
         %alpha = %this.owner.getBlendAlpha();
         %updatesRemaining = %time / %this.fadeIncrement;
         %alpha += (%toAlpha - %alpha) / %updatesRemaining;
         %this.owner.setBlendAlpha(%alpha);
         %this.schedule(%this.fadeIncrement, "fadeOutText", %toAlpha, %time - %this.fadeIncrement);
   }else
   {
         %this.owner.setBlendAlpha(%toAlpha);
         %this.owner.setPosition(%this.startPosition);
   }
}

function GameTextBehavior::gameOverText(%this)
{
   %this.owner.text = "Game Over";
   %this.owner.setBlendAlpha(0);
   %this.owner.setPosition(0, 0);
   %this.fadeInText(1, %this.fadeLength, 1);
}


The gameText behavior allows us to display the text of our choice with a some fade in/fade out effects. It will mostly be used to inform the player that a new wave of asteroids is incoming. While "Wave" might be a confusing name, the more typical "Level" description will be used so the player can easily measure his or her progress.


Back in the TGB Editor


Start up TGB and from the create tab, drag a t2dTextObject into the scene. Type into whatever text you wish or leave it blank even, the gameText behavior will change the text for each level. Place the text somewhere outside of the camera view, like most of the objects in the scene.

Image:TGB Asteroids Tutorial 6 1.jpg

In the edit tab, give the text object the Game Text behavior.

Image:TGB Asteroids Tutorial 6 2.jpg

Depending on whether you want the text to display above the player ship and asteroids or below it, change the layer as well. Here, the layer was set to 10 so the text would appear below the player ship as not to obstruct the player finding the ship on screen. Don't forget to give the text object a name in the scripting rollout. Next, click on the spawn area object and make sure to set the textObject field to the name you just gave the game text object.

Image:TGB Asteroids Tutorial 6 3.jpg

Remember to save the scene and hit play to test it.

Image:TGB Asteroids Tutorial 6 4.jpg

Hopefully everything added so far has been interesting and made the game more fun. If you've played quite a number of times, perhaps it would be nice if some of those high scores were saved? Continue to the next section and we'll add that functionality.


Return to the Asteroids Tutorial Hub