Guest User

Untitled

a guest
Jan 22nd, 2021
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.70 KB | None | 0 0
  1. using System.Linq;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class DungeonGenerator : MonoBehaviour {
  7.  
  8.     public GameManager GM;
  9.     public GameObject NavMesh;
  10.     public Transform RoomsContainer;
  11.     public Transform WallsContainer;
  12.     public Transform DoorsContainer;
  13.  
  14.     public int maxRooms = 100;
  15.     public int doorFrequency = 5;
  16.     public int forwardPreference;
  17.  
  18.     public GameObject Spawnpoints;
  19.     public GameObject[] SpawnpointsGM;
  20.     public GameObject roomPrefab;
  21.     public GameObject[] wallPrefabs;
  22.     public GameObject floorPrefab;
  23.     public GameObject doorwayPrefab;
  24.     public GameObject ladderPrefab;
  25.     public GameObject bossRoomPrefab;
  26.     public GameObject startingRoomPrefab;
  27.     public int spawnPointFrequency = 5;
  28.     private Dictionary<GameObject, Vector3> rooms = new Dictionary<GameObject, Vector3>();
  29.  
  30.     private List<Vector3> spawnPositions = new List<Vector3>();
  31.     private Vector3 lastPosition;
  32.     private Vector3 position;
  33.     private bool findNewPosition = false;
  34.     private Vector3 furthestPosition = new Vector3(0, 0, 0);
  35.     private Vector3 closestPosition = new Vector3(0, 0, 0);
  36.     private string state;
  37.     private string progress;
  38.  
  39.     private void Start()
  40.     {
  41.         GM = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
  42.         maxRooms = GM.roomsToGenerate;
  43.         StartCoroutine("GenerateDungeon", maxRooms);
  44.     }
  45.  
  46.     private void OnGUI()
  47.     {
  48.         if (!GM.generationComplete)
  49.         {
  50.             GUI.color = Color.white;
  51.             GUI.Label(new Rect(20, Screen.height - 20, 500, 20), state + " : " + progress);
  52.         }
  53.     }
  54.  
  55.     private IEnumerable GenerateDungeon(int max)
  56.     {
  57.         state = "Generating Rooms";
  58.         GenerateRooms(max);
  59.         GenerateBossRoom();
  60.         GenerateStartingRoom();
  61.         state = "Generating Walls";
  62.         GenerateWalls();
  63.         state = "Generating Doorways";
  64.         GenerateDoorways();
  65.         state = "Generating AI NavMesh";
  66.         GenerateNavMesh();
  67.         state = "Complete!";
  68.         GM.setNewSpawnPoints(spawnPositions);
  69.         GM.Spawnpoints = SpawnpointsGM;
  70.         GM.startGame();
  71.         rooms.Clear();
  72.         spawnPositions.Clear();
  73.         return null;
  74.     }
  75.  
  76.     private void GenerateRooms(int max)
  77.     {
  78.         int i = 0;
  79.  
  80.         position = new Vector3(0, 0, 0);
  81.  
  82.         GameObject newRoom;
  83.         newRoom = Instantiate(roomPrefab, position, roomPrefab.transform.rotation);
  84.         newRoom.transform.SetParent(RoomsContainer);
  85.         rooms.Add(newRoom, position);
  86.  
  87.         //Main Generator
  88.         while (i < max)
  89.         {
  90.             if (findNewPosition)
  91.             {
  92.                 int r = UnityEngine.Random.Range(0, rooms.Count - 1);
  93.                 Vector3[] roomPositionsArray = rooms.Values.ToArray();
  94.                 position = roomPositionsArray[r];
  95.                 findNewPosition = false;
  96.             }
  97.             else
  98.             {
  99.                 List<Vector3> validPositions = new List<Vector3>();
  100.  
  101.                 Vector3 checkPositionFront = position + new Vector3(0, 0, 10);
  102.                 Vector3 checkPositionBack = position + new Vector3(0, 0, -10);
  103.                 Vector3 checkPositionLeft = position + new Vector3(-10, 0, 0);
  104.                 Vector3 checkPositionRight = position + new Vector3(10, 0, 0);
  105.  
  106.                 bool roomPresentFront = false;
  107.                 bool roomPresentBack = false;
  108.                 bool roomPresentLeft = false;
  109.                 bool roomPresentRight = false;
  110.  
  111.                 foreach (KeyValuePair<GameObject, Vector3> kv in rooms)
  112.                 {
  113.                     if (kv.Value == checkPositionFront)
  114.                     {
  115.                         roomPresentFront = true;
  116.                     }
  117.                     if (kv.Value == checkPositionBack)
  118.                     {
  119.                         roomPresentBack = true;
  120.                     }
  121.                     if (kv.Value == checkPositionLeft)
  122.                     {
  123.                         roomPresentLeft = true;
  124.                     }
  125.                     if (kv.Value == checkPositionRight)
  126.                     {
  127.                         roomPresentRight = true;
  128.                     }
  129.                 }
  130.  
  131.                 if (!roomPresentFront)
  132.                 {
  133.                     validPositions.Add(checkPositionFront);
  134.                 }
  135.  
  136.                 if (!roomPresentBack)
  137.                 {
  138.                     validPositions.Add(checkPositionBack);
  139.                 }
  140.  
  141.                 if (!roomPresentLeft)
  142.                 {
  143.                     validPositions.Add(checkPositionLeft);
  144.                 }
  145.  
  146.                 if (!roomPresentRight)
  147.                 {
  148.                     validPositions.Add(checkPositionRight);
  149.                 }
  150.  
  151.                 if (validPositions.Count > 0)
  152.                 {
  153.                     Vector3 spawnPosition = new Vector3();
  154.  
  155.                     int newRoomPosition = 0;
  156.  
  157.                     if (!roomPresentFront)
  158.                     {
  159.                         int p = Random.Range(0, forwardPreference);
  160.                         if (p == 0)
  161.                         {
  162.                             spawnPosition = checkPositionFront;
  163.                         }
  164.                         else
  165.                         {
  166.                             newRoomPosition = Random.Range(0, validPositions.Count);
  167.                             spawnPosition = validPositions[newRoomPosition];
  168.                         }
  169.                     }
  170.                     else
  171.                     {
  172.                         newRoomPosition = Random.Range(0, validPositions.Count);
  173.                         spawnPosition = validPositions[newRoomPosition];
  174.                     }
  175.  
  176.                     GameObject newRoomObject;
  177.                     newRoomObject = Instantiate(roomPrefab, spawnPosition, roomPrefab.transform.rotation);
  178.                     if (RoomsContainer)
  179.                     {
  180.                         newRoomObject.transform.SetParent(RoomsContainer);
  181.                     }
  182.  
  183.                     rooms.Add(newRoomObject, newRoomObject.transform.position);
  184.  
  185.                     position = spawnPosition;
  186.  
  187.                     int r = Random.Range(0, spawnPointFrequency);
  188.                     if(r == 0)
  189.                     {
  190.                         spawnPositions.Add(position + new Vector3(0, 2, 0));
  191.                     }
  192.                     i++;
  193.                     progress = i.ToString() + " / " + max.ToString();
  194.                 }
  195.                 else
  196.                 {
  197.                     findNewPosition = true;
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203.     private void GenerateBossRoom()
  204.     {
  205.         foreach (KeyValuePair<GameObject, Vector3> kvp in rooms)
  206.         {
  207.             if (kvp.Value.z > furthestPosition.z)
  208.             {
  209.                 furthestPosition = kvp.Value;
  210.             }
  211.         }
  212.         GameObject newBossRoom = Instantiate(bossRoomPrefab, furthestPosition, ladderPrefab.transform.rotation);
  213.         newBossRoom.transform.SetParent(transform);
  214.     }
  215.  
  216.     private void GenerateStartingRoom()
  217.     {
  218.         foreach(KeyValuePair<GameObject, Vector3> kvp in rooms)
  219.         {
  220.             if(kvp.Value.z < closestPosition.z)
  221.             {
  222.                 closestPosition = kvp.Value;
  223.             }
  224.         }
  225.         Spawnpoints.transform.position = closestPosition;
  226.         GameObject newStartingRoom = Instantiate(startingRoomPrefab, closestPosition, startingRoomPrefab.transform.rotation);
  227.         Spawnpoints.transform.position = closestPosition + new Vector3(0, 0, -20);
  228.     }
  229.  
  230.     private void GenerateWalls()
  231.     {
  232.         int i = 0;
  233.         foreach (KeyValuePair<GameObject, Vector3> kvp in rooms)
  234.         {
  235.             Vector3 currentPosition = kvp.Value;
  236.             bool roomFront = false;
  237.             bool roomBack = false;
  238.             bool roomLeft = false;
  239.             bool roomRight = false;
  240.  
  241.             foreach (KeyValuePair<GameObject, Vector3> kvpNext in rooms)
  242.             {
  243.                 if (kvp.Key != kvpNext.Key)
  244.                 {
  245.                     if (kvp.Value + new Vector3(0, 0, 10) == kvpNext.Value)
  246.                     {
  247.                         roomFront = true;
  248.                     }
  249.                     if (kvp.Value + new Vector3(0, 0, -10) == kvpNext.Value)
  250.                     {
  251.                         roomBack = true;
  252.                     }
  253.                     if (kvp.Value + new Vector3(-10, 0, 0) == kvpNext.Value)
  254.                     {
  255.                         roomLeft = true;
  256.                     }
  257.                     if (kvp.Value + new Vector3(10, 0, 0) == kvpNext.Value)
  258.                     {
  259.                         roomRight = true;
  260.                     }
  261.                 }
  262.             }
  263.  
  264.             GameObject newWall;
  265.             if (!roomFront)
  266.             {
  267.                 if (currentPosition != furthestPosition)
  268.                 {
  269.                     newWall = Instantiate(wallPrefabs[0], currentPosition, wallPrefabs[0].transform.rotation);
  270.                     newWall.transform.parent = WallsContainer;
  271.                 }
  272.             }
  273.             if (!roomBack)
  274.             {
  275.                 if (currentPosition != closestPosition)
  276.                 {
  277.                     newWall = Instantiate(wallPrefabs[1], currentPosition, wallPrefabs[1].transform.rotation);
  278.                     newWall.transform.parent = WallsContainer;
  279.                 }
  280.             }
  281.             if (!roomLeft)
  282.             {
  283.                 newWall = Instantiate(wallPrefabs[2], currentPosition, wallPrefabs[2].transform.rotation);
  284.                 newWall.transform.parent = WallsContainer;
  285.             }
  286.             if (!roomRight)
  287.             {
  288.                 newWall = Instantiate(wallPrefabs[3], currentPosition, wallPrefabs[3].transform.rotation);
  289.                 newWall.transform.parent = WallsContainer;
  290.             }
  291.             i++;
  292.             progress = i.ToString();
  293.         }
  294.     }
  295.  
  296.     private void GenerateDoorways()
  297.     {
  298.         foreach (KeyValuePair<GameObject, Vector3> kvp in rooms)
  299.         {
  300.             Vector3 currentPosition = kvp.Value;
  301.             bool roomForward = false;
  302.             bool roomBack = false;
  303.             bool roomLeft = false;
  304.             bool roomRight = false;
  305.            
  306.  
  307.             foreach (KeyValuePair<GameObject, Vector3> otherKvp in rooms)
  308.             {
  309.                 if (otherKvp.Key != kvp.Key)
  310.                 {
  311.                     if (otherKvp.Value == currentPosition + new Vector3(0, 0, 10))
  312.                     {
  313.                         roomForward = true;
  314.                     }
  315.                     if (otherKvp.Value == currentPosition + new Vector3(0, 0, -10))
  316.                     {
  317.                         roomBack = true;
  318.                     }
  319.                     if (otherKvp.Value == currentPosition + new Vector3(-10, 0, 0))
  320.                     {
  321.                         roomLeft = true;
  322.                     }
  323.                     if (otherKvp.Value == currentPosition + new Vector3(10, 0, 0))
  324.                     {
  325.                         roomRight = true;
  326.                     }
  327.                 }
  328.             }
  329.  
  330.             if (roomForward && roomBack && !roomLeft && !roomRight)
  331.             {
  332.                 int p = Random.Range(0, doorFrequency);
  333.                 if (p == 0)
  334.                 {
  335.                     GameObject newDoor;
  336.                     newDoor = Instantiate(doorwayPrefab, currentPosition, doorwayPrefab.transform.rotation);
  337.                     newDoor.transform.parent = DoorsContainer;
  338.                 }
  339.             }
  340.         }
  341.     }
  342.  
  343.     void GenerateNavMesh()
  344.     {
  345.         NavMesh.SendMessage("BuildNavMesh");
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment