Game Development Reference
In-Depth Information
{
textlines.Add(line);
line = fileReader.ReadLine();
}
As you can see, the first time we read a line, we also store the number of characters
in that line, so that we know how many columns our game object grid should have.
Once we have read all the lines, we create the GameObjectGrid with the right number
of columns ( width ) and number of rows, which is the same as the number of lines in
the textlines list ( textlines.Count
1 ):
1, width, 1, "tiles");
GameObjectGrid tiles = new GameObjectGrid(textlines.Count
this .Add(tiles);
We also set the width and height of each cell in the grid, so that the game object grid
knows where to draw the tiles on the screen:
tiles.CellWidth = 72;
tiles.CellHeight = 55;
Then, we create the Tile objects and add them to the GameObjectGrid object.
for ( int x = 0; x < width; ++x)
for ( int y = 0; y < textlines.Count; ++y)
{
Tile t = LoadTile(textlines[y][x], x, y);
tiles.Add(t, x, y);
}
The nested for -loop examines all the characters that we read from a file. Just like we
did in Penguin Pairs , we use a method called LoadTile , which creates a Tile object for
us, given a character and the x and y positions of the tile in the grid.
Inside the LoadTile method, we want to load a different tile according to the char-
acter that was passed as a parameter. For each type of tile, we add a method to the
Level class that creates that particular kind of tile. For example, the LoadWaterTile
loads a background tile with a water drop on top of it.
All of the methods in the LevelLoading.cs file are private . This means that only
methods inside the Level class can access these methods. Why did we do this? The
reason is that we want to be sure that a level is only loaded when needed. Users
of the Level class do not have to call LoadTiles or LoadWaterTile explicitly. If they
did (by accident hopefully), then the Level object might contain invalid information
afterwards. It might contain a different level than what the designer had in mind,
or perhaps some information related to the level, such as the number of water drops
gathered or the time left to finish the game, might no longer be up to date. By making
these methods private , we are sure that a level can only be loaded from a file within
the Level class itself.
Search WWH ::




Custom Search