Hi! I have a game that randomly generates houses to walk through and explore.
The only problem is that I can't figure out a way to generate/spawn doors or openings in-between each of the rooms in a way that looks neat and allows the player to reach the other side of the house.
https://drive.google.com/open?id=0B-FHV ... mc5QWtvM2s is the link to my CAPX.
If anyone could help me figure this out, that would be great.
P.S. The CAPX also uses the Canvas Plugin, which you can search for or get here.
Develop games in your browser. Powerful, performant & highly capable.
I do not have much time as I have a business trip tomorrow morning, but I glanced at your capx and I can tell you I had the same problem. And mine was a bit bigger because I have a 64 by 64 tile building floor with ~40 randomly generated rooms with different shapes.
What I did was the following, please note I forgotten some details and I feel an immense sense of dread whenever I need to look into my map generation logic (at 350 event blocks it is a bit big and unpleasant to look at, also it takes 40-50 seconds to generate a level on my LG spirit LTE/4G droid phone (Desktop PC with NW.js takes 3-4 seconds for the same). I know the solution is not fool proof but the margin of not accessible rooms I get is acceptable (maybe 1 in a 100 if not less). Still it should give you some ideas.
One thing I did is that in my array that stores all map data in z-indexes (from 0 to 17 I think, stuff like fog of war, walls, bitwise for walls etc.). One of the things stores was room ID.
1) Generate rooms/walls first. I used prefabs for this but it doesn't matter so long as you have an array which tells you which tile belongs to which room. I have a separate array used for rooms which uses the y-axis to store data such as, what is the parent room, is the room connected to the starting room (elevator in my case). So every time I placed a new room, it would also get pushed as an X element to the array.
2) For each room I checked the whole map array for all tiles belonging to that room that were walls (but not corner walls). Every time a tile like that was found, it was added to yet another array. This array stored the x coordinate at y=1 and the y coordinate of the tile at y=2. So what was at y=0 you ask? A random number from 0 to 100. Why? Because after I finished checking the whole map for that particular room and had all its wall, I would sort the array. Since it sorts by values at (x,0) I would have wall arrays in a random order.
3) I would go through the now randomly sorted wall tiles for the room and start checking the following. Does it neighbour a different room? If yes, is that room already a child or parent of this room (a parent here is a room that is "closer" to the start, a child is a room "closer" to the end, but not by actual distance but by the number of rooms needed to cross)? If yes, we skip to the next tile in the array, if not we can create a door at that tile. The room to which we just connected is now set as the parent of the current room. If the parent happens to be connected to the start, we also mark the current room as connected in the room array.
4) We did 2 and 3 for each room. So next we check the room array if all rooms are connected to the start. When we find a room that is not connected, we do 2 and 3 for it again. The logic should again create a door to a room that was previously not directly connected with the current one. We do that for every room that is not connected until all rooms are connected or there are no more connections that can be created for the unconnected rooms(I do this via a while loop).
Actually I stretched the truth a bit, I looked at my project and I never bothered marking rooms as being out of possibilities to connect. I just hard-capped the while loop that goes through each room>generates a door>creates the connection tree/map, to run two times an apparently it was enough to not get unconnected rooms >_>. I do not know why, but there's probably some mathematical theory or theorem that explains this. I kind of like it because the room connections are quite random, sometimes there are dead end rooms, there are rooms with 2 doors, some have 3 and in more rare cases 4.