Advertisement
JontePonte

bad maze gen

Apr 23rd, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6.  
  7. public class MazeGenerator : MonoBehaviour
  8. {
  9. public int gridSize;
  10.  
  11. public Vector2Int entrance;
  12. public Vector2Int exit;
  13.  
  14. // Forms a grid
  15. public Point[,] points { get; private set; }
  16. // Forms pathways
  17. public List<Connection> connections { get; private set; }
  18.  
  19. bool foundValidPosition;
  20.  
  21. void Start()
  22. {
  23. StartCoroutine(GenerateMaze());
  24. }
  25.  
  26. void Update()
  27. {
  28. if (connections != null)
  29. {
  30. foreach (var connection in connections)
  31. {
  32. Debug.DrawLine((Vector2)connection.pointA.position, (Vector2)connection.pointB.position, Color.red);
  33. }
  34. }
  35. }
  36.  
  37. public IEnumerator GenerateMaze()
  38. {
  39. InitializePoints();
  40.  
  41. connections = new();
  42. Vector2Int position = entrance;
  43. foundValidPosition = true;
  44.  
  45. while (foundValidPosition)
  46. {
  47. Vector2Int direction;
  48.  
  49. while (true)
  50. {
  51. direction = RandomDirection();
  52.  
  53. if (!InsideBounds(position + direction)) continue;
  54. if (!IsLooping(position, direction)) break;
  55. }
  56.  
  57. connections.Add(new Connection
  58. {
  59. pointA = points[position.x, position.y],
  60. pointB = points[position.x + direction.x, position.y + direction.y]
  61. });
  62.  
  63. points[position.x, position.y].hasConnection = true;
  64. points[position.x + direction.x, position.y + direction.y].hasConnection = true;
  65.  
  66. position += direction;
  67.  
  68. if (IsStuck(position)) position = FindValidPosition();
  69.  
  70. yield return new WaitForSeconds(.05f);
  71. }
  72. }
  73.  
  74. // Checks if position is inside the bounds of grid
  75. public bool InsideBounds(Vector2Int position)
  76. {
  77. return position.x >= 0 && position.x < gridSize &&
  78. position.y >= 0 && position.y < gridSize;
  79. }
  80.  
  81. // If the point we are heading towards has a connection, a loop will be created
  82. bool IsLooping(Vector2Int position, Vector2Int direction)
  83. {
  84. return points[position.x + direction.x, position.y + direction.y].hasConnection;
  85. }
  86.  
  87. // If all of the four neighbouring points has a connection, we are stuck
  88. bool IsStuck(Vector2Int position)
  89. {
  90. bool isStuck = true;
  91.  
  92. if (InsideBounds(position + Vector2Int.right))
  93. {
  94. if (!points[position.x + 1, position.y].hasConnection)
  95. {
  96. isStuck = false;
  97. }
  98. }
  99.  
  100. if (InsideBounds(position + Vector2Int.left))
  101. {
  102. if (!points[position.x - 1, position.y].hasConnection)
  103. {
  104. isStuck = false;
  105. }
  106. }
  107.  
  108. if (InsideBounds(position + Vector2Int.up))
  109. {
  110. if (!points[position.x, position.y + 1].hasConnection)
  111. {
  112. isStuck = false;
  113. }
  114. }
  115.  
  116. if (InsideBounds(position + Vector2Int.down))
  117. {
  118. if (!points[position.x, position.y - 1].hasConnection)
  119. {
  120. isStuck = false;
  121. }
  122. }
  123.  
  124. return isStuck;
  125. }
  126.  
  127. Vector2Int FindValidPosition()
  128. {
  129. foreach (var connection in connections)
  130. {
  131. if (!IsStuck(connection.pointA.position))
  132. {
  133. foundValidPosition = true;
  134. return connection.pointA.position;
  135. }
  136.  
  137. if (!IsStuck(connection.pointB.position))
  138. {
  139. foundValidPosition = true;
  140. return connection.pointB.position;
  141. }
  142. }
  143.  
  144. foundValidPosition = false;
  145. return Vector2Int.zero;
  146. }
  147.  
  148. void InitializePoints()
  149. {
  150. points = new Point[gridSize, gridSize];
  151.  
  152. for (int y = 0, i = 0; y < gridSize; y++)
  153. {
  154. for (int x = 0; x < gridSize; x++, i++)
  155. {
  156. points[x, y] = new Point
  157. {
  158. position = new Vector2Int(x, y),
  159. hasConnection = false
  160. };
  161. }
  162. }
  163. }
  164.  
  165. Vector2Int RandomDirection()
  166. {
  167. int random = UnityEngine.Random.Range(0, 4);
  168.  
  169. switch (random)
  170. {
  171. case 0: return Vector2Int.down;
  172. case 1: return Vector2Int.up;
  173. case 2: return Vector2Int.left;
  174. case 3: return Vector2Int.right;
  175. default: throw new ArgumentException();
  176. }
  177. }
  178. }
  179.  
  180. public struct Point
  181. {
  182. public Vector2Int position;
  183. public bool hasConnection;
  184. }
  185.  
  186. public struct Connection
  187. {
  188. public Point pointA;
  189. public Point pointB;
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement