Loading Map/Game Data from XML, part 2

Alright, in the last part we dealt with creating the basic map file. This time around we're going to look at the classes that hold the data that we will eventually be reading in.

The first class we're going to be looking at is the Map class.

    1:  public class Map {
    2:      private Vector2D startingPosition;
    3:      private Dictionary<Vector2D, Area> areas;
    4:   
    5:      public Map() {
    6:          this.areas = new Dictionary<Vector2D, Area>();
    7:      }
    8:   
    9:      public Area this[Vector2D position] {
   10:          get {
   11:              return this.areas[position];
   12:          }
   13:      }
   14:   
   15:      public Vector2D StartingPosition {
   16:          get {
   17:              return this.startingPosition;
   18:          }
   19:          set {
   20:              this.startingPosition = value;
   21:          }
   22:      }
   23:   
   24:      public void AddArea(Area area) {
   25:          if (area == null) throw new ArgumentNullException("area");
   26:          this.areas.Add(area.Position, area);
   27:      }
   28:   
   29:      public bool RemoveArea(Vector2D position) {
   30:          return this.areas.Remove(position);
   31:      }
   32:  }

As you can see, there are two other classes that Map relies on: Vector2D and Area. Both of these are defined below. The Map class is pretty straight forward, the only thing to note is the way in which the Areas are stored. Instead of using a fixed array, I'm using a Dictionary so that I can create a map with only the Areas I want defined and not have an array full of null Areas.

Next is the Area class.

    1:  public class Area {
    2:      private Vector2D position;
    3:      private string description;
    4:      private string title;
    5:      private List<string> exits;
    6:      private List<string> items;
    7:   
    8:      public Area(Vector2D position) {
    9:          this.position = position;
   10:          this.exits = new List<string>();
   11:          this.items = new List<string>();
   12:      }
   13:   
   14:      public Vector2D Position {
   15:          get {
   16:              return this.position;
   17:          }
   18:      }
   19:   
   20:      public string Description {
   21:          get {
   22:              return this.description;
   23:          }
   24:          set {
   25:              this.description = value;
   26:          }
   27:      }
   28:   
   29:      public string Title {
   30:          get {
   31:              return this.title;
   32:          }
   33:          set {
   34:              this.title = value;
   35:          }
   36:      }
   37:   
   38:      public IEnumerable<string> Exits {
   39:          get {
   40:              foreach (string exit in this.exits) {
   41:                  yield return exit;
   42:              }
   43:          }
   44:      }
   45:   
   46:      public int ExitCount {
   47:          get {
   48:              return this.exits.Count;
   49:          }
   50:      }
   51:   
   52:      public bool HasExit(string direction) {
   53:          return this.exits.Contains(direction);
   54:      }
   55:   
   56:      public void AddExit(string direction) {
   57:          if (!this.exits.Contains(direction)) {
   58:              this.exits.Add(direction);
   59:          }
   60:      }
   61:   
   62:      public void RemoveExit(string direction) {
   63:          this.exits.Remove(direction);
   64:      }
   65:   
   66:      public IEnumerable<string> Items {
   67:          get {
   68:              foreach (string item in this.items) {
   69:                  yield return item;
   70:              }
   71:          }
   72:      }
   73:   
   74:      public int ItemCount {
   75:          get {
   76:              return this.items.Count;
   77:          }
   78:      }
   79:   
   80:      public void AddItem(string item) {
   81:          if (!this.items.Contains(item)) {
   82:              this.items.Add(item);
   83:          }
   84:      }
   85:   
   86:      public void RemoveItem(string item) {
   87:          this.items.Remove(item);
   88:      }
   89:   
   90:      public bool HasItem(string item) {
   91:          return this.items.Contains(item);
   92:      }
   93:  }

And finally, let's look at Vector2D - definately the simpliest of all of the items.

    1:  public struct Vector2D {
    2:      public int X;
    3:      public int Y;
    4:   
    5:      public Vector2D(int x, int y) {
    6:          this.X = x;
    7:          this.Y = y;
    8:      }
    9:  }

The classes are all pretty self-explanatory (at least I think so =)), so I'm not going to go into great detail. Just remember that when we are storing the items and the exits, we will be using the IDs. 

That's it for this post. In the next post I'll be showing you how to read the XML map file and create your map dynamically at run-time.