Search Unity

Suggestion for a 2D Dungeon generator (with rooms)

Discussion in '2D' started by Flipp3rix, Apr 26, 2019.

  1. Flipp3rix

    Flipp3rix

    Joined:
    Mar 13, 2015
    Posts:
    10
    Hi guys, I want to make a 2D dungeon generator, tiled based, and now I'm stuck with rooms overlapping. In the scene i have a "Maker" GameObject who randomly moves, and spawn rooms.
    Maker script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class gen : MonoBehaviour
    6. {
    7.     public int room_number;
    8.     [SerializeField] private GameObject r;
    9.     int x, y;
    10.     private void Start()
    11.     {
    12.         for (int i = 0; i < room_number; i++) {
    13.             x = Random.Range(-5 * room_number, 5 * room_number);
    14.             y = Random.Range(-5 * room_number, 5 * room_number);
    15.             //confronto
    16.             this.transform.position = new Vector3(x, y, 0);
    17.             r =Instantiate(r, this.transform.position, this.transform.rotation);
    18.            
    19.             //r.transform.parent = this.transform;
    20.             //Debug.Log(i+" "+this.transform.position);  
    21.         }
    22.     }
    23. }
    24.  
    The Room prefab has multiple variables, like room size , min and max size and so on... you can see the script here:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Room : MonoBehaviour
    6. {
    7.     public int minx, miny, maxx, maxy;
    8.     public GameObject tilem;
    9.     public GameObject tilew;
    10.     public GameObject maker;
    11.     [SerializeField] private int x, y;
    12.  
    13.     private void Start()
    14.     {
    15.         //maker = GameObject.FindGameObjectWithTag("Maker");
    16.       /*this.transform.position = maker.transform.position;//new Vector2(maker, Random.Range(-1000, 1000));
    17.        */ GameObject az;
    18.         x = Random.Range(this.minx, this.maxx);
    19.         y = Random.Range(this.miny, this.maxy);
    20.         for (int i = 0; i < x; i++){
    21.             for (int j = 0; j < y; j++){
    22.                 if ((j != 0) && (i != 0) && (j != y - 1) && (i != x - 1)){
    23.                     az = Instantiate(tilem, new Vector3(i+this.transform.position.x, j + this.transform.position.y, 0), Quaternion.identity);
    24.                     az.transform.parent = this.transform;
    25.                 }
    26.                 else{
    27.                     az = Instantiate(tilew, new Vector3(i + this.transform.position.x, j + this.transform.position.y, 0), Quaternion.identity);
    28.                     az.transform.parent = this.transform;
    29.                 }
    30.             }
    31.         }
    32.         //this.transform.position = new Vector3(this.transform.position.x + 30, this.transform.position.y + 30, 0);
    33.         //Debug.Log(" ");
    34.     }
    35.  
    36.     public string toString() {
    37.         return "Stanza[x: " + x +" y: "+ y+"]";
    38.     }
    39. }
    They works fine until a room overlap another room, any suggestions? Sorry for my bad English
    Have a nice day :)
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
  3. Flipp3rix

    Flipp3rix

    Joined:
    Mar 13, 2015
    Posts:
    10
    Already seen and IMHO it's to difficult for my level, i need only a way ( not a script) to avoid overlapping
     
    LiterallyJeff likes this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Well, one way would be to keep a list of all the rooms you have generated, and when generating a new room, check if it overlaps the bounds of any other rooms. If it does, regenerate that room. You could have this limited to like 100 tries or something. It will get less performant as the map grows.

    Another option is to establish a grid of cells as your map, and as you add rooms, mark cells "occupied". Then when you place a new room, that room will check only the cells it will take up to and if any are occupied then it will regenerate.

    I've never coded a generator myself but that is what i would probably attempt.
     
  5. Flipp3rix

    Flipp3rix

    Joined:
    Mar 13, 2015
    Posts:
    10
    Ty, I will try