Game Development Reference
In-Depth Information
TileField tiles = GameWorld.Find("tiles") as TileField;
if (BoundingBox.Top >= tiles.Rows
tiles.CellHeight)
this .Die( true );
In the beginning of the Update method, we call the base .Update method to ensure
that the animation is updated. Then, we do the physics and collisions (which still
needs to be done, even if the player is dead). Then, we check if the player is alive.
If not, we are done, and we return from the method.
Now that the player can die in various gruesome ways, we have to extend the
enemy classes to deal with the collisions. In the Rocket class, we added a method
called CheckPlayerCollision , which we call in the rocket's Update method. Inside the
CheckPlayerCollision method, we simply check if the player collides with the rocket.
If that is the case, we call the Die method on the player object. The complete method
is given as follows:
public void CheckPlayerCollision()
{
Player player = GameWorld.Find("player") as Player;
if ( this .CollidesWith(player))
player.Die( false );
}
In the case of the patrolling enemy, we do exactly the same. We add the same method
to that class, and call it from the Update method. The version in the Sparky class is
slightly different. The player should only die if Sparky is currently being electro-
cuted. Therefore, we slightly change the method, as follows:
Player player = GameWorld.Find("player") as Player;
if ( this .CollidesWith(player) && idleTime <= 0.0f)
player.Die( false );
Finally, the turtle enemy adds even more behavior. As a first step, we check if
the turtle collides with the player. If that is not the case, we simply return from the
CheckPlayerCollision method since we are done:
Player player = GameWorld.Find("player") as Player;
if (! this .CollidesWith(player))
return ;
If there is a collision, there are two possibilities. The first one is that the turtle is
currently sneezing. In that case the player dies:
if (sneezeTime > 0)
player.Die( false );
The second case is that the turtle is in waiting mode, and the player is jumping on the
turtle. In that case the player should make an extra high jump. An easy way to check
Search WWH ::




Custom Search