Game Development Reference
In-Depth Information
its position and type, but that would not make the text file very clear. Another pos-
sibility is to divide the level up in small blocks, also called
tiles
. Every block has a
certain type (this could be a penguin, a white tile, an empty tile, a penguin, and so
on). A tile can then be represented by a character, and we can store the structure of
the level in a text file like in the following example:
#.......#
#...r...#
#.......#
#. .#
#. .#
#. .#
#.......#
#...r...#
#.......#
In this level definition, a number of different blocks are defined. An iceberg tile is
defined by the '#' sign, a penguin by an 'r' character, a normal tile by a '.' character
and an empty tile by a space character. Now we can write a method that reads this
information from the text file, creates the tiles and stores them somewhere (probably
in a
GameObjectGrid
instance). This means that we will need different types of tiles:
a normal tile on which a penguin can stand, a transparent background tile, and a
wall (iceberg) tile against which a penguin can collide.
22.3 The
Tile
Class
To get things started, let us first write a basic
Tile
class. This class is going to be
a subclass of the
SpriteGameObject
class. The complete class can be found in List-
ing
22.1
. For now, we do not yet consider items in the level such as penguins, ice-
bergs, seals, or sharks. We only look at background (transparent) tiles, normal tiles,
and wall tiles. We introduce an enumerated type
TileType
to represent these different
varieties of tiles:
enum
TileType { Normal, Background, Wall };
In the
Tile
class, we then declare a member variable
type
to store the type of tile that
an instance represents:
protected
TileType type;
In order to accommodate for transparent tiles, we override the
Draw
method in the
Tile
class to only draw the sprite if the tile is not a background tile:
if
(type == TileType.Background)
return
;
base
.Draw(gameTime, spriteBatch);