Update
This commit is contained in:
75
Assets/Scripts/GameSetup/Maze/CrawlerAlgorithm.cs
Normal file
75
Assets/Scripts/GameSetup/Maze/CrawlerAlgorithm.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user