Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to instantiate prefabs between 2 objects like a path

Discussion in 'Scripting' started by darryldonohoe, Aug 17, 2014.

  1. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Hi,
    I am trying to make a level generator using prefabs. I want to generate a path of prefabs between 2 objects.

    However! These instantiated prefabs have fixed sizes, and need to connect with each other. Eventually the purpose is that the prefabs will be instantiated in a grid-like manner. And each time the path would be different.
    A L shape towards the end position. Or just a "curvy" path towards the end position.
    Of course the instantiated prefabs will be randomized each time as well. (but this is easy)

    Attached an example:



    I am not asking for an script, merely a push in the right direction. I honestly don't know where to start.

    This is the beginning of the "test" script what i came up with.

    Code (JavaScript):
    1.  
    2. var startNode : Transform;
    3. var endNode : Transform;
    4. var nodes : Transform;
    5.  
    6. var startPosition : Vector3;
    7. var endPosition : Vector3;
    8.  
    9. private var distance : float;
    10. private var center : Vector3;
    11.  
    12.  
    13. function Start()
    14. {
    15.     startPosition = Vector3(0, 0, 0);
    16.     endPosition = Vector3(startPosition.x + 5 * 5, 0,startPosition.z + 5 * 6);
    17.    
    18.     Instantiate(startNode, startPosition, Quaternion.identity);
    19.     Instantiate(endNode, endPosition, Quaternion.identity);
    20.    
    21.     GeneratePath();
    22. }
    23.  
    24.  
    25. function GeneratePath()
    26. {
    27.    
    28. }

    So the startNode and endNode will be instantiated(eventually at a random location). And between them the generated "path". Of course the code that i have posted isn't much. But like i said, i just don't know where to start.

    I really hope someone can help me!

    Thanks in advance and cheers,
    Darryl
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Use two FOR loops with an y and x incrementing. Add x and y to the spawn vector x and y. You can use certain conditions to determine if a tile is spawned on x or y, like a 2d array with bools or something like that.
     
  3. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Hey Fluzing, so i looked into a 2d array. Never heard of them before, and never used them before.
    So i have to say, they are pretty hard to understand for me. However. I have a better start right now.

    This is how the script looks right now. (still no actual path, but a working grid array with an start and a end cell)

    Code (JavaScript):
    1.  
    2. public var gridX : int = 7;
    3. public var gridZ : int = 7;
    4.  
    5. public var xSpacing : int = 5;
    6. public var zSpacing : int = 5;
    7. public var cellPrefab : GameObject;
    8. public var startingCell : GameObject;
    9. public var endCell : GameObject;
    10.  
    11. public var delay : float = 0.25;
    12.  
    13. public var gridArray : RoomCell[,];
    14.  
    15. private var startEndPositions : int = 6;
    16. public var startPosition : int[];
    17. public var endPosition : int[];
    18.  
    19.  
    20. public class RoomCell
    21. {
    22.     public var room : GameObject;
    23.     public var zone : String;
    24. }
    25.  
    26.  
    27. function Start()
    28. {
    29.     gridArray = new RoomCell[gridX,gridZ];
    30.    
    31.     startPosition = new int[2];
    32.     endPosition = new int[2];
    33.    
    34.     StartEndPosition();
    35. }
    36.    
    37.    
    38. function StartEndPosition()
    39. {
    40.     startEndPositions = 6;
    41.    
    42.     switch(startEndPositions)
    43.     {
    44.         case 6:                    //Start position bottom row, end position top row
    45.             print ("6 : start bottom - end top");
    46.             startPosition[0] = Random.Range(0, gridX);
    47.             startPosition[1] = 0;
    48.             endPosition[0] = Random.Range(0, gridX);
    49.             endPosition[1] = gridZ-1;
    50.             break;
    51.         case 5:                    //Start position bottom row, end position top row
    52.             print("5 : start bottom - end top");
    53.             break;
    54.         case 4:                    //Start position top row, end position bottom row
    55.             print("4 : start top - end bottom");
    56.             break;
    57.         case 3:                    //Start position left collumn, end position top side
    58.             print("3 : start left - end top");
    59.             break;
    60.         case 2:                    //Start position bottom row, end position right collumn
    61.             print ("2 : start bottom - end right");
    62.             break;
    63.         case 1:                    //Start position left collumn, end position right collumn
    64.             print ("1 : start left - end right");
    65.             break;
    66.         default:                //Start position bottom row, end position top row
    67.             print ("default : start bottom - end top");
    68.             break;
    69.     }
    70.    
    71.     GenerateGrid();
    72. }
    73.  
    74.  
    75. function GenerateGrid()
    76. {
    77.     for(var x = 0; x < gridArray.GetLength(0); x++)
    78.     {
    79.         for(var z = 0; z < gridArray.GetLength(1); z++)
    80.         {
    81.             yield WaitForSeconds(delay);
    82.            
    83.             GenerateCell(x, z);
    84.         }
    85.     }
    86. }
    87.  
    88.  
    89. function GenerateCell(x : int, z : int)
    90. {
    91.     var cell : RoomCell = RoomCell();
    92.    
    93.     cellLocation = Vector3(x * xSpacing, 0, z * zSpacing);
    94.    
    95.     if(x == startPosition[0] && z == startPosition[1])
    96.     {
    97.         newCell = Instantiate(startingCell, cellLocation, Quaternion.identity);
    98.     }
    99.     else if(x == endPosition[0] && z == endPosition[1])
    100.     {
    101.         newCell = Instantiate(endCell, cellLocation, Quaternion.identity);
    102.         print("ENDCELL");
    103.     }
    104.     else
    105.     {
    106.         newCell = Instantiate(cellPrefab, cellLocation, Quaternion.identity);
    107.     }
    108.    
    109.     newCell.name = "Cell: " + x + ", " + z;
    110.     newCell.transform.parent = transform;
    111. }
    At first the end cell was not working, but got it to work luckily! Also i have to put more locations in the switch function, but i had to test it out first.

    Here is a screenshot of the actual grid.

    grid.jpg


    Now i have to find a way to instantiate the path form start to end position. And make it so, that the path cells can not overlap each other. So they have to check if the cell in the 2d array is taken or not.

    Anyway thanks!

    Cheers, Darryl
     
  4. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Well, thought of posting some progress over here, i recently switched to C#.

    Deleted the old script, redid the whole thing. Still finding it quite hard. Right now i have two 2D arrays. One int and one GameObject.

    The int is for initializing the level data: 0 = empty, 1 = occupied, 2 = startingroom etc.(right now just 3 of them)

    The GameObject will be used for the actual rooms. I have an function working which checks adjacent cells.
    But doesn't do anything at the moment, just for testing.

    I am trying to figure out to choose a random direction to instantiate the next cell. Now this is giving me a hard time.
    The idea is, to call the adjacent cell function and check if there is an open space, if there is. Then set it to 1, and instantiate cell on that place. Anyway here is the script at the moment:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelGenerator : MonoBehaviour
    5. {
    6.     public int width = 15;            //Width of the level grid
    7.     public int length = 15;            //Length of the level grid
    8.  
    9.     public int iterations = 5;        //How many iterations are instantiated
    10.  
    11.     public int[,] levelArray;        //A 2D array of ints, managing level data
    12.  
    13.     public float delay = 0.3f;        //Float handling a delay between each instantiate of GameObject
    14.  
    15.     public GameObject cellPrefab;  
    16.     private GameObject[,] cells;    //A 2D array of GameObjects representing cells
    17.  
    18.     private Direction direction;    //Controlling the enum: Direction
    19.     private const int count = 4;    //Int to declare the amount of choices in the enum: Direction
    20.  
    21.     public GameObject lastRoom;
    22.  
    23.     public enum Direction            //Enum for deciding which direction to take
    24.     {
    25.         up,
    26.         down,
    27.         left,
    28.         right
    29.     }
    30.  
    31.     void Awake()
    32.     {
    33.         levelArray = new int[width,length];
    34.         cells = new GameObject[width,length];
    35.  
    36.         direction = (Direction)Random.Range(0,count);
    37.        
    38.         print("Direction: " + direction);
    39.        
    40.         for(int x = 0; x < width; x++)
    41.         {
    42.             for(int z = 0; z < length; z++)
    43.             {
    44.                 if(x == Random.Range (0,width) && z == Random.Range(0,length))
    45.                 {
    46.                     levelArray[x,z] = 2;
    47.                 }
    48.                 else
    49.                 {
    50.                     levelArray[x,z] = 0;
    51.                 }
    52.             }
    53.         }
    54.  
    55.         //StartCoroutine(Generate());
    56.     }
    57.  
    58.     //public IEnumerator Generate()
    59.     //{
    60.  
    61.     //    while(iterations > 0)
    62.     //    {
    63.     //        yield return new WaitForSeconds(delay);
    64.     //
    65.     //        if(lastRoom == null)
    66.     //        {
    67.     //            print("IEnumerator");
    68.     //            CreateCell(Random.Range(0,width),Random.Range(0,length));
    69.     //        }
    70.     //        else
    71.     //        {
    72.     //
    73.     //            iterations -= 1;
    74.     //        }
    75.     //    }
    76.     //}
    77.  
    78.     void CreateCell(int x, int z)
    79.     {
    80.         Vector3 cellPosition = new Vector3(x * 5, 0, z * 5);
    81.  
    82.         if(levelArray[x,z] == 2)
    83.         {
    84.             cells[x,z] = (GameObject) Instantiate(cellPrefab, cellPosition,Quaternion.identity);
    85.             cells[x,z].name = ("Starting Cell: " + x + ", " + z);
    86.             cells[x,z].transform.parent = transform;
    87.         }
    88.         else if(levelArray[x,z] == 1)
    89.         {
    90.             cells[x,z] = (GameObject) Instantiate(cellPrefab, cellPosition,Quaternion.identity);
    91.             cells[x,z].name = ("Cell: " + x + ", " + z);
    92.             cells[x,z].transform.parent = transform;
    93.         }
    94.  
    95.         lastRoom = cells[x,z];
    96.     }
    97.  
    98.     void AdjacentCell(int _x, int _z)
    99.     {
    100.         for(int x = _x - 1; x < _x + 2; x++)
    101.         {
    102.             for(int z = _z -1; x < _z + 2; z++)
    103.             {
    104.                 if(x == _x && z == _z)
    105.                 {
    106.                     print("Our current cell");
    107.                     continue;
    108.                 }
    109.                 else if(x <= 0 || x >= width || z <= 0 || z >= length)
    110.                 {
    111.                     print("Out of array index");
    112.                     continue;
    113.                 }
    114.                 else if(x > _x && z == _z)
    115.                 {
    116.                    
    117.                 }
    118.                 else if(x < _x && z == _z)
    119.                 {
    120.  
    121.                 }
    122.                 else if(x == _x && z > _z)
    123.                 {
    124.  
    125.                 }
    126.                 else if(x == _x && z < _z)
    127.                 {
    128.  
    129.                 }
    130.             }
    131.         }
    132.         NextDirection(_x, _z);
    133.     }
    134.    
    135.     void NextDirection(int x, int z)
    136.     {
    137.  
    138.         switch(direction)
    139.         {
    140.             case Direction.up:
    141.             print("UP");
    142.             break;
    143.  
    144.             case Direction.down:
    145.             print("DOWN");
    146.             break;
    147.  
    148.             case Direction.right:
    149.             print("RIGHT");
    150.             break;
    151.  
    152.             case Direction.left:
    153.             print("LEFT");
    154.             break;
    155.  
    156.             default:
    157.             print("NOTHING");
    158.             break;
    159.         }
    160.  
    161.     }
    162. }

    If you guys have any ideas or tips, it will be extremely helpful!

    Cheers,
    Darryl
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    You could write an ant algorithm, basically an "ant" (position in code on the 2D grid, starts at your starting point) crawls along your grid, in the general direction of the endPoint, and chooses random left or right turns (using an error calculation to determine if it can turn the way it wants, e.g if the angle would be too far from the endPoint). Then by the time the ant reaches the endPoint the algorithm is finished. And your path is created! Hope this helps. :)

    EDIT: Also just a suggestion, but how you intialise your 2D arrays, and you check while the for loops are running for random positions. A better idea would be to first initialize the 2D array all zeroes, and then choose two sets of random coordinates when the for loops finish, and place your start and end points, you'll be guaranteed to have a start and end, and only one of each. Hope this helps! :)
     
  6. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    I'm not sure if this would help but why dont you just try adding a empty GameObject on each cell that has a reference to the gameobject you want to spawn and just make it spawn randomly when u want throught a timer or such? that or u can always get a random number based on the cell position where the player is and the end and get the cell at position (randx, randy)

    Ex: player at position 2, end at position 5
    cell[Random Range 2 to 5,Random Range 2 to 5].SpawnObjectMethodNameHere=herpderp
     
  7. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    http://tinysubversions.com/spelunkyGen/
    I am not sure if this is what you want, but this shows how Spelunky's procedural level generation works. (Basically, making sure there's a path from start to finish, and then filling the rest)
     
  8. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    There is always a path already made, what he is trying to do is make it spawn a certain object on a "random" cell between the player and the end
     
  9. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I get that, that guide thing also talks about filling in the empty spaces, which include things like "A ladder between the start and the end" or so I believe.
    I did not read it yet, I just remembered that article.
     
  10. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Well i finally have a working level generator!!! I took your advise(TwixEmma), in the Start() method i "generated" the grid.
    Setting each type to 0. And later on when the actual level is being generated, switch accordingly.

    I also looked at the link you(MrPriest) provided and it was a good read!

    However i switched(again) from two 2D arrays to 1 2D array this time a 2D array of Classes where i will store all sorts of data.

    Here is a screenshot how a level looks like once "completed"(it doesn't have an end position yet, but thats easy to add)

    levelGeneratorScreenshot.jpg

    Green is obvious the starting position. This is just a simple GUI fit to screen width and length screenshot.

    However! I have an Adjacent room check in my script, which works. But i want to pass data to each room for example:

    how many rooms is this room connected?
    which directions is this room connected to? North, south, east, west?

    The Adjacent room check is working, so i thought this would be easy to implement but i was wrong. If i use the Adjacent room check to add value to the room connections, it gives my wrong values ranging from 1 to 6(which would never be possible).

    When i declare a room type on the given position and try to pass value on it then, it still gives me wrong values.

    Here is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class MapGenerator : MonoBehaviour
    6. {
    7.     public int width = 15;                //total width of the grid
    8.     public int length = 15;             //total length of the grid
    9.  
    10.     public RoomID[,] mapArray;            //2d array of classes that holds everything
    11.  
    12.     //PRIVATE VARIABLES
    13.  
    14.     //level variables
    15.     public int level;                    //our current level we are on, this to determine difficulty and level size
    16.     public int rooms;                    //this variable will handle how many rooms have been built
    17.     public int iterations;                //how many rooms will be instantiated? This will randomize itself at the beginning
    18.  
    19.     //current positions variables
    20.     private int curRoom;                //current room int which we use to get values of the Vector2 list below
    21.     private int curRoomPosX;            //these curRoomPosX and Z variables will keep track on which position we currently are
    22.     private int curRoomPosZ;            //this to determine our next position whith the newRoomPosX and Z variable
    23.     private Vector2[] roomPositions;    //list of Vector2 positions to determine where the next RANDOM room can be placed
    24.  
    25.     //next position variables
    26.     private int newDirection;            //this variable will keep track which direction we want the next room to be set
    27.     private int newRoomPosX;            //this variable will set the next rooms position on the X axis
    28.     private int newRoomPosZ;            //this variable will set the next rooms position on the Z axis
    29.  
    30.     private int newRoomAdjacents;        //how many adjacent room has the new room?
    31.  
    32.     private bool complete;                //level generating is completed
    33.  
    34.     public float miniMapX;                //mini map width
    35.     public float miniMapZ;                //mini map height
    36.  
    37.     public Texture2D startingRoom;
    38.     public Texture2D basicRoom;
    39.     public Texture2D empty;
    40.  
    41.     private float iconX;                //icon width of each room
    42.     private float iconZ;                //icon height of each room
    43.  
    44.  
    45.     void Start()
    46.     {
    47.         mapArray = new RoomID[width, length];        //Assign the width and length value to the 2d Array grid
    48.         rooms = 0;                                    //just to be sure
    49.         iterations = Random.Range(5 + (level * 2), 7 + (level * 3));    //randomize iterations
    50.         roomPositions = new Vector2[iterations+1];    //set the roomPositions Vector2 array higher then iterations by 1
    51.  
    52.         miniMapX = Screen.width;                    //for now set the mini map to cover the whole screen
    53.         miniMapZ = Screen.height;
    54.      
    55.         iconX = (miniMapX/width);
    56.         iconZ = (miniMapZ/length);
    57.  
    58.         //A 2 for loop, generating the grid and assigning basic values
    59.         for(int x = 0;x < width; x++)
    60.         {
    61.             for(int z = 0; z < length; z++)
    62.             {
    63.                 mapArray[x,z] = new RoomID();        //create a RoomID instance in each cell on the grid
    64.                 mapArray[x,z].xPos = x;                //passing the x value to the RoomID x position
    65.                 mapArray[x,z].zPos = z;                //passing the z value to the RoomID z position
    66.                 mapArray[x,z].type = 0;                //each starting cell is an empty room, so we set this to 0
    67.             }
    68.         }
    69.      
    70.         Generate();                                    //calling the generate method for actual generating
    71.     }
    72.  
    73.     void Generate()
    74.     {
    75.         //while rooms is equal or less then iterations, we start building rooms
    76.         while(rooms <= iterations)
    77.         {
    78.             if(rooms == 0)
    79.             {
    80.                 roomPositions[rooms] = new Vector2(width/2,length/2);
    81.  
    82.                 mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].type = 2;
    83.                 mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].id = rooms;
    84.  
    85.                 rooms++;
    86.             }
    87.             else
    88.             {
    89.                 curRoom = Random.Range(0, rooms);                //grab random room in rooms so we know where to build next
    90.  
    91.                 curRoomPosX = (int)roomPositions[curRoom].x;    //set curRoomPosX to roomPositionsX value which we picked earlier
    92.                 curRoomPosZ = (int)roomPositions[curRoom].y;    //set curRoomPosZ to roomPositionsY value which we picked earlier
    93.  
    94.                 newDirection = Random.Range(1,5);                //Randomly choose next direction each time we build a new room
    95.              
    96.                 if(newDirection == 1)        //NORTH
    97.                 {
    98.                     newRoomPosX = curRoomPosX;
    99.                     newRoomPosZ = curRoomPosZ + 1;
    100.                 }
    101.                 else if(newDirection == 2)    //EAST
    102.                 {
    103.                     newRoomPosX = curRoomPosX + 1;
    104.                     newRoomPosZ = curRoomPosZ;
    105.                 }
    106.                 else if(newDirection == 3)    //SOUTH
    107.                 {
    108.                     newRoomPosX = curRoomPosX;
    109.                     newRoomPosZ = curRoomPosZ - 1;
    110.                 }
    111.                 else if(newDirection == 4)    //WEST
    112.                 {
    113.                     newRoomPosX = curRoomPosX - 1;
    114.                     newRoomPosZ = curRoomPosZ;
    115.                 }
    116.                 else if(newDirection == 5)    //OUT OF RANGE
    117.                 {
    118.                     Debug.Log("Out of range!");
    119.                 }
    120.              
    121.                 //we just have to make sure that the new position is on the grid
    122.                 if(newRoomPosX < width && newRoomPosX > 0 && newRoomPosZ < length && newRoomPosZ > 0)
    123.                 {
    124.                     if(mapArray[newRoomPosX, newRoomPosZ].type == 0)    //roomtype = 0, so space is available
    125.                     {
    126.                         newRoomAdjacents = Adjacents(newRoomPosX,newRoomPosZ);
    127.  
    128.                         if(newRoomAdjacents != 1)
    129.                         {
    130.                             //More then 1 adjacent rooms, which we try to avoid so we do not have 1 big square level
    131.                         }
    132.                         else
    133.                         {
    134.                             roomPositions[rooms] = new Vector2(newRoomPosX, newRoomPosZ);
    135.                          
    136.                             mapArray[newRoomPosX,newRoomPosZ].type = 1;
    137.                             mapArray[newRoomPosX,newRoomPosZ].id = rooms;    //set the RoomID.id equal to rooms
    138.  
    139.                             //mapArray[newRoomPosX,newRoomPosZ].connections = newRoomAdjacents;    <---TRYING TO ADD CONNECTIONS
    140.  
    141.                             rooms++;
    142.                         }
    143.                     }
    144.                     else
    145.                     {
    146.                         //Debug.Log("Cell already taken, searching for new space");
    147.                     }
    148.                 }
    149.                 else
    150.                 {
    151.                     //Debug.Log("Out of the grid, searching for new space.");
    152.                 }
    153.             }
    154.         }
    155.  
    156.         if(rooms >= iterations)
    157.         {
    158.             complete = true;
    159.         }
    160.     }
    161.  
    162.     //this is used to calculate how many adjacent rooms we have
    163.     //it does a 2 for loop where it looks around the current room position from -1 to +1
    164.     //in UP DOWN LEFT and RIGHT direction, then if the room type is NOT 0, we add 1 to the newRoomAdjacents
    165.     int Adjacents(int x, int z)
    166.     {
    167.         newRoomAdjacents = 0;
    168.  
    169.         for(int _x = -1; _x < x + 2; _x++)
    170.         {
    171.             for(int _z = -1; _z < z + 2; _z++)
    172.             {
    173.                 if(_x > 0 && _x < width && _z > 0 && _z < length)
    174.                 {
    175.                     if(_x == x && _z == z)
    176.                     {
    177.                         continue;
    178.                     }
    179.                     if(_x > x && _z == z && mapArray[_x,_z].type != 0)
    180.                     {
    181.                         newRoomAdjacents ++;
    182.                         //mapArray[_x,_z].connections ++;  <-----TRYING TO ADD CONNECTIONS
    183.                     }
    184.                     if(_x < x && _z == z && mapArray[_x,_z].type == 1)
    185.                     {
    186.                         newRoomAdjacents ++;
    187.                     }
    188.                     if(_x == x && _z > z && mapArray[_x,_z].type != 0)
    189.                     {
    190.                         newRoomAdjacents ++;
    191.                     }
    192.                     if(_x == x && _z < z && mapArray[_x,_z].type != 0)
    193.                     {
    194.                         newRoomAdjacents ++;
    195.                     }
    196.                 }
    197.             }
    198.         }
    199.         return newRoomAdjacents;
    200.     }
    201.  
    202.     void OnGUI()
    203.     {
    204.         if(complete)
    205.         {
    206.  
    207.             GUI.BeginGroup(new Rect(2,2,miniMapX,miniMapZ));
    208.  
    209.             for(int x = 0; x < width; x++)
    210.             {
    211.                 for(int z = 0; z < length; z++)
    212.                 {
    213.                     if(mapArray[x,z].type == 2)
    214.                     {
    215.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), startingRoom);
    216.                     }
    217.                     else if(mapArray[x,z].type == 1)
    218.                     {
    219.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), basicRoom);
    220.                      
    221.                         //GUI.Label(new Rect(x*iconX,z*iconZ,iconX-1,iconZ-1),"W: " + mapArray[x,z].wConnected);
    222.                     }
    223.                     else if(mapArray[x,z].type == 0)
    224.                     {
    225.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-1,iconZ-1), empty);
    226.                     }
    227.                 }
    228.             }
    229.      
    230.         GUI.EndGroup();
    231.  
    232.         }
    233.     }
    234. }
    And here is the RoomID Class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RoomID
    5. {
    6.     public int type;            //0 = empty
    7.                                 //1 = basic room(which may contain enemies or not)
    8.                                 //2 = starting room
    9.                                 //3 = boss room
    10.                                 //4 = treasure room
    11.                                 //5 = shop
    12.  
    13.     public int id;                //this rooms unique ID
    14.  
    15.     public int intensity;        //for a basic, miniboss or boss room, how difficult it is(more enemies, stronger enemies)
    16.     public int connections;        //with how many rooms is this room connected
    17.  
    18.     public bool nConnected;        //north connected
    19.     public bool sConnected;        //south connected
    20.     public bool eConnected;        //east connected
    21.     public bool wConnected;        //west connected
    22.  
    23.     public int xPos;            //its X axis position on the grid
    24.     public int zPos;            //its Z axis position on the grid
    25. }
    26.  
    I hope anyone can help me!

    Cheers,
    Darryl
     
  11. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I remember solving that issue, @darryldonohoe. It blew my mind the first time :p

    Make your nConnected, sConnected, eConnected and wConnected variables the same class that they are in. Then you will have a reference to every room a room is connected to and access to all of it's data from the room you are in. What is currentRoom.nConnected.nConnected? The room 2 rooms north of yours!

    This is useful for making grids that need to be connected for path finding. When making a class of nodes, you make an array of nodes inside it (the same class type) and those nodes represent the other nodes connected to this one.
     
  12. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Hey Tomnnn, the problem is, that the nConnected and so on are not working as they should be. They tend to turn true while it shouldn't be possible. For example:

    The W is wrong, it should say E. So they are not checking for their west side, but for their east side!

    connectionsScreenshot.jpg

    As you can see, the room on the most left, should have a true value, but it is false. As well as the room on the same row on the most right, should have it to false.

    I thought it was a problem with the double for loop, but that was not the case.(or at least not that i can find)

    So after the first problem(the actual generating), I have to look for a way to fix the new problem!

    Cheers,
    Darryl
     
  13. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    Well finally found a way to make it sort of work, but it sometimes gives me errors, while most rooms are correct.
    There are occasionally rooms which say that eastConnected or westConnected are false, while they should be true.

    Anyway here is the code, let me know what you think. (any improvements or hopefully the solution)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class MapGenerator : MonoBehaviour
    6. {
    7.     public int width = 15;                //total width of the grid
    8.     public int length = 15;             //total length of the grid
    9.    
    10.     public RoomID[,] mapArray;            //2d array of classes that holds everything
    11.    
    12.     //PRIVATE VARIABLES
    13.  
    14.     //level variables
    15.     public int level;                    //our current level we are on, this to determine difficulty and level size
    16.     public int rooms;                    //this variable will handle how many rooms have been built
    17.     public int iterations;                //how many rooms will be instantiated? This will randomize itself at the beginning
    18.  
    19.     //current positions variables
    20.     private int curRoom;                //current room int which we use to get values of the Vector2 list below
    21.     private int curRoomPosX;            //these curRoomPosX and Z variables will keep track on which position we currently are
    22.     private int curRoomPosZ;            //this to determine our next position whith the newRoomPosX and Z variable
    23.     private Vector2[] roomPositions;    //list of Vector2 positions to determine where the next RANDOM room can be placed
    24.  
    25.     //next position variables
    26.     private int newDirection;            //this variable will keep track which direction we want the next room to be set
    27.     private int newRoomPosX;            //this variable will set the next rooms position on the X axis
    28.     private int newRoomPosZ;            //this variable will set the next rooms position on the Z axis
    29.  
    30.     private int newRoomAdjacents;        //how many adjacent room has the new room?
    31.  
    32.     private bool complete;                //level generating is completed
    33.  
    34.     public float miniMapX;                //mini map width
    35.     public float miniMapZ;                //mini map height
    36.    
    37.     public Texture2D startingRoom;
    38.     public Texture2D basicRoom;
    39.     public Texture2D empty;
    40.    
    41.     private float iconX;                //icon width of each room
    42.     private float iconZ;                //icon height of each room
    43.  
    44.  
    45.     void Start()
    46.     {
    47.         mapArray = new RoomID[width, length];        //Assign the width and length value to the 2d Array grid
    48.         rooms = 0;                                    //just to be sure
    49.         iterations = Random.Range(5 + (level * 2), 7 + (level * 3));    //randomize iterations
    50.         roomPositions = new Vector2[iterations+1];    //set the roomPositions Vector2 array higher then iterations by 1
    51.  
    52.         miniMapX = Screen.width;                    //for now set the mini map to cover the whole screen
    53.         miniMapZ = Screen.height;
    54.        
    55.         iconX = (miniMapX/width);
    56.         iconZ = (miniMapZ/length);
    57.  
    58.         //A 2 for loop, generating the grid and assigning basic values
    59.         for(int x = 0;x < width; x++)
    60.         {
    61.             for(int z = 0; z < length; z++)
    62.             {
    63.                 mapArray[x,z] = new RoomID();        //create a RoomID instance in each cell on the grid
    64.                 mapArray[x,z].xPos = x;                //passing the x value to the RoomID x position
    65.                 mapArray[x,z].zPos = z;                //passing the z value to the RoomID z position
    66.                 mapArray[x,z].type = 0;                //each starting cell is an empty room, so we set this to 0
    67.             }
    68.         }
    69.        
    70.         Generate();                                    //calling the generate method for actual generating
    71.     }
    72.    
    73.     void Generate()
    74.     {
    75.         //while rooms is equal or less then iterations, we start building rooms
    76.         while(rooms <= iterations)
    77.         {
    78.             if(rooms == 0)
    79.             {
    80.                 roomPositions[rooms] = new Vector2(width/2,length/2);
    81.  
    82.                 mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].type = 2;
    83.                 mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].id = rooms;
    84.  
    85.                 rooms++;
    86.             }
    87.             else
    88.             {
    89.                 curRoom = Random.Range(0, rooms);                //grab random room in rooms so we know where to build next
    90.  
    91.                 curRoomPosX = (int)roomPositions[curRoom].x;    //set curRoomPosX to roomPositionsX value which we picked earlier
    92.                 curRoomPosZ = (int)roomPositions[curRoom].y;    //set curRoomPosZ to roomPositionsY value which we picked earlier
    93.  
    94.                 newDirection = Random.Range(1,5);                //Randomly choose next direction each time we build a new room
    95.                
    96.                 if(newDirection == 1)        //NORTH
    97.                 {
    98.                     newRoomPosX = curRoomPosX;
    99.                     newRoomPosZ = curRoomPosZ + 1;
    100.                 }
    101.                 else if(newDirection == 2)    //EAST
    102.                 {
    103.                     newRoomPosX = curRoomPosX + 1;
    104.                     newRoomPosZ = curRoomPosZ;
    105.                 }
    106.                 else if(newDirection == 3)    //SOUTH
    107.                 {
    108.                     newRoomPosX = curRoomPosX;
    109.                     newRoomPosZ = curRoomPosZ - 1;
    110.                 }
    111.                 else if(newDirection == 4)    //WEST
    112.                 {
    113.                     newRoomPosX = curRoomPosX - 1;
    114.                     newRoomPosZ = curRoomPosZ;
    115.                 }
    116.                 else if(newDirection == 5)    //OUT OF RANGE
    117.                 {
    118.                     Debug.Log("Out of range!");
    119.                 }
    120.                
    121.                 //we just have to make sure that the new position is on the grid
    122.                 if(newRoomPosX < width && newRoomPosX > 0 && newRoomPosZ < length && newRoomPosZ > 0)
    123.                 {
    124.                     if(mapArray[newRoomPosX, newRoomPosZ].type == 0)    //roomtype = 0, so space is available
    125.                     {
    126.                         newRoomAdjacents = Adjacents(newRoomPosX,newRoomPosZ);
    127.  
    128.                         if(newRoomAdjacents != 1)
    129.                         {
    130.                             //More then 1 adjacent rooms, which we try to avoid so we do not have 1 big square level
    131.                         }
    132.                         else
    133.                         {
    134.                             roomPositions[rooms] = new Vector2(newRoomPosX, newRoomPosZ);
    135.                            
    136.                             mapArray[newRoomPosX,newRoomPosZ].type = 1;
    137.                             mapArray[newRoomPosX,newRoomPosZ].id = rooms;    //set the RoomID.id equal to rooms
    138.  
    139.                             rooms++;
    140.                         }
    141.                     }
    142.                     else
    143.                     {
    144.                         //Debug.Log("Cell already taken, searching for new space");
    145.                     }
    146.                 }
    147.                 else
    148.                 {
    149.                     //Debug.Log("Out of the grid, searching for new space.");
    150.                 }
    151.             }
    152.         }
    153.  
    154.         if(rooms >= iterations)
    155.         {
    156.             complete = true;
    157.  
    158.             for(int i = 0; i < iterations; i++)
    159.             {
    160.                 //newRoomAdjacents = Adjacents(mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].xPos,
    161.                 //                             mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].zPos);
    162.  
    163.                 //mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].connections = newRoomAdjacents;
    164.  
    165.                 if(mapArray[(int)roomPositions[i].x+1, (int)roomPositions[i].y].type != 0)
    166.                 {
    167.                     mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].eConnected = true;
    168.                 }
    169.                 if(mapArray[(int)roomPositions[i].x-1, (int)roomPositions[i].y].type != 0)
    170.                 {
    171.                     mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].wConnected = true;
    172.                 }
    173.                 if(mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y+1].type != 0)
    174.                 {
    175.                     mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].nConnected = true;
    176.                 }
    177.                 if(mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y-1].type != 0)
    178.                 {
    179.                     mapArray[(int)roomPositions[i].x, (int)roomPositions[i].y].sConnected = true;
    180.                 }
    181.             }
    182.         }
    183.     }
    184.  
    185.  
    186.     //this is used to calculate how many adjacent rooms we have
    187.     //it does a 2 for loop where it looks around the current room position from -1 to +1
    188.     //in UP DOWN LEFT and RIGHT direction, then if the room type is NOT 0, we add 1 to the newRoomAdjacents
    189.     int Adjacents(int x, int z)
    190.     {
    191.         int curX = x;
    192.         int curZ = z;
    193.  
    194.         newRoomAdjacents = 0;
    195.  
    196.         for(int _x = -1; _x < x + 2; _x++)
    197.         {
    198.             for(int _z = -1; _z < z + 2; _z++)
    199.             {
    200.                 if(_x > 0 && _x < width && _z > 0 && _z < length)
    201.                 {
    202.                     if(_x == x && _z == z)
    203.                     {
    204.                         continue;
    205.                     }
    206.                     if(_x > x && _z == z && mapArray[_x,_z].type != 0)
    207.                     {
    208.                         newRoomAdjacents ++;
    209.                     }
    210.                     if(_x < x && _z == z && mapArray[_x,_z].type != 0)
    211.                     {
    212.                         newRoomAdjacents ++;
    213.                     }
    214.                     if(_x == x && _z > z && mapArray[_x,_z].type != 0)
    215.                     {
    216.                         newRoomAdjacents ++;
    217.                     }
    218.                     if(_x == x && _z < z && mapArray[_x,_z].type != 0)
    219.                     {
    220.                         newRoomAdjacents ++;
    221.                     }
    222.                 }
    223.             }
    224.         }
    225.         return newRoomAdjacents;
    226.     }
    227.  
    228.    
    229.     void OnGUI()
    230.     {
    231.         if(complete)
    232.         {
    233.  
    234.             GUI.BeginGroup(new Rect(2,2,miniMapX,miniMapZ));
    235.  
    236.             for(int x = 0; x < width; x++)
    237.             {
    238.                 for(int z = 0; z < length; z++)
    239.                 {
    240.                     if(mapArray[x,z].type == 2)
    241.                     {
    242.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), startingRoom);
    243.                     }
    244.                     else if(mapArray[x,z].type == 1)
    245.                     {
    246.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), basicRoom);
    247.                        
    248.                         GUI.Label(new Rect(x*iconX,z*iconZ,iconX-1,iconZ-1),"East: " + mapArray[x,z].sConnected + " / West:" +
    249.                                   mapArray[x,z].wConnected + " type: " + mapArray[x,z].type);
    250.                     }
    251.                     else if(mapArray[x,z].type == 0)
    252.                     {
    253.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-1,iconZ-1), empty);
    254.                     }
    255.                 }
    256.             }
    257.        
    258.         GUI.EndGroup();
    259.  
    260.         }
    261.     }
    262. }
    Cheers,
    Darryl
     
  14. darryldonohoe

    darryldonohoe

    Joined:
    Jan 10, 2012
    Posts:
    55
    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!

    generated01.jpg

    And here is the code!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class MapGenerator : MonoBehaviour
    6. {
    7.     public int width = 15;                //total width of the grid
    8.     public int length = 15;             //total length of the grid
    9.    
    10.     public RoomID[,] mapArray;            //2d array of classes that holds everything
    11.    
    12.     //PRIVATE VARIABLES
    13.  
    14.     //level variables
    15.     public int level;                    //our current level we are on, this to determine difficulty and level size
    16.     public int rooms;                    //this variable will handle how many rooms have been built
    17.     public int iterations;                //how many rooms will be instantiated? This will randomize itself at the beginning
    18.  
    19.     //current positions variables
    20.     private int curRoom;                //current room int which we use to get values of the Vector2 list below
    21.     private int curRoomPosX;            //these curRoomPosX and Z variables will keep track on which position we currently are
    22.     private int curRoomPosZ;            //this to determine our next position whith the newRoomPosX and Z variable
    23.     private Vector2[] roomPositions;    //list of Vector2 positions to determine where the next RANDOM room can be placed
    24.  
    25.     //next position variables
    26.     private int newDirection;            //this variable will keep track which direction we want the next room to be set
    27.     private int newRoomPosX;            //this variable will set the next rooms position on the X axis
    28.     private int newRoomPosZ;            //this variable will set the next rooms position on the Z axis
    29.  
    30.     private int newRoomAdjacents;        //how many adjacent room has the new room?
    31.  
    32.     private bool complete;                //level generating is completed
    33.  
    34.     public float miniMapX;                //mini map width
    35.     public float miniMapZ;                //mini map height
    36.    
    37.     public Texture2D startingRoom;
    38.     public Texture2D basicRoom;
    39.     public Texture2D bossRoom;
    40.     public Texture2D empty;
    41.  
    42.     public Texture2D north;
    43.     public Texture2D south;
    44.     public Texture2D west;
    45.     public Texture2D east;
    46.  
    47.     public Texture2D ne;
    48.     public Texture2D nw;
    49.     public Texture2D ns;
    50.     public Texture2D sw;
    51.     public Texture2D se;
    52.     public Texture2D we;
    53.  
    54.     public Texture2D nJunct;
    55.     public Texture2D sJunct;
    56.     public Texture2D wJunct;
    57.     public Texture2D eJunct;
    58.  
    59.     public Texture2D cross;
    60.    
    61.     private float iconX;                //icon width of each room
    62.     private float iconZ;                //icon height of each room
    63.  
    64.  
    65.     void Start()
    66.     {
    67.         mapArray = new RoomID[width, length];        //Assign the width and length value to the 2d Array grid
    68.         rooms = 0;                                    //just to be sure
    69.         iterations = Random.Range(5 + (level * 2), 7 + (level * 3));    //randomize iterations
    70.         roomPositions = new Vector2[iterations+1];    //set the roomPositions Vector2 array higher then iterations by 1
    71.  
    72.         miniMapX = Screen.height;                    //for now set the mini map to cover the whole screen
    73.         miniMapZ = Screen.height;
    74.        
    75.         iconX = (miniMapX/width);
    76.         iconZ = (miniMapZ/length);
    77.  
    78.         //A 2 for loop, generating the grid and assigning basic values
    79.         for(int x = 0;x < width; x++)
    80.         {
    81.             for(int z = 0; z < length; z++)
    82.             {
    83.                 mapArray[x,z] = new RoomID();        //create a RoomID instance in each cell on the grid
    84.                 mapArray[x,z].xPos = x;                //passing the x value to the RoomID x position
    85.                 mapArray[x,z].zPos = z;                //passing the z value to the RoomID z position
    86.                 mapArray[x,z].type = 0;                //each starting cell is an empty room, so we set this to 0
    87.             }
    88.         }
    89.        
    90.         Generate();                                    //calling the generate method for actual generating
    91.     }
    92.    
    93.     void Generate()
    94.     {
    95.         //while rooms is equal or less then iterations, we start building rooms
    96.         while(rooms <= iterations)
    97.         {
    98.             if(rooms == 0)
    99.             {
    100.                 roomPositions[rooms] = new Vector2(width/2,length/2);
    101.  
    102.                 mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].type = 2;
    103.                 mapArray[(int)roomPositions[rooms].x,(int)roomPositions[rooms].y].id = rooms;
    104.  
    105.                 rooms++;
    106.             }
    107.             else
    108.             {
    109.                 curRoom = Random.Range(0, rooms);                //grab random room in rooms so we know where to build next
    110.  
    111.                 curRoomPosX = (int)roomPositions[curRoom].x;    //set curRoomPosX to roomPositionsX value which we picked earlier
    112.                 curRoomPosZ = (int)roomPositions[curRoom].y;    //set curRoomPosZ to roomPositionsY value which we picked earlier
    113.  
    114.                 newDirection = Random.Range(1,5);                //Randomly choose next direction each time we build a new room
    115.                
    116.                 if(newDirection == 1)        //NORTH
    117.                 {
    118.                     newRoomPosX = curRoomPosX;
    119.                     newRoomPosZ = curRoomPosZ + 1;
    120.                 }
    121.                 else if(newDirection == 2)    //EAST
    122.                 {
    123.                     newRoomPosX = curRoomPosX + 1;
    124.                     newRoomPosZ = curRoomPosZ;
    125.                 }
    126.                 else if(newDirection == 3)    //SOUTH
    127.                 {
    128.                     newRoomPosX = curRoomPosX;
    129.                     newRoomPosZ = curRoomPosZ - 1;
    130.                 }
    131.                 else if(newDirection == 4)    //WEST
    132.                 {
    133.                     newRoomPosX = curRoomPosX - 1;
    134.                     newRoomPosZ = curRoomPosZ;
    135.                 }
    136.                 else if(newDirection == 5)    //OUT OF RANGE
    137.                 {
    138.                     Debug.Log("Out of range!");
    139.                 }
    140.                
    141.                 //we just have to make sure that the new position is on the grid
    142.                 if(newRoomPosX < width && newRoomPosX > 0 && newRoomPosZ < length && newRoomPosZ > 0)
    143.                 {
    144.                     if(mapArray[newRoomPosX, newRoomPosZ].type == 0)    //roomtype = 0, so space is available
    145.                     {
    146.                         newRoomAdjacents = Adjacents(newRoomPosX,newRoomPosZ);
    147.  
    148.                         if(newRoomAdjacents != 1)
    149.                         {
    150.                             //More then 1 adjacent rooms, which we try to avoid so we do not have 1 big square level
    151.                         }
    152.                         else
    153.                         {
    154.                             roomPositions[rooms] = new Vector2(newRoomPosX, newRoomPosZ);
    155.                            
    156.                             mapArray[newRoomPosX,newRoomPosZ].type = 1;
    157.                             mapArray[newRoomPosX,newRoomPosZ].id = rooms;    //set the RoomID.id equal to rooms
    158.  
    159.                             mapArray[newRoomPosX,newRoomPosZ].distance = (Mathf.Abs(newRoomPosX - (int)roomPositions[0].x) +
    160.                                                                           Mathf.Abs(newRoomPosZ - (int)roomPositions[0].y));
    161.  
    162.                             rooms++;
    163.                         }
    164.                     }
    165.                     else
    166.                     {
    167.                         //Debug.Log("Cell already taken, searching for new space");
    168.                     }
    169.                 }
    170.                 else
    171.                 {
    172.                     //Debug.Log("Out of the grid, searching for new space.");
    173.                 }
    174.             }
    175.         }
    176.  
    177.         if(rooms >= iterations)
    178.         {
    179.             complete = true;
    180.  
    181.             int i = 0;
    182.             int j = 0;
    183.  
    184.             while(i < width)
    185.             {
    186.                 j = 0;
    187.                 while(j < length)
    188.                 {
    189.                     if(mapArray[i,j].type != 0)
    190.                     {
    191.                         North(i,j);
    192.                         South(i,j);
    193.                         East(i,j);
    194.                         West(i,j);
    195.  
    196.                         if(mapArray[i,j].connections == 1)        //DEAD END
    197.                         {
    198.                             if(mapArray[i,j].nConnected)        //NORTH
    199.                             {
    200.                                 mapArray[i,j].mini = north;
    201.                             }
    202.                             else if(mapArray[i,j].sConnected)    //SOUTH
    203.                             {
    204.                                 mapArray[i,j].mini = south;
    205.                             }
    206.                             else if(mapArray[i,j].wConnected)    //WEST
    207.                             {
    208.                                 mapArray[i,j].mini = west;
    209.                             }
    210.                             else if(mapArray[i,j].eConnected)    //EAST
    211.                             {
    212.                                 mapArray[i,j].mini = east;
    213.                             }
    214.                         }
    215.                         else if(mapArray[i,j].connections == 2)
    216.                         {
    217.                             //STRAIGHT
    218.                             if(mapArray[i,j].nConnected && mapArray[i,j].sConnected)        //NORTH SOUTH
    219.                             {
    220.                                 mapArray[i,j].mini = ns;
    221.                             }
    222.                             else if(mapArray[i,j].wConnected && mapArray[i,j].eConnected)    //WEST EAST
    223.                             {
    224.                                 mapArray[i,j].mini = we;
    225.                             }
    226.                             //CORNERS
    227.                             else if(mapArray[i,j].sConnected && mapArray[i,j].eConnected)    //SOUTH EAST
    228.                             {
    229.                                 mapArray[i,j].mini = se;
    230.                             }
    231.                             else if(mapArray[i,j].sConnected && mapArray[i,j].wConnected)    //SOUTH WEST
    232.                             {
    233.                                 mapArray[i,j].mini = sw;
    234.                             }
    235.                             else if(mapArray[i,j].nConnected && mapArray[i,j].wConnected)    //NORTH WEST
    236.                             {
    237.                                 mapArray[i,j].mini = nw;
    238.                             }
    239.                             else if(mapArray[i,j].nConnected && mapArray[i,j].eConnected)    //NORTH EAST
    240.                             {
    241.                                 mapArray[i,j].mini = ne;
    242.                             }
    243.                         }
    244.                         else if(mapArray[i,j].connections == 3)    //T JUNCTIONS
    245.                         {
    246.                             if(mapArray[i,j].nConnected && mapArray[i,j].eConnected && mapArray[i,j].wConnected)        //NORTH T JUNCTION
    247.                             {
    248.                                 mapArray[i,j].mini = nJunct;
    249.                             }
    250.                             else if(mapArray[i,j].nConnected && mapArray[i,j].eConnected && mapArray[i,j].sConnected)    //EAST T JUNCTION
    251.                             {
    252.                                 mapArray[i,j].mini = eJunct;
    253.                             }
    254.                             else if(mapArray[i,j].sConnected && mapArray[i,j].wConnected && mapArray[i,j].eConnected)    //SOUTH T JUNCTION
    255.                             {
    256.                                 mapArray[i,j].mini = sJunct;
    257.                             }
    258.                             else if(mapArray[i,j].sConnected && mapArray[i,j].wConnected && mapArray[i,j].nConnected)    //WEST T JUNCTION
    259.                             {
    260.                                 mapArray[i,j].mini = wJunct;
    261.                             }
    262.                         }
    263.                         else if(mapArray[i,j].connections == 4)    //CROSS
    264.                         {
    265.                             mapArray[i,j].mini = cross;
    266.                         }
    267.                         //if(mapArray[i,j].distance >= iterations/2 && mapArray[i,j].connections == 1)
    268.                         //{
    269.  
    270.                         //}
    271.                     }
    272.                     j++;
    273.                 }
    274.                 i++;
    275.             }
    276.         }
    277.     }
    278.  
    279.     bool North(int x, int z)
    280.     {
    281.         if(z == 0) return false;
    282.  
    283.         if(mapArray[x,z-1].type != 0)
    284.         {
    285.             mapArray[x,z].nConnected = true;
    286.             mapArray[x,z].connections ++;
    287.             return true;
    288.         }
    289.  
    290.         return false;
    291.     }
    292.     bool South(int x, int z)
    293.     {
    294.         if(z == length-1) return false;
    295.  
    296.         if(mapArray[x,z+1].type != 0)
    297.         {
    298.             mapArray[x,z].sConnected = true;
    299.             mapArray[x,z].connections ++;
    300.             return true;
    301.         }
    302.         return false;
    303.     }
    304.     bool East(int x, int z)
    305.     {
    306.         if(x == width-1) return false;
    307.  
    308.         if(mapArray[x+1,z].type != 0)
    309.         {
    310.             mapArray[x,z].eConnected = true;
    311.             mapArray[x,z].connections ++;
    312.             return true;
    313.         }
    314.         return false;
    315.     }
    316.     bool West(int x, int z)
    317.     {
    318.         if(x == 0) return false;
    319.  
    320.         if(mapArray[x-1,z].type != 0)
    321.         {
    322.             mapArray[x,z].wConnected = true;
    323.             mapArray[x,z].connections ++;
    324.             return true;
    325.         }
    326.         return false;
    327.     }
    328.  
    329.     //this is used to calculate how many adjacent rooms we have
    330.     //it does a 2 for loop where it looks around the current room position from -1 to +1
    331.     //in UP DOWN LEFT and RIGHT direction, then if the room type is NOT 0, we add 1 to the newRoomAdjacents
    332.     int Adjacents(int x, int z)
    333.     {
    334.         int curX = x;
    335.         int curZ = z;
    336.  
    337.         newRoomAdjacents = 0;
    338.  
    339.         for(int _x = -1; _x < x + 2; _x++)
    340.         {
    341.             for(int _z = -1; _z < z + 2; _z++)
    342.             {
    343.                 if(_x > 0 && _x < width && _z > 0 && _z < length)
    344.                 {
    345.                     if(_x == x && _z == z)
    346.                     {
    347.                         continue;
    348.                     }
    349.                     if(_x > x && _z == z && mapArray[_x,_z].type != 0)
    350.                     {
    351.                         newRoomAdjacents ++;
    352.                     }
    353.                     if(_x < x && _z == z && mapArray[_x,_z].type != 0)
    354.                     {
    355.                         newRoomAdjacents ++;
    356.                     }
    357.                     if(_x == x && _z > z && mapArray[_x,_z].type != 0)
    358.                     {
    359.                         newRoomAdjacents ++;
    360.                     }
    361.                     if(_x == x && _z < z && mapArray[_x,_z].type != 0)
    362.                     {
    363.                         newRoomAdjacents ++;
    364.                     }
    365.                 }
    366.             }
    367.         }
    368.         return newRoomAdjacents;
    369.     }
    370.  
    371.    
    372.     void OnGUI()
    373.     {
    374.         if(complete)
    375.         {
    376.  
    377.             GUI.BeginGroup(new Rect(miniMapX/2,0,miniMapX,miniMapZ));
    378.  
    379.             for(int x = 0; x < width; x++)
    380.             {
    381.                 for(int z = 0; z < length; z++)
    382.                 {
    383.                     if(mapArray[x,z].type == 2)
    384.                     {
    385.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX,iconZ), mapArray[x,z].mini);
    386.  
    387.                         //GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), startingRoom);
    388.                     }
    389.                     else if(mapArray[x,z].type == 1)
    390.                     {
    391.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX,iconZ), mapArray[x,z].mini);
    392.                         //GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), basicRoom);
    393.                        
    394.                         //GUI.Label(new Rect(x*iconX,z*iconZ,iconX-1,iconZ-1),"E: " + mapArray[x,z].eConnected + " / W:" +
    395.                         //          mapArray[x,z].wConnected + " / S: " + mapArray[x,z].sConnected +
    396.                         //          " / N: " + mapArray[x,z].nConnected + " / ID: " + mapArray[x,z].type);
    397.                     }
    398.                     else if(mapArray[x,z].type == 3)
    399.                     {
    400.                         //GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX-2,iconZ-2), bossRoom);
    401.                     }
    402.                     else if(mapArray[x,z].type == 0)
    403.                     {
    404.                         GUI.DrawTexture(new Rect(x*iconX,z*iconZ,iconX,iconZ), empty);
    405.                     }
    406.                 }
    407.             }
    408.        
    409.         GUI.EndGroup();
    410.  
    411.         }
    412.     }
    413. }
    And the RoomID class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RoomID
    5. {
    6.     public int type;            //0 = empty
    7.                                 //1 = basic room(which may contain enemies or not)
    8.                                 //2 = starting room
    9.                                 //3 = boss room
    10.                                 //4 = treasure room
    11.                                 //5 = shop
    12.  
    13.     public int id;                //this rooms unique ID
    14.  
    15.     public int intensity;        //for a basic, miniboss or boss room, how difficult it is(more enemies, stronger enemies)
    16.     public int connections;        //with how many rooms is this room connected
    17.  
    18.     public bool nConnected;        //north connected
    19.     public bool sConnected;        //south connected
    20.     public bool eConnected;        //east connected
    21.     public bool wConnected;        //west connected
    22.  
    23.     public int xPos;            //its X axis position on the grid
    24.     public int zPos;            //its Z axis position on the grid
    25.  
    26.     public int distance;        //this rooms distance from the start
    27.  
    28.     public Texture2D mini;      
    29. }
    30.  
    Any tips or suggestions are always welcome!

    Cheers,
    Darryl