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; }
- // Forms pathways
- public List<Connection> connections { get; private set; }
- bool foundValidPosition;
- void Start()
- {
- StartCoroutine(GenerateMaze());
- }
- void Update()
- {
- if (connections != null)
- {
- foreach (var connection in connections)
- {
- Debug.DrawLine((Vector2)connection.pointA.position, (Vector2)connection.pointB.position, Color.red);
- }
- }
- }
- public IEnumerator GenerateMaze()
- {
- InitializePoints();
- connections = new();
- Vector2Int position = entrance;
- foundValidPosition = true;
- while (foundValidPosition)
- {
- Vector2Int direction;
- while (true)
- {
- direction = RandomDirection();
- if (!InsideBounds(position + direction)) continue;
- if (!IsLooping(position, direction)) break;
- }
- connections.Add(new Connection
- {
- pointA = points[position.x, position.y],
- pointB = points[position.x + direction.x, position.y + direction.y]
- });
- points[position.x, position.y].hasConnection = true;
- points[position.x + direction.x, position.y + direction.y].hasConnection = true;
- position += direction;
- 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;
- }
- // If the point we are heading towards has a connection, a loop will be created
- bool IsLooping(Vector2Int position, Vector2Int direction)
- {
- return points[position.x + direction.x, position.y + direction.y].hasConnection;
- }
- // 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))
- {
- if (!points[position.x + 1, position.y].hasConnection)
- {
- isStuck = false;
- }
- }
- if (InsideBounds(position + Vector2Int.left))
- {
- if (!points[position.x - 1, position.y].hasConnection)
- {
- isStuck = false;
- }
- }
- if (InsideBounds(position + Vector2Int.up))
- {
- if (!points[position.x, position.y + 1].hasConnection)
- {
- isStuck = false;
- }
- }
- if (InsideBounds(position + Vector2Int.down))
- {
- if (!points[position.x, position.y - 1].hasConnection)
- {
- isStuck = false;
- }
- }
- return isStuck;
- }
- Vector2Int FindValidPosition()
- {
- foreach (var connection in connections)
- {
- if (!IsStuck(connection.pointA.position))
- {
- foundValidPosition = true;
- return connection.pointA.position;
- }
- if (!IsStuck(connection.pointB.position))
- {
- foundValidPosition = true;
- return connection.pointB.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;
- }
- public struct Connection
- {
- public Point pointA;
- public Point pointB;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement