Files
BABA_YAGA/Assets/Scripts/GameSetup/Maze/Recursive.cs

41 lines
1.3 KiB
C#
Raw Normal View History

using Hallucinate.GameSetup.Maze.Extensions;
2026-03-27 22:42:43 +07:00
using UnityEngine;
namespace Hallucinate.GameSetup.Maze
2026-03-27 22:42:43 +07:00
{
/// <summary>
/// A recursive backtracker algorithm for maze generation.
/// It explores the grid randomly and backtracks when it reaches a dead end.
/// </summary>
public class Recursive : Maze
2026-03-27 22:42:43 +07:00
{
/// <summary>
/// Entry point for the recursive generation.
/// Starts from a fixed position (5, 5).
/// </summary>
public override void Generate()
{
Generate(5, 5);
}
2026-03-27 22:42:43 +07:00
/// <summary>
/// Internal recursive method that carves corridors by exploring neighbors in a random order.
/// </summary>
/// <param name="x">The current X coordinate.</param>
/// <param name="z">The current Z coordinate.</param>
protected void Generate(int x, int z)
{
if (CountSquareNeighbours(x, z) >= 2) return;
map[x, z] = Corridor;
2026-03-27 22:42:43 +07:00
directions.Shuffle();
2026-03-27 22:42:43 +07:00
Generate(x + directions[0].x, z + directions[0].z);
Generate(x + directions[1].x, z + directions[1].z);
Generate(x + directions[2].x, z + directions[2].z);
Generate(x + directions[3].x, z + directions[3].z);
}
2026-03-27 22:42:43 +07:00
}
}