Suggestions on how best to design this, please.

1
2
3
4
5
6
7
8
9
10
11
12
13
class CompositeLocationBase : virtual public Location {
    // All functions pure virtual.
};

template <int N>
class CompositeLocation : public CompositeLocationBase {
    std::vector<std::list<Location*>> locations;
    // ....
};

using Tower = CompositeLocation<0>;
using Dungeon = CompositeLocation<1>;
using Castle = CompositeLocation<2>;


CompositeLocation<N> has a std::vector<std::list<Location*>> data member for all the Location* objects within it, and the index of the vector corresponds to the floor number (0 being ground level), and the std::list<Location*>> is then the list of all Location*'s on that floor. The problem I have is that of the various types of CompositeLocations, Tower only has above-ground floors, Dungeon only has underground floors, and Castle has both underground and above-ground floors. So how to best enumerate these floor levels?

I've currently implemented the class with only Tower in mind, which has obvious floors numbers 0,1,2,... Now I'm thinking about Dungeon and Castle. For the Dungeon class, I can simply use the floors numbers 0,1,2,... still, (the nth floor being the floor n below the ground level). But now what about Castle which has both underground and above-ground floors? Should I change std::vector<std::list<Location*>> to std::map<std::list<Location*>> just to accommodate Castle (and keep 0 as ground level, and now negative indices correspond to underground floors)? Or have two separate std::vector<std::list<Location*>> data members, one for floors above ground level and the other for floors below ground level?

CompositeLocation<N> is derived from CompositeLocationBase, and functions being used have CompositeLocationBase* passed as argument instead of any particular CompositeLocation<N>*, and thus CompositeLocationBase has all of its functions pure virtual.
Last edited on
You can add undergroundFloors member. Then locations[x] will actually be floor x - undergroundFloors
Topic archived. No new replies allowed.