-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimGenerator.cs
58 lines (46 loc) · 2 KB
/
PrimGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Prim's Algorithm. Inheritance from MazeGenerator.
/// </summary>
public sealed class PrimGenerator : MazeGenerator
{
protected override void GenerateMaze() => StartCoroutine(GenerateMazeCoroutine(false));
protected override IEnumerator GenerateMazeCoroutine(bool isSlowly = true)
{
// Initialize the visitedCells array and the cell stack
VisitedCells = new bool[width, height];
CellStack = new Stack<Vector3Int>();
SetupMaze();
// Choose a random starting cell and mark it as visited
var startPosition = FindAndSetStartPosition();
VisitedCells[startPosition.x, startPosition.y] = true;
// Add the starting cell to the cell stack
CellStack.Push(startPosition);
// Loop until all cells have been visited
while (!AllTilesVisited())
{
if (isSlowly)
{
yield return new WaitForSeconds(waitTime); // Pause execution and resume
tileChangeSound.Play();
}
// Get the current cell from the top of the stack
var currentCell = CellStack.Peek();
// Get the unvisited neighbors of the current cell
var unvisitedNeighbors = GetUnvisitedNeighbors(currentCell);
if (unvisitedNeighbors.Count > 0)
{
var randomNeighbor = unvisitedNeighbors[Random.Range(0, unvisitedNeighbors.Count)];
VisitedCells[randomNeighbor.x, randomNeighbor.y] = true;
// Add the neighbor to the maze by updating the tile colors
UpdateTileColor(currentCell, randomNeighbor);
// Add the neighbor to the cell stack
CellStack.Push(randomNeighbor);
}
else CellStack.Pop(); // If all neighbors have been visited, remove the current cell from the stack
}
CreateEntranceAndExit();
}
}