I've recently attempt for the first time to implement a map generation system and while it does work, it doesn't work exactly the same I want but I'm not really sure on what changes I can make it to make it generate the type of map I want.
This is my map generation code
///<summary>Generates a Room at a Start Position</summary>
void GenerateMap(Vector2 roomStartPos)
{
Vector2 startPos = roomStartPos;
int roomTileNum = random.Next(minRoomTiles, maxRoomTiles);
for (int i = 0; i < roomTileNum; i++)
{
if (CheckTilePosValid(startPos)) //Checks if a position is taken by a tile already
{
Tile newTile = CreateTile(startPos); //Creates a Tile at a passed in Position
startPos = GetRandomEdgeDirection(newTile.SpritePosition); //Gets a Random position next to the passed in Position
}
else
{
startPos = GetRandomEdgeDirection(startPos);
i--;
}
}
}
It's likely a really bad way of generating a map. It basically gets a random number of tiles to generate, it will then place a Tile at the start position and select a new position in a random direction (Up, Down, Left, Right). It'll then place a new tile at that new position specified, it will then simply loop through that till it's placed every tile. The "CheckTilePosValid()" function is used to determine if the random position that is gathered has already been taken by a tile. If the position isn't taken then it will create the tile and continue through the loop, while if the position is taken then it will get a new position and basically keeping doing that till it gets a position that hasn't been taken already.
This generates maps like this:
The issue is mainly with the first image since as you can see, the map is very stretched out which isn't what I want. I'd more want a map similar to the second image, tiles much closer together and not so stretched out but is still random enough.
I'm just not sure how exactly I can achieve this so I was wondering if anyone would be able to help me? Would I need to completely rework my generation system or can I just make simple changes to fix this? I'd very much appreciate the help :)