Game Development Reference
In-Depth Information
The TurnAround method simply inverts the velocity and mirrors the animation:
public void TurnAround()
{
Mirror = !Mirror;
this .velocity.X = 120;
if (Mirror)
this .velocity.X =
this .velocity.X;
}
If the enemy is currently not waiting but walking, we need to find out if we have
reached the edge of the platform that we are walking on. We have reached an edge
in two cases: either there is a gap, so we cannot move any further, or there is a wall
tile that is blocking our way. We will use the bounding box of the enemy to find this
information. If the enemy is walking to the left, we check if the leftmost x -value
has reached a wall tile, or the border of the platform. If the enemy is walking to the
right, we check the rightmost x -value. We can calculate this x -value as follows:
TileField tiles = GameWorld.Find("tiles") as TileField;
float posX = this .BoundingBox.Left;
if (!Mirror)
posX = this .BoundingBox.Right;
Now, we calculate in which tile this x -value falls. We can do that by dividing the
x -value by the width of a tile. In order to make sure that we always get the correct
(lower bound) tile index, we use the Math.Floor method:
int tileX = ( int )Math.Floor(posX / tiles.CellWidth);
In a similar way, we can also calculate the y -index of the tile that we are currently
standing on:
int tileY = ( int )Math.Floor(position.Y / tiles.CellHeight);
Note that because we use the bottom of the sprite to represent the position of the
enemy, the y -index we get is the one of the tile below the enemy. Now, we have
to check if we have reached a wall tile, or the border of the platform. If the tile at
the calculated indices is a background tile, then we have reached the border of the
platform and we have to stop walking. If the tile at indices (tileX, tileY 1) (e.g., the
tile right next to the enemy) is a wall tile, we also have to stop walking. In order to
stop walking, we assign a positive value to the wait time and we set the x velocity
to zero:
1) == TileType.Normal ||
tiles.GetTileType(tileX, tileY) == TileType.Background)
if (tiles.GetTileType(tileX, tileY
{
Search WWH ::




Custom Search