spectregames

DungeonManager.cs

May 7th, 2021
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class DungeonManager : MonoBehaviour {
  8.  
  9.     public GameObject[] randomItems;
  10.     public GameObject floorPrefab, wallPrefab, tilePrefab, exitPrefab;
  11.     public int totalFloorCount;
  12.  
  13.     [HideInInspector] public float minX, maxX, minY, maxY;
  14.  
  15.     List<Vector3> floorList = new List<Vector3>();
  16.     LayerMask floorMask, wallMask;
  17.  
  18.     void Start() {
  19.         floorMask = LayerMask.GetMask("Floor");
  20.         wallMask = LayerMask.GetMask("Wall");
  21.         DrunkenWalker();
  22.     }
  23.  
  24.     void Update() {
  25.         if (Application.isEditor && Input.GetKeyDown(KeyCode.F12)) {
  26.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  27.         }
  28.     }
  29.  
  30.     void DrunkenWalker() {
  31.         Vector3 curPosition = Vector3.zero;
  32.  
  33.         floorList.Add(curPosition);
  34.  
  35.         while (floorList.Count < totalFloorCount) {
  36.             switch (UnityEngine.Random.Range(1, 5)) {
  37.                 case 1: curPosition += Vector3.up;      break;
  38.                 case 2: curPosition += Vector3.right;   break;
  39.                 case 3: curPosition += Vector3.down;    break;
  40.                 case 4: curPosition += Vector3.left;    break;
  41.             }
  42.             bool alreadyInFloorList = false;
  43.             for (int i = 0; i < floorList.Count; ++i) {
  44.                 if (Vector3.Equals(curPosition, floorList[i])) {
  45.                     alreadyInFloorList = true;
  46.                     break;
  47.                 }
  48.             }
  49.             if (!alreadyInFloorList) {
  50.                 floorList.Add(curPosition);
  51.             }
  52.         }
  53.         for (int i = 0; i < floorList.Count; ++i) {
  54.             GameObject goTile = Instantiate(tilePrefab, floorList[i], Quaternion.identity) as GameObject;
  55.             goTile.name = tilePrefab.name;
  56.             goTile.transform.SetParent(transform);
  57.         }
  58.         StartCoroutine(DelayProgress());
  59.     }
  60.  
  61.     IEnumerator DelayProgress() {
  62.         while(FindObjectsOfType<TileSpawner>().Length > 0) {
  63.             yield return null;
  64.         }
  65.         ExitDoorway();
  66.         Vector2 _hitSize = Vector2.one * 0.8f;
  67.         for (int x = (int)minX - 2; x <= (int)maxX + 2; ++x) {
  68.             for(int y = (int)minY - 2; y <= (int)maxY + 2; ++y) {
  69.                 Collider2D _hitFloor = Physics2D.OverlapBox(new Vector2(x, y), _hitSize, 0, floorMask);
  70.                 if(_hitFloor) {
  71.                     if(!Vector2.Equals(_hitFloor.transform.position, floorList[floorList.Count - 1])) {
  72.                         Collider2D _hitTop =     Physics2D.OverlapBox(new Vector2(x, y + 1), _hitSize, 0, wallMask);
  73.                         Collider2D _hitRight =   Physics2D.OverlapBox(new Vector2(x + 1, y), _hitSize, 0, wallMask);
  74.                         Collider2D _hitBottom =  Physics2D.OverlapBox(new Vector2(x, y - 1), _hitSize, 0, wallMask);
  75.                         Collider2D _hitLeft =    Physics2D.OverlapBox(new Vector2(x - 1, y), _hitSize, 0, wallMask);
  76.  
  77.                         if((_hitTop || _hitRight || _hitBottom || _hitLeft) && !(_hitTop && _hitBottom) && !(_hitLeft && _hitRight)) {
  78.                             int _itemIndex = UnityEngine.Random.Range(0, randomItems.Length);
  79.                             GameObject goItems = Instantiate(randomItems[_itemIndex], _hitFloor.transform.position, Quaternion.identity) as GameObject;
  80.                             goItems.name = randomItems[_itemIndex].name;
  81.                             goItems.transform.SetParent(_hitFloor.transform);
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     void ExitDoorway() {
  90.         Vector3 doorPosition = floorList[floorList.Count - 1];
  91.         GameObject goDoor = Instantiate(exitPrefab, doorPosition, Quaternion.identity) as GameObject;
  92.         goDoor.name = exitPrefab.name;
  93.         goDoor.transform.SetParent(transform);
  94.     }
  95. }
  96.  
RAW Paste Data