using Hallucinate.GameSetup.Maze.Extensions; using UnityEngine; namespace Hallucinate.GameSetup.Maze { /// /// A recursive backtracker algorithm for maze generation. /// It explores the grid randomly and backtracks when it reaches a dead end. /// public class Recursive : Maze { /// /// Entry point for the recursive generation. /// Starts from a fixed position (5, 5). /// public override void Generate() { Generate(5, 5); } /// /// Internal recursive method that carves corridors by exploring neighbors in a random order. /// /// The current X coordinate. /// The current Z coordinate. protected void Generate(int x, int z) { if (CountSquareNeighbours(x, z) >= 2) return; map[x, z] = Corridor; directions.Shuffle(); 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); } } }