I want to generate a complex of connected rooms. Every room can have 1-4 rooms next to it.
My algorythm generates this complex of connected rooms.
public class Room
{
private Room top = null; //Connected rooms
private Room right = null;
private Room bottom = null;
private Room left = null;
private Vector2 roomOffset; //Position of the room
}
Before creating a new room I want to check if there is already a room at the position of the new room. Is there an easier way than going through all rooms and checking if the position matches the position of the new room?
I know you are probably set on your solution and for very good reasons, but I'll just leave here how I'd do this:
Supposing rooms are not only an empty space that provide a maze (can contain enemies, treasures, etc.) I'd create a Room
array rooms[,]
(otherwise bool[,]
would suffice).
I'd have an application level constant (or user option) with the maximum allowed maze dimensions and I'd create my array accordingly: rooms[MAXDIMENSION, MAXDIMENSION]
.
Then my algorithm would just set rooms in my array by assigning room instances. First dimension would represent north / south (rows) traversals and second dimension would represent east / west (columns) traversals.
An example:
rooms = new [3,2] { { new Room(), new Room() }, {null, new Room()} , {new Room(), new Room() } };
Neigbooring null
cells or array range bounds would mean rooms without path in that direction. In the example, if the starting node were [0,0]
the possible paths would be east, west / south, north / south, north / west, east.
Note that the possible traversals from any given room wouldn't be information included in the Room
instance as it is no longer needed; you can dynamically generate that info when rendering the maze just checking rooms[x, y]
neighbors.
An interesting thing about this approach is that it is extensible to maze leves too. room[,][MAXLEVELS]
where the last dimension would represent the amount of levels. They'd all share the same basic grid and dynamically checking up and down traversals would be just as easy.
Just an idea...