TGB/Tutorials/Asteroids/Section2

From TDN


Asteroids Tutorial

Written for TGB Version: 1.6


Section 2

After trying out some gameplay, this section fixes a few issues in the behavior scripts.


Playing Around


Feel free to experiment with the behavior settings of each object in the level builder. It's a nice way to see how easy it is to tweak gameplay and hopefully make things more fun.

Did you notice anything unusual while playing the asteroids level from either the behavior playground or the copy you just created? While we deliberately reduced the large asteroid count in the spawn machine from 50 to 10, there are a few things that we need to address.

1. The player can still shoot projectiles even when dead.
2. The player ship can still move around the screen while in a dead state.

Let's fix these issues.

Fixing Things


A good way to approach solving these issues is to have a toggle that lets our player movement and shooting functions know the player is dead and all keyboard input should be ignored. In the text editor of your choice, open up the asteroidsControls.cs file found in the project's behavior folder.

Change the onUpdate function to look like this:

function AsteroidsControlsBehavior::onUpdate(%this)
{
   if (%this.owner.control == 0)
      return;

   %this.owner.setImpulseForcePolar(%this.owner.rotation, (%this.up - %this.down) * %this.acceleration);
   %this.owner.setAngularVelocity((%this.right - %this.left) * %this.turnSpeed);
}

Now we've added a way that tells this function if %this.owner.control (which is our player ship) is 0, it will return out and not apply any forces or velocity to the player ship. Save the changes and close this file.

Let's open up the shoots.cs file and do something similar. Find the fire function and add the exact same if statement to the top of the function. It should now look like this:

function ShootsBehavior::fire(%this, %val)
{
   if (%this.owner.control == 0)
      return;
   
   if (%val == 0)
   {
      cancel(%this.fireSchedule);
      return;
   }
   
   if (!isObject(%this.projectile))
      return;
   
   %projectile = %this.projectile.cloneWithBehaviors();
   
   %projectile.setPosition(%this.owner.position);
   %projectile.setRotation(%this.owner.rotation);
   %projectile.setLinearVelocityPolar(%this.owner.rotation, %this.projectileSpeed);
   
   if (!isEventPending(%this.fireSchedule))
      %this.fireSchedule = %this.schedule(%this.fireRate * 1000, "fire", 1);
}

Save and close the file. Our if statements are set up to ignore player input when dead, so let's set the toggle switching in the kill and spawn functions for the player ship. They can be found in takesDamage.cs. In the kill function we want to set the control to 0 and in the spawn function we'll set the control to 1. The kill function also has a setAtRest() command to ensure the player respawns at the same spot he was killed at.

function TakesDamageBehavior::kill(%this)
{
   %this.lives--;
   if (%this.lives <= 0)
   {
      %this.owner.safeDelete();
      return;
   }
   
   %this.invincible = true;
   %this.owner.visible = false;
   %this.owner.control = 0;
   %this.owner.setAtRest();
   %this.owner.collisionActiveReceive = false;
   %this.schedule(%this.respawnTime * 1000, "spawn");
}
function TakesDamageBehavior::spawn(%this)
{
   %this.owner.collisionActiveReceive = true;
   %this.schedule(%this.invincibleTime * 1000, "setVincible");
   %this.health = %this.startHealth;
   %this.owner.setBlendColor(1, 1, 1, 1);
   %this.owner.visible = true;
   %this.owner.control = 1;
   %this.owner.setFrame(%this.startFrame);
   
   if (isObject(%this.respawnEffect))
   {
      %explosion = %this.respawnEffect.cloneWithBehaviors();
      %explosion.position = %this.owner.position;
      %explosion.setEffectLifeMode("Kill", 1.0);
      %explosion.playEffect();
   }
}

Save the takesDamage file and play the scene. The player can now no longer shoot or move while dead.

We now have some decent, basic gameplay. To make this a game though, we need to give the player a reason to shoot at all the asteroids. A scoring mechanic will be the next piece of functionality we add. Continue on to the next section when you're ready.


Return to the Asteroids Tutorial Hub