Game Development Reference
In-Depth Information
more convenient methods to the Animal class. One useful method is checking if
agiven x and y coordinate is outside of the tilefield. For that, we add a method
IsOutsideField to the Animal class. This method is rather straightforward:
public bool IsOutsideField(Point p)
{
GameObjectGrid tilefield = GameWorld.Find("tilefield") as GameObjectGrid;
return (p.X < 0 || p.X >= tilefield.Columns || p.Y < 0 || p.Y >= tilefield.Rows);
}
This method is used in another method, GetTileType , that retrieves the type of the tile
for a given point p . The first thing we check in this method is if the point is outside
of the tilefield. If that is the case, we return a 'background' tile type:
if (IsOutsideField(p))
return TileType.Background;
In all other cases, we can retrieve the tile type by getting the Tile object from the tile
field, and accessing its TileType property:
GameObjectGrid tilefield = GameWorld.Find("tilefield") as GameObjectGrid;
Tile t = tilefield.Objects[p.X, p.Y] as Tile;
return t.TileType;
Now we can go back to the Update method and check if we have fallen off of the tile
field. If so, we set the animal visibility to false , and its velocity to zero, to ensure
that the animal does not keep moving indefinitely while it is invisible:
Point target = GetCurrentBlock();
if ( this .GetTileType(target) == TileType.Background)
{
this .Visible = false ;
velocity = Vector2.Zero;
return ;
}
Another possibility is that we ran into a wall tile. If that is the case, we have to stop
moving:
else if ( this .GetTileType(target) == TileType.Wall) // we ran into a wall
{
this .StopMoving();
return ;
}
Stopping moving is not as easy as it sounds. We could simply set the animal velocity
to zero, but then the animal would be partly in another tile. What we want to do is
Search WWH ::




Custom Search