Game Development Reference
In-Depth Information
Chapter 29
Adding Player Interaction
29.1 Introduction
In this chapter, we will add more interaction between the player and the objects in
the level. Currently, the player can walk around, and there is a basic physics system
that allows the player to jump, collide with wall tiles, or fall out of the screen. First,
we will look at a very simple kind of interaction: collecting the water drops. Then,
we will show how to create the behavior that allows the player to slide over ice.
Finally, we will deal with the part of the program that deals with the various player-
enemy interactions in the game.
29.2 Collecting Water Drops
The first thing we are going to add is the possibility for the player to collect the
water drops. A player collects a water drop if the bomb character collides with that
drop. In that case, we make the drop invisible. The place where we will do this is
in the WaterDrop class. The reason for this is clear: like before, each game object
is responsible for its own behavior. If we handle these collisions in the WaterDrop
class, each water drop checks if it collides with the player. We write this code in the
Update method. The first step is retrieving the player:
Player player = GameWorld.Find("player") as Player;
If the water drop is currently visible, we check if the water drop collides with the
player using the CollidesWith method. If so, we set the visibility status of the drop to
false . Furthermore, we also play a sound to let the player know that the water drop
has been collected:
if ( this .visible && this .CollidesWith(player))
{
 
Search WWH ::




Custom Search