Advertisement
JontePonte

maze yeah

Apr 24th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 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.  
  17. bool foundValidPosition;
  18.  
  19. void Start()
  20. {
  21. StartCoroutine(GenerateMaze());
  22. }
  23.  
  24. void OnDrawGizmos()
  25. {
  26. if (points != null)
  27. {
  28. foreach (var cell in points)
  29. {
  30. if (cell.hasConnection)
  31. {
  32. Gizmos.color = Color.red;
  33. }
  34. else
  35. {
  36. Gizmos.color = Color.gray;
  37. }
  38.  
  39. Gizmos.DrawCube((Vector2)cell.position, Vector2.one);
  40. }
  41. }
  42. }
  43.  
  44. public IEnumerator GenerateMaze()
  45. {
  46. InitializePoints();
  47.  
  48. Vector2Int position = entrance;
  49. foundValidPosition = true;
  50.  
  51. while (foundValidPosition)
  52. {
  53. Vector2Int direction;
  54.  
  55. while (true)
  56. {
  57. direction = RandomDirection();
  58.  
  59. if (!InsideBounds(position + direction)) continue;
  60. if (!IsLooping(position, direction)) break;
  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 * 2;
  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. bool IsLooping(Vector2Int position, Vector2Int direction)
  82. {
  83. if (InsideBounds(new Vector2Int(position.x + direction.x * 2, position.y + direction.y * 2)))
  84. {
  85. return points[position.x + direction.x * 2, position.y + direction.y * 2].hasConnection;
  86. }
  87. else
  88. {
  89. return false;
  90. }
  91.  
  92. }
  93.  
  94. // If all of the four neighbouring points has a connection, we are stuck
  95. bool IsStuck(Vector2Int position)
  96. {
  97. bool isStuck = true;
  98.  
  99. if (InsideBounds(position + Vector2Int.right * 2))
  100. {
  101. if (!points[position.x + 2, position.y].hasConnection)
  102. {
  103. isStuck = false;
  104. }
  105. }
  106.  
  107. if (InsideBounds(position + Vector2Int.left * 2))
  108. {
  109. if (!points[position.x - 2, position.y].hasConnection)
  110. {
  111. isStuck = false;
  112. }
  113. }
  114.  
  115. if (InsideBounds(position + Vector2Int.up * 2))
  116. {
  117. if (!points[position.x, position.y + 2].hasConnection)
  118. {
  119. isStuck = false;
  120. }
  121. }
  122.  
  123. if (InsideBounds(position + Vector2Int.down * 2))
  124. {
  125. if (!points[position.x, position.y - 2].hasConnection)
  126. {
  127. isStuck = false;
  128. }
  129. }
  130.  
  131. return isStuck;
  132. }
  133.  
  134. Vector2Int FindValidPosition()
  135. {
  136. foreach (var cell in points)
  137. {
  138. if (!IsStuck(cell.position))
  139. {
  140. foundValidPosition = true;
  141. return cell.position;
  142. }
  143. }
  144.  
  145. foundValidPosition = false;
  146. return Vector2Int.zero;
  147. }
  148.  
  149. void InitializePoints()
  150. {
  151. points = new Point[gridSize, gridSize];
  152.  
  153. for (int y = 0, i = 0; y < gridSize; y++)
  154. {
  155. for (int x = 0; x < gridSize; x++, i++)
  156. {
  157. points[x, y] = new Point
  158. {
  159. position = new Vector2Int(x, y),
  160. hasConnection = false
  161. };
  162. }
  163. }
  164. }
  165.  
  166. Vector2Int RandomDirection()
  167. {
  168. int random = UnityEngine.Random.Range(0, 4);
  169.  
  170. switch (random)
  171. {
  172. case 0: return Vector2Int.down;
  173. case 1: return Vector2Int.up;
  174. case 2: return Vector2Int.left;
  175. case 3: return Vector2Int.right;
  176. default: throw new ArgumentException();
  177. }
  178. }
  179. }
  180.  
  181. public struct Point
  182. {
  183. public Vector2Int position;
  184. public bool hasConnection;
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement