Time for action - creating the TileMap class

  1. Add a new class called "TileMap" to the Robot Rampage project.
  2. Add the following using directives to the top of the class file:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
  3. Modify the declaration of the TileMap class to make it a static class:
    static class TileMap
    
  4. Add declarations to the TileMap class:
    #region Declarations
    public const int TileWidth = 32;
    public const int TileHeight = 32;
    public const int MapWidth = 50;
    public const int MapHeight = 50;
    public const int FloorTileStart = 0;
    public const int FloorTileEnd = 3;
    public const int WallTileStart = 4;
    public const int WallTileEnd = 7;
    static private Texture2D texture;
    static private List<Rectangle> tiles = new List<Rectangle>();
    static private int[,] mapSquares = new int[MapWidth, MapHeight];
    static private Random rand = new Random();
    #endregion
    
    
  5. Add the Initialize() method to the TileMap class:
    #region Initialization
    static public void Initialize(Texture2D tileTexture)
    {
    texture = tileTexture;
    tiles.Clear();
    tiles.Add(new Rectangle(0, 0, TileWidth, TileHeight));
    tiles.Add(new Rectangle(32, 0, TileWidth, TileHeight));
    tiles.Add(new Rectangle(64, 0, TileWidth, TileHeight));
    tiles.Add(new Rectangle(96, 0, TileWidth, TileHeight));
    tiles.Add(new Rectangle(0, 32, TileWidth, TileHeight));
    tiles.Add(new Rectangle(32, 32, TileWidth, TileHeight));
    tiles.Add(new Rectangle(64, 32, TileWidth, TileHeight));
    tiles.Add(new Rectangle(96, 32, TileWidth, TileHeight));
    for (int x = 0; x < MapWidth; x++)
    for (int y = 0; y < MapHeight; y++)
    {
    mapSquares[x, y] = FloorTileStart;
    }
    }
    #endregion
    

What just happened?

Most of the declarations for the TileMap class are constants that define the dimensions of the individual tiles, the size of the map, and the meanings of the tile index numbers.

In our case, each tile is 32 by 32 pixels, and the map will be 50 tiles wide and 50 tiles high, resulting in 2,500 individual map squares.

The tiles list contains a set of rectangles that correspond to the locations of each individual tile on the texture image. When a rectangle is added to the tiles list, it will automatically receive an index number. The first tile added will be index zero, the second will be index one, and so on.

It is these index numbers that we will store in the mapSquares array to indicate what type of terrain should be displayed for each square on the map.

When the Initialize() method is executed, eight rectangles are added to the tiles list. Each of these rectangles corresponds to the position of one of the tiles on the game's sprite sheet.

In the declarations area, we determined that the first four tiles (numbers zero through three) would be floor tiles, while the second set of four tiles will be considered wall tiles by the game's code.

After adding the tiles to the list, the Initialize() method loops through each square in the mapSquares array and sets it to the first of the floor tile indexes. This way we start with a known, empty map.

Map squares

We need to make a distinction between "squares" and "tiles" as we use them in our code. We will use "square" to refer to a location within the mapSquares array, while we will use "tile" to refer to the index number stored in a particular square.

Our first set of methods for the TileMap class deal with squares, providing methods for locating squares based on pixel positions, and providing locations in both world and screen coordinates for squares on the map.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.205.154