LRDL2 – Creating a Roguelike Engine – Part 2 – A TileMap

<-Take me to Part 1!                                   Take me to Part 3!->

We have a Tile class and a few derived classes, but we need to organize them somehow into a “map” the player can navigate. We can use an array for this–essentially we arrange our tiles of content (floors with monsters, walls, floor with a player, etc.) into a miniature “level” with the map of tiles having a maximum width and height. LRDL2 uses a 2D rectangular array to hold the tiles that are written to the console. The Map property is the container for this array. It also has a print method that renders this view. Since it is a very light console application, having a dedicated Model-View-Controller architecture might not be necessary, though that is still on the table, especially if the code base grows[Edit: MVC is now on the table]! For now, the print class simply writes the symbols for each tile directly to the console to give the user a representation of the game world (see the PrintMap method below). The current repository for this project can be found here.


/// <summary>
/// A 2D map holding floor tiles.
/// </summary>
public class TileMap
{
  /// <summary>
  /// Creates the floor tiles for the engine.
  /// </summary>
  public Tile[,] Map { get; set; }

  /// <summary>
  /// Creates a tile map of the specified dimensions.
  /// </summary>
  public TileMap ()
  {
    //Initialize "blank" floor of tiles of the preset dimensions.
    Map = new Tile[Configuration.MapWidth, Configuration.MapHeight];

    //Instantiate each Tile for this TileMap with a FloorTile as default.
    for (int width = 0; width < Configuration.MapWidth; width++)
    {
      for (int height = 0; height < Configuration.MapHeight; height++)
      {
        Map[width, height] = new FloorTile();
      }
    }
  }

  /// <summary>
  /// Prints the contents of Map directly to console.
  /// </summary>
  public void PrintMap ()
  {
    //Manually printing the TileMap
    for (int height = 0; height < Configuration.MapHeight - 1; height++)
    {
      for (int width = 0; width < Configuration.MapWidth - 1; width++)
      {
        Console.Write(Map[width, height].MySymbol);

        if (width == Configuration.MapWidth - 2)
        {
          Console.WriteLine();
        }
      }
    }
  }
}

 

<-Take me to Part 1!                                   Take me to Part 3!->

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s