Update
This commit is contained in:
95
Assets/Scripts/GameSetup/Maze/PrimsAlgorithm.cs
Normal file
95
Assets/Scripts/GameSetup/Maze/PrimsAlgorithm.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Hallucinate.GameSetup.Maze
|
||||
{
|
||||
public class PrimsAlgorithm : IMazeAlgorithm
|
||||
{
|
||||
private const int InitialX = 2;
|
||||
private const int InitialZ = 2;
|
||||
private const int MaxIterations = 10000;
|
||||
private const int TargetCorridorNeighbours = 1;
|
||||
|
||||
public void Generate(MazeGrid grid)
|
||||
{
|
||||
int x = InitialX;
|
||||
int z = InitialZ;
|
||||
grid.SetCell(x, z, MazeCellType.Corridor);
|
||||
|
||||
List<MapLocation> walls = GetNeighbouringWalls(grid, x, z);
|
||||
|
||||
int iterations = 0;
|
||||
while (walls.Count > 0 && iterations < MaxIterations)
|
||||
{
|
||||
int rIndex = Random.Range(0, walls.Count);
|
||||
MapLocation w = walls[rIndex];
|
||||
walls.RemoveAt(rIndex);
|
||||
|
||||
if (grid.CountSquareNeighbours(w.x, w.z, MazeCellType.Corridor) == TargetCorridorNeighbours)
|
||||
{
|
||||
grid.SetCell(w.x, w.z, MazeCellType.Corridor);
|
||||
walls.AddRange(GetNeighbouringWalls(grid, w.x, w.z));
|
||||
}
|
||||
iterations++;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator GenerateStepByStep(MazeGrid grid, float interval)
|
||||
{
|
||||
int x = InitialX;
|
||||
int z = InitialZ;
|
||||
grid.SetCell(x, z, MazeCellType.Corridor);
|
||||
yield return new WaitForSeconds(interval);
|
||||
|
||||
List<MapLocation> walls = GetNeighbouringWalls(grid, x, z);
|
||||
|
||||
foreach(var w in walls) grid.SetCell(w.x, w.z, MazeCellType.Processing);
|
||||
|
||||
int iterations = 0;
|
||||
while (walls.Count > 0 && iterations < MaxIterations)
|
||||
{
|
||||
int rIndex = Random.Range(0, walls.Count);
|
||||
MapLocation w = walls[rIndex];
|
||||
walls.RemoveAt(rIndex);
|
||||
|
||||
if (grid.CountSquareNeighbours(w.x, w.z, MazeCellType.Corridor) == TargetCorridorNeighbours)
|
||||
{
|
||||
grid.SetCell(w.x, w.z, MazeCellType.Corridor);
|
||||
if (interval > 0) yield return new WaitForSeconds(interval);
|
||||
|
||||
var newWalls = GetNeighbouringWalls(grid, w.x, w.z);
|
||||
foreach(var nw in newWalls)
|
||||
{
|
||||
if (grid.GetCell(nw.x, nw.z) == MazeCellType.Wall)
|
||||
{
|
||||
grid.SetCell(nw.x, nw.z, MazeCellType.Processing);
|
||||
walls.Add(nw);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(grid.GetCell(w.x, w.z) == MazeCellType.Processing)
|
||||
grid.SetCell(w.x, w.z, MazeCellType.Wall);
|
||||
}
|
||||
iterations++;
|
||||
}
|
||||
}
|
||||
|
||||
private List<MapLocation> GetNeighbouringWalls(MazeGrid grid, int x, int z)
|
||||
{
|
||||
List<MapLocation> neighbours = new List<MapLocation>();
|
||||
foreach (var dir in MapLocation.Directions)
|
||||
{
|
||||
int nx = x + dir.x;
|
||||
int nz = z + dir.z;
|
||||
if (grid.IsInBounds(nx, nz) && grid.GetCell(nx, nz) == MazeCellType.Wall)
|
||||
{
|
||||
neighbours.Add(new MapLocation(nx, nz));
|
||||
}
|
||||
}
|
||||
return neighbours;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user