Well i finally got it to work! Here is a screenshot showing new icons. Each room knows of its neighbor(s) and with how many rooms it is connected to.

The next step is to generate a treasure room, boss room and after the boss room a shop.

I already have an idea how to do the boss and the shop. If you notice the script, i already did a “Manhattan distance” calculation for each cell. I will use this to change the farthest away cell (from the start) to a type 3(boss room type) and after that the shop cell would be easy.(mainly looping through the dungeon searching for the boss cell and instantiate at a random position next to the boss cell)

Anyway here is the screenshot!

And here is the code!

```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MapGenerator : MonoBehaviour
{
public int width = 15; //total width of the grid
public int length = 15; //total length of the grid
public RoomID[,] mapArray; //2d array of classes that holds everything
//PRIVATE VARIABLES
//level variables
public int level; //our current level we are on, this to determine difficulty and level size
public int rooms; //this variable will handle how many rooms have been built
public int iterations; //how many rooms will be instantiated? This will randomize itself at the beginning
//current positions variables
private int curRoom; //current room int which we use to get values of the Vector2 list below
private int curRoomPosX; //these curRoomPosX and Z variables will keep track on which position we currently are
private int curRoomPosZ; //this to determine our next position whith the newRoomPosX and Z variable
private Vector2[] roomPositions; //list of Vector2 positions to determine where the next RANDOM room can be placed
//next position variables
private int newDirection; //this variable will keep track which direction we want the next room to be set
private int newRoomPosX; //this variable will set the next rooms position on the X axis
private int newRoomPosZ; //this variable will set the next rooms position on the Z axis
private int newRoomAdjacents; //how many adjacent room has the new room?
private bool complete; //level generating is completed
public float miniMapX; //mini map width
public float miniMapZ; //mini map height
public Texture2D startingRoom;
public Texture2D basicRoom;
public Texture2D bossRoom;
public Texture2D empty;
public Texture2D north;
public Texture2D south;
public Texture2D west;
public Texture2D east;
public Texture2D ne;
public Texture2D nw;
public Texture2D ns;
public Texture2D sw;
public Texture2D se;
public Texture2D we;
public Texture2D nJunct;
public Texture2D sJunct;
public Texture2D wJunct;
public Texture2D eJunct;
public Texture2D cross;
private float iconX; //icon width of each room
private float iconZ; //icon height of each room
void Start()
{
mapArray = new RoomID[width, length]; //Assign the width and length value to the 2d Array grid
rooms = 0; //just to be sure
iterations = Random.Range(5 + (level * 2), 7 + (level * 3)); //randomize iterations
roomPositions = new Vector2[iterations+1]; //set the roomPositions Vector2 array higher then iterations by 1
miniMapX = Screen.height; //for now set the mini map to cover the whole screen
miniMapZ = Screen.height;
iconX = (miniMapX/width);
iconZ = (miniMapZ/length);
//A 2 for loop, generating the grid and assigning basic values
for(int x = 0;x < width; x++)
{
for(int z = 0; z < length; z++)
{
mapArray[x,z] = new RoomID(); //create a RoomID instance in each cell on the grid
mapArray[x,z].xPos = x; //passing the x value to the RoomID x position
mapArray[x,z].zPos = z; //passing the z value to the RoomID z position
mapArray[x,z].type = 0; //each starting cell is an empty room, so we set this to 0
}
}
Generate(); //calling the generate method for actual generating
}
void Generate()
{
//while rooms is equal or less then iterations, we start building rooms
while(rooms <= iterations)
{
if(rooms == 0)
{
roomPositions[rooms] = new Vector2(width/2,length/2);
mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].type = 2;
mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].id = rooms;
rooms++;
}
else
{
curRoom = Random.Range(0, rooms); //grab random room in rooms so we know where to build next
curRoomPosX = (int)roomPositions[curRoom].x; //set curRoomPosX to roomPositionsX value which we picked earlier
curRoomPosZ = (int)roomPositions[curRoom].y; //set curRoomPosZ to roomPositionsY value which we picked earlier
newDirection = Random.Range(1,5); //Randomly choose next direction each time we build a new room
if(newDirection == 1) //NORTH
{
newRoomPosX = curRoomPosX;
newRoomPosZ = curRoomPosZ + 1;
}
else if(newDirection == 2) //EAST
{
newRoomPosX = curRoomPosX + 1;
newRoomPosZ = curRoomPosZ;
}
else if(newDirection == 3) //SOUTH
{
newRoomPosX = curRoomPosX;
newRoomPosZ = curRoomPosZ - 1;
}
else if(newDirection == 4) //WEST
{
newRoomPosX = curRoomPosX - 1;
newRoomPosZ = curRoomPosZ;
}
else if(newDirection == 5) //OUT OF RANGE
{
Debug.Log("Out of range!");
}
//we just have to make sure that the new position is on the grid
if(newRoomPosX < width && newRoomPosX > 0 && newRoomPosZ < length && newRoomPosZ > 0)
{
if(mapArray[newRoomPosX, newRoomPosZ].type == 0) //roomtype = 0, so space is available
{
newRoomAdjacents = Adjacents(newRoomPosX,newRoomPosZ);
if(newRoomAdjacents != 1)
{
//More then 1 adjacent rooms, which we try to avoid so we do not have 1 big square level
}
else
{
roomPositions[rooms] = new Vector2(newRoomPosX, newRoomPosZ);
mapArray[newRoomPosX,newRoomPosZ].type = 1;
mapArray[newRoomPosX,newRoomPosZ].id = rooms; //set the RoomID.id equal to rooms
mapArray[newRoomPosX,newRoomPosZ].distance = (Mathf.Abs(newRoomPosX - (int)roomPositions[0].x) +
Mathf.Abs(newRoomPosZ - (int)roomPositions[0].y));
rooms++;
}
}
else
{
//Debug.Log("Cell already taken, searching for new space");
}
}
else
{
//Debug.Log("Out of the grid, searching for new space.");
}
}
}
if(rooms >= iterations)
{
complete = true;
int i = 0;
int j = 0;
while(i < width)
{
j = 0;
while(j < length)
{
if(mapArray[i,j].type != 0)
{
North(i,j);
South(i,j);
East(i,j);
West(i,j);
if(mapArray[i,j].connections == 1) //DEAD END
{
if(mapArray[i,j].nConnected) //NORTH
{
mapArray[i,j].mini = north;
}
else if(mapArray[i,j].sConnected) //SOUTH
{
mapArray[i,j].mini = south;
}
else if(mapArray[i,j].wConnected) //WEST
{
mapArray[i,j].mini = west;
}
else if(mapArray[i,j].eConnected) //EAST
{
mapArray[i,j].mini = east;
}
}
else if(mapArray[i,j].connections == 2)
{
//STRAIGHT
if(mapArray[i,j].nConnected && mapArray[i,j].sConnected) //NORTH SOUTH
{
mapArray[i,j].mini = ns;
}
else if(mapArray[i,j].wConnected && mapArray[i,j].eConnected) //WEST EAST
{
mapArray[i,j].mini = we;
}
//CORNERS
else if(mapArray[i,j].sConnected && mapArray[i,j].eConnected) //SOUTH EAST
{
mapArray[i,j].mini = se;
}
else if(mapArray[i,j].sConnected && mapArray[i,j].wConnected) //SOUTH WEST
{
mapArray[i,j].mini = sw;
}
else if(mapArray[i,j].nConnected && mapArray[i,j].wConnected) //NORTH WEST
{
mapArray[i,j].mini = nw;
}
else if(mapArray[i,j].nConnected && mapArray[i,j].eConnected) //NORTH EAST
{
mapArray[i,j].mini = ne;
}
}
else if(mapArray[i,j].connections == 3) //T JUNCTIONS
{
if(mapArray[i,j].nConnected && mapArray[i,j].eConnected && mapArray[i,j].wConnected) //NORTH T JUNCTION
{
mapArray[i,j].mini = nJunct;
}
else if(mapArray[i,j].nConnected && mapArray[i,j].eConnected && mapArray[i,j].sConnected) //EAST T JUNCTION
{
mapArray[i,j].mini = eJunct;
}
else if(mapArray[i,j].sConnected && mapArray[i,j].wConnected && mapArray[i,j].eConnected) //SOUTH T JUNCTION
{
mapArray[i,j].mini = sJunct;
}
else if(mapArray[i,j].sConnected && mapArray[i,j].wConnected && mapArray[i,j].nConnected) //WEST T JUNCTION
{
mapArray[i,j].mini = wJunct;
}
}
else if(mapArray[i,j].connections == 4) //CROSS
{
mapArray[i,j].mini = cross;
}
//if(mapArray[i,j].distance >= iterations/2 && mapArray[i,j].connections == 1)
//{
//}
}
j++;
}
i++;
}
}
}
bool North(int x, int z)
{
if(z == 0) return false;
if(mapArray[x,z-1].type != 0)
{
mapArray[x,z].nConnected = true;
mapArray[x,z].connections ++;
return true;
}
return false;
}
bool South(int x, int z)
{
if(z == length-1) return false;
if(mapArray[x,z+1].type != 0)
{
mapArray[x,z].sConnected = true;
mapArray[x,z].connections ++;
return true;
}
return false;
}
bool East(int x, int z)
{
if(x == width-1) return false;
if(mapArray[x+1,z].type != 0)
{
mapArray[x,z].eConnected = true;
mapArray[x,z].connections ++;
return true;
}
return false;
}
bool West(int x, int z)
{
if(x == 0) return false;
if(mapArray[x-1,z].type != 0)
{
mapArray[x,z].wConnected = true;
mapArray[x,z].connections ++;
return true;
}
return false;
}
//this is used to calculate how many adjacent rooms we have
//it does a 2 for loop where it looks around the current room position from -1 to +1
//in UP DOWN LEFT and RIGHT direction, then if the room type is NOT 0, we add 1 to the newRoomAdjacents
int Adjacents(int x, int z)
{
int curX = x;
int curZ = z;
newRoomAdjacents = 0;
for(int _x = -1; _x < x + 2; _x++)
{
for(int _z = -1; _z < z + 2; _z++)
{
if(_x > 0 && _x < width && _z > 0 && _z < length)
{
if(_x == x && _z == z)
{
continue;
}
if(_x > x && _z == z && mapArray[_x,_z].type != 0)
{
newRoomAdjacents ++;
}
if(_x < x && _z == z && mapArray[_x,_z].type != 0)
{
newRoomAdjacents ++;
}
if(_x == x && _z > z && mapArray[_x,_z].type != 0)
{
newRoomAdjacents ++;
}
if(_x == x && _z < z && mapArray[_x,_z].type != 0)
{
newRoomAdjacents ++;
}
}
}
}
return newRoomAdjacents;
}
void OnGUI()
{
if(complete)
{
GUI.BeginGroup(new Rect(miniMapX/2,0,miniMapX,miniMapZ));
for(int x = 0; x < width; x++)
{
for(int z = 0; z < length; z++)
{
if(mapArray[x,z].type == 2)
{
GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX,iconZ), mapArray[x,z].mini);
//GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), startingRoom);
}
else if(mapArray[x,z].type == 1)
{
GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX,iconZ), mapArray[x,z].mini);
//GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), basicRoom);
//GUI.Label(new Rect(x*iconX,z*iconZ,iconX-1,iconZ-1),"E: " + mapArray[x,z].eConnected + " / W:" +
// mapArray[x,z].wConnected + " / S: " + mapArray[x,z].sConnected +
// " / N: " + mapArray[x,z].nConnected + " / ID: " + mapArray[x,z].type);
}
else if(mapArray[x,z].type == 3)
{
//GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), bossRoom);
}
else if(mapArray[x,z].type == 0)
{
GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX,iconZ), empty);
}
}
}
GUI.EndGroup();
}
}
}
```

And the RoomID class:

```
using UnityEngine;
using System.Collections;
public class RoomID
{
public int type; //0 = empty
//1 = basic room(which may contain enemies or not)
//2 = starting room
//3 = boss room
//4 = treasure room
//5 = shop
public int id; //this rooms unique ID
public int intensity; //for a basic, miniboss or boss room, how difficult it is(more enemies, stronger enemies)
public int connections; //with how many rooms is this room connected
public bool nConnected; //north connected
public bool sConnected; //south connected
public bool eConnected; //east connected
public bool wConnected; //west connected
public int xPos; //its X axis position on the grid
public int zPos; //its Z axis position on the grid
public int distance; //this rooms distance from the start
public Texture2D mini;
}
```

Any tips or suggestions are always welcome!

Cheers,

Darryl