Advertisement
Lyk05

Untitled

Mar 24th, 2025
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CellularAutomataTest : MonoBehaviour
  6. {
  7. public class Cell
  8. {
  9. public bool visited = false;
  10. public bool[] status = new bool[6]; // [North, South, East, West, Up, Down]
  11. }
  12.  
  13. public Vector3 size;
  14. public int startPos = 0;
  15. public GameObject room;
  16. public Vector3 offset;
  17.  
  18. List<Cell> board;
  19.  
  20. void Start()
  21. {
  22. MazeGenerator();
  23. }
  24.  
  25. void GenerateDungeon()
  26. {
  27. for (int x = 0; x < size.x; x++)
  28. {
  29. for (int y = 0; y < size.y; y++)
  30. {
  31. for (int z = 0; z < size.z; z++)
  32. {
  33. int index = x + y * (int)size.x + z * (int)(size.x * size.y);
  34. Cell currentCell = board[index];
  35. if (currentCell.visited)
  36. {
  37. var newRoom = Instantiate(room, new Vector3(x * offset.x, y * offset.y, z * offset.z), Quaternion.identity, transform).GetComponent<RoomBehavior>();
  38. newRoom.UpdateRoom(currentCell.status);
  39. newRoom.name = $"Room {x}-{y}-{z}";
  40. Debug.Log($"Placed Room {x}-{y}-{z} with doors: {string.Join(",", currentCell.status)}");
  41. }
  42. }
  43. }
  44. }
  45. }
  46.  
  47. void MazeGenerator()
  48. {
  49. board = new List<Cell>();
  50.  
  51. for (int i = 0; i < size.x * size.y * size.z; i++)
  52. {
  53. board.Add(new Cell());
  54. }
  55.  
  56. int currentCell = startPos;
  57. Stack<int> path = new Stack<int>();
  58. int k = 0;
  59.  
  60. while (k < 1000)
  61. {
  62. k++;
  63. board[currentCell].visited = true;
  64. if (currentCell == board.Count - 1) break;
  65.  
  66. List<int> neighbors = CheckNeighbors(currentCell);
  67. if (neighbors.Count == 0)
  68. {
  69. if (path.Count == 0) break;
  70. currentCell = path.Pop();
  71. }
  72. else
  73. {
  74. path.Push(currentCell);
  75. int newCell = neighbors[Random.Range(0, neighbors.Count)];
  76.  
  77. // Ensure bidirectional connections
  78. int diff = newCell - currentCell;
  79. if (diff == 1)
  80. {
  81. board[currentCell].status[2] = true;
  82. board[newCell].status[3] = true;
  83. }
  84. else if (diff == -1)
  85. {
  86. board[currentCell].status[3] = true;
  87. board[newCell].status[2] = true;
  88. }
  89. else if (diff == size.x)
  90. {
  91. board[currentCell].status[4] = true;
  92. board[newCell].status[5] = true;
  93. }
  94. else if (diff == -size.x)
  95. {
  96. board[currentCell].status[5] = true;
  97. board[newCell].status[4] = true;
  98. }
  99. else if (diff == size.x * size.y)
  100. {
  101. board[currentCell].status[1] = true;
  102. board[newCell].status[0] = true;
  103. }
  104. else if (diff == -size.x * size.y)
  105. {
  106. board[currentCell].status[0] = true;
  107. board[newCell].status[1] = true;
  108. }
  109. Debug.Log($"Connecting cell {currentCell} to {newCell}, doors: {string.Join(",", board[currentCell].status)}");
  110. currentCell = newCell;
  111. }
  112. }
  113.  
  114. Debug.Log("Generating dungeon");
  115. GenerateDungeon();
  116. }
  117.  
  118. List<int> CheckNeighbors(int cell)
  119. {
  120. List<int> neighbors = new List<int>();
  121.  
  122. int x = cell % (int)size.x;
  123. int y = (cell / (int)size.x) % (int)size.y;
  124. int z = cell / (int)(size.x * size.y);
  125.  
  126. if (x + 1 < size.x && !board[cell + 1].visited) neighbors.Add(cell + 1);
  127. if (x - 1 >= 0 && !board[cell - 1].visited) neighbors.Add(cell - 1);
  128. if (y + 1 < size.y && !board[cell + (int)size.x].visited) neighbors.Add(cell + (int)size.x);
  129. if (y - 1 >= 0 && !board[cell - (int)size.x].visited) neighbors.Add(cell - (int)size.x);
  130. if (z + 1 < size.z && !board[cell + (int)(size.x * size.y)].visited) neighbors.Add(cell + (int)(size.x * size.y));
  131. if (z - 1 >= 0 && !board[cell - (int)(size.x * size.y)].visited) neighbors.Add(cell - (int)(size.x * size.y));
  132.  
  133. return neighbors;
  134. }
  135. }
  136.  
  137.  
  138. --- SCRIPT BREAK ---
  139.  
  140. using UnityEngine;
  141.  
  142. public class RoomBehavior : MonoBehaviour
  143. {
  144. public GameObject[] walls; // Order: [North, South, East, West, Up, Down]
  145. public GameObject[] doors; // Order: [North, South, East, West, Up, Down]
  146.  
  147. public void UpdateRoom(bool[] status)
  148. {
  149. for (int i = 0; i < status.Length; i++)
  150. {
  151. doors[i].SetActive(status[i]); // Activate doors where needed
  152. walls[i].SetActive(!status[i]); // Deactivate walls if there's a door
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement