Game Development Reference
In-Depth Information
The result of which would be that as soon as the player releases the left or right
arrow key, the x velocity would become zero. However, there are two situations in
which case we do not want this to happen, namely if the player is not on the ground,
or if the player is moving on ice. Therefore, we will only set the x velocity to zero
if the player is not walking on ice, and the player is standing on the ground:
else if (!walkingOnIce && isOnTheGround)
velocity.X = 0.0f;
The only thing we still need to do is find out whether the player is walking on ice
or not, and update the walkingOnIce member variable accordingly. We already look at
the tiles surrounding the player in the HandleCollisions method, so if we extend that
method to also check if the player is walking on ice, we only need to add a few lines
of code.
In the beginning of this method, we assume that we are not walking on ice:
walkingOnIce = false ;
We can only walk on ice, if we are on the ground. We check if we are on the ground
in the following if instruction:
if (previousYPosition <= tileBounds.Top && tileType != TileType.Background)
{
isOnTheGround = true ;
velocity.Y = 0;
}
In order to check if the tile we are standing on is an ice tile, we have to retrieve
the tile from the tile field and check its Ice property. We can simply access this tile
through the currentTile variable that contains the current tile. Finally we update the
walkingOnIce variable. We use a logical or operator, so that if the player is only partly
on an ice tile, the variable will also be set to true :
if (currentTile != null )
walkingOnIce = walkingOnIce || currentTile.Ice;
We only perform this instruction if the currentTile variable does not point to null .The
reason we use the logical or to calculate whether we are walking on ice is to take all
surrounding tiles into account. The effect is that the character will keep on moving
until he/she is not standing on an ice tile anymore (not even partly).
29.4 Enemies Colliding with the Player
The final kind of interaction that we would like to add is collisions with enemies. In
many cases, when the player collides with an enemy, it causes the player's untimely
Search WWH ::




Custom Search