Graphics Reference
In-Depth Information
When this enemy's health is at 0, it is time to explode:
if( myDataManager.GetHealth()==0 )
{
Now, as I write this, I can almost hear the collective mutter of hundreds of program-
mers tutting at how the ID is retrieved from the projectile. It is not the most efficient or the
tidiest way of doing it, but it works. Each projectile is named by its owner ID, meaning that
the quickest way to find out where the projectile came from is to get its name via collider.
gameObject.name. In this line of code, the ID is retrieved from the name of the projectile
hitting the enemy, converted into an integer by int.Parse() and stored in the variable tem-
pINT for use later in the script.
Perhaps a cleaner method would have been to store a reference to the collider's
gameObject and use gameObject.GetComponent to find its ProjectileController.cs script
component. From there, we could have gotten the ID from ProjectileController; however,
without doing some extensive tests, it is hard to say which method would end up more or
less efficient. In this code, the gameObject's name is converted to an integer, and I would
expect the difference in CPU hit to be negligible between converting the string to an inte-
ger and having to look up the ProjectileController component with GetComponent():
tempINT= int.Parse( collider.gameObject.name );
The way that this script tells the game controller about the destruction of this enemy may
seem a little odd, as it is contained in another function named TellGCEnemyDestroyed().
By splitting it out into its own function, any script that were to derive from this one could
easily override the TellGCEnemyDestroyed() function to easily swap out the game con-
troller for one of another type or name:
// tell game controller to make an explosion at our position
// and to award the player points for hitting us
TellGCEnemyDestroyed();
Boss enemy types will use this script, too, so we have the Boolean variable isBoss
to tell whether or not this is a regular enemy or an end-of-level boss. If it is a boss,
TellGCBossDestroyed() is called, which tells the game controller about the hit so that it
can act differently to this enemy's destruction. In this game, the game controller will start
to end the level when the boss has been destroyed, and this function is called:
// if this is a boss enemy, tell the game controller when we
// get destroyed so it can end the level
if( isBoss )
TellGCBossDestroyed();
Regardless of the type of enemy, it was a hit, and its health is at 0, so it needs destroy-
ing. The Destroy command removes this enemy's gameObject from memory:
// destroy this
Destroy(gameObject);
}
}
}
Search WWH ::




Custom Search