76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
|
|
using System.Collections;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace Hallucinate.GameSetup.Maze
|
||
|
|
{
|
||
|
|
public class CrawlerAlgorithm : IMazeAlgorithm
|
||
|
|
{
|
||
|
|
private const int CrawlChance = 50;
|
||
|
|
private const int MinBoundary = 1;
|
||
|
|
|
||
|
|
public void Generate(MazeGrid grid)
|
||
|
|
{
|
||
|
|
CrawlV(grid, 0);
|
||
|
|
CrawlH(grid, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
public IEnumerator GenerateStepByStep(MazeGrid grid, float interval)
|
||
|
|
{
|
||
|
|
yield return CrawlV(grid, interval);
|
||
|
|
yield return CrawlH(grid, interval);
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator CrawlV(MazeGrid grid, float interval)
|
||
|
|
{
|
||
|
|
bool done = false;
|
||
|
|
int x = Random.Range(MinBoundary, grid.Width - MinBoundary);
|
||
|
|
int z = MinBoundary;
|
||
|
|
|
||
|
|
while (!done)
|
||
|
|
{
|
||
|
|
grid.SetCell(x, z, MazeCellType.Processing);
|
||
|
|
if (interval > 0) yield return new WaitForSeconds(interval);
|
||
|
|
|
||
|
|
grid.SetCell(x, z, MazeCellType.Corridor);
|
||
|
|
|
||
|
|
if (Random.Range(0, 100) < CrawlChance)
|
||
|
|
{
|
||
|
|
x += Random.Range(-1, 2);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
z += Random.Range(0, 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
done |= (x < MinBoundary || x >= grid.Width - MinBoundary || z < MinBoundary || z >= grid.Depth - MinBoundary);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator CrawlH(MazeGrid grid, float interval)
|
||
|
|
{
|
||
|
|
bool done = false;
|
||
|
|
int x = MinBoundary;
|
||
|
|
int z = Random.Range(MinBoundary, grid.Depth - MinBoundary);
|
||
|
|
|
||
|
|
while (!done)
|
||
|
|
{
|
||
|
|
grid.SetCell(x, z, MazeCellType.Processing);
|
||
|
|
if (interval > 0) yield return new WaitForSeconds(interval);
|
||
|
|
|
||
|
|
grid.SetCell(x, z, MazeCellType.Corridor);
|
||
|
|
|
||
|
|
if (Random.Range(0, 100) < CrawlChance)
|
||
|
|
{
|
||
|
|
x += Random.Range(0, 2);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
z += Random.Range(-1, 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
done |= (x < MinBoundary || x >= grid.Width - MinBoundary || z < MinBoundary || z >= grid.Depth - MinBoundary);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|