Game Development Reference
In-Depth Information
class
Level
const int
tileSize
= 32
int
width
,
height
int
tiles
[][]
function
Draw()
end
The tiles array stores the tile IDs from the level file, and the width/height corres-
ponds to those numbers in the level file. Loading the actual level file into the class
is very language specific, but the file is so basic that parsing it should not prove
to be difficult. Once the parsing is implemented, the next step is to implement the
level's draw function:
function
Draw()
for int
row
= 0,
row
<
height
,
row
++
for int
col
= 0,
col
<
width
,
col
++
// Draw tiles[row][col] at (col*tileSize,
row*tileSize)
loop
loop
end
Drawing the actual texture is also something that's platform specific, but most 2D
frameworks support drawing only a subset of a texture at a location. As long as
the tile ID can be converted to the correct sub-image in the tile set, drawing it is
relatively straightforward.
Oneimportant thing tonote isthat it may bepossible tocreate several different tile
sets that still use the same tile IDs. This is one easy way to implement seasonal or
holiday-themed “skins” for a game. In the winter, it would be possible to change
the tile set into something that's covered in snow instead. It would be the same ex-
act level, but it would look more festive.
Although this text-based tile map file format may work for simple levels, in a
commercial game a more robust format would be preferable.
Chapter 11
, “
Script-
ing Languages and Data Formats
,
” discusses other common file formats, some of
which may be preferable formats for tile maps.