Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class MazeGenerator : MonoBehaviour
- {
- public int gridSize;
- public Vector2Int entrance;
- public Vector2Int exit;
- // Forms a grid
- public Point[,] points { get; private set; }
- bool foundValidPosition;
- void Start()
- {
- StartCoroutine(GenerateMaze());
- }
- void OnDrawGizmos()
- {
- if (points != null)
- {
- foreach (var cell in points)
- {
- if (cell.hasConnection)
- {
- Gizmos.color = Color.red;
- }
- else
- {
- Gizmos.color = Color.gray;
- }
- Gizmos.DrawCube((Vector2)cell.position, Vector2.one);
- }
- }
- }
- public IEnumerator GenerateMaze()
- {
- InitializePoints();
- Vector2Int position = entrance;
- foundValidPosition = true;
- while (foundValidPosition)
- {
- Vector2Int direction;
- while (true)
- {
- direction = RandomDirection();
- if (!InsideBounds(position + direction)) continue;
- if (!IsLooping(position, direction)) break;
- }
- points[position.x, position.y].hasConnection = true;
- points[position.x + direction.x, position.y + direction.y].hasConnection = true;
- position += direction * 2;
- if (IsStuck(position)) position = FindValidPosition();
- yield return new WaitForSeconds(.05f);
- }
- }
- // Checks if position is inside the bounds of grid
- public bool InsideBounds(Vector2Int position)
- {
- return position.x >= 0 && position.x < gridSize &&
- position.y >= 0 && position.y < gridSize;
- }
- bool IsLooping(Vector2Int position, Vector2Int direction)
- {
- if (InsideBounds(new Vector2Int(position.x + direction.x * 2, position.y + direction.y * 2)))
- {
- return points[position.x + direction.x * 2, position.y + direction.y * 2].hasConnection;
- }
- else
- {
- return false;
- }
- }
- // If all of the four neighbouring points has a connection, we are stuck
- bool IsStuck(Vector2Int position)
- {
- bool isStuck = true;
- if (InsideBounds(position + Vector2Int.right * 2))
- {
- if (!points[position.x + 2, position.y].hasConnection)
- {
- isStuck = false;
- }
- }
- if (InsideBounds(position + Vector2Int.left * 2))
- {
- if (!points[position.x - 2, position.y].hasConnection)
- {
- isStuck = false;
- }
- }
- if (InsideBounds(position + Vector2Int.up * 2))
- {
- if (!points[position.x, position.y + 2].hasConnection)
- {
- isStuck = false;
- }
- }
- if (InsideBounds(position + Vector2Int.down * 2))
- {
- if (!points[position.x, position.y - 2].hasConnection)
- {
- isStuck = false;
- }
- }
- return isStuck;
- }
- Vector2Int FindValidPosition()
- {
- foreach (var cell in points)
- {
- if (!IsStuck(cell.position))
- {
- foundValidPosition = true;
- return cell.position;
- }
- }
- foundValidPosition = false;
- return Vector2Int.zero;
- }
- void InitializePoints()
- {
- points = new Point[gridSize, gridSize];
- for (int y = 0, i = 0; y < gridSize; y++)
- {
- for (int x = 0; x < gridSize; x++, i++)
- {
- points[x, y] = new Point
- {
- position = new Vector2Int(x, y),
- hasConnection = false
- };
- }
- }
- }
- Vector2Int RandomDirection()
- {
- int random = UnityEngine.Random.Range(0, 4);
- switch (random)
- {
- case 0: return Vector2Int.down;
- case 1: return Vector2Int.up;
- case 2: return Vector2Int.left;
- case 3: return Vector2Int.right;
- default: throw new ArgumentException();
- }
- }
- }
- public struct Point
- {
- public Vector2Int position;
- public bool hasConnection;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement