Home > C++, Programming > Loading Map/Game Data from XML (Part 2)

Loading Map/Game Data from XML (Part 2)

April 22, 2008

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.

Code:

public class Map {
   private Vector2D startingPosition;
   private Dictionary<Vector2D, Area> areas;

   public Map() {
       this.areas = new Dictionary<Vector2D, Area>();
   }

   public Area this[Vector2D position] {
        get {
            return this.areas[position];
        }
    }

    public Vector2D StartingPosition {
        get {
            return this.startingPosition;
        }
        set {
            this.startingPosition = value;
        }
    }

    public void AddArea(Area area) {
        if (area == null) throw new ArgumentNullException("area");
        this.areas.Add(area.Position, area);
    }

    public bool RemoveArea(Vector2D position) {
        return this.areas.Remove(position);
    }
}

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.

Code:

public class Area {
    private Vector2D position;
    private string description;
    private string title;
    private List<string> exits;
    private List<string> items;

    public Area(Vector2D position) {
        this.position = position;
        this.exits = new List<string>();
        this.items = new List<string>();
    }

    public Vector2D Position {
        get {
            return this.position;
        }
    }

    public string Description {
        get {
            return this.description;
        }
        set {
            this.description = value;
        }
    }

    public string Title {
        get {
            return this.title;
        }
        set {
            this.title = value;
        }
    }

    public IEnumerable<string> Exits {
        get {
            foreach (string exit in this.exits) {
                yield return exit;
            }
        }
    }

    public int ExitCount {
        get {
            return this.exits.Count;
        }
    }

    public bool HasExit(string direction) {
        return this.exits.Contains(direction);
    }

    public void AddExit(string direction) {
        if (!this.exits.Contains(direction)) {
            this.exits.Add(direction);
        }
    }

    public void RemoveExit(string direction) {
        this.exits.Remove(direction);
    }

    public IEnumerable<string> Items {
        get {
            foreach (string item in this.items) {
                yield return item;
            }
        }
    }

    public int ItemCount {
        get {
            return this.items.Count;
        }
    }

    public void AddItem(string item) {
        if (!this.items.Contains(item)) {
            this.items.Add(item);
        }
    }

    public void RemoveItem(string item) {
        this.items.Remove(item);
    }

    public bool HasItem(string item) {
        return this.items.Contains(item);
    }
}

And finally, let’s look at Vector2D – definately the simpliest of all of the items.

Code:

public struct Vector2D {
    public int X;
    public int Y;

    public Vector2D(int x, int y) {
        this.X = x;
        this.Y = y;
    }
}

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.

-David

Advertisements
Categories: C++, Programming Tags:
%d bloggers like this: