Move Maze-related scripts into the Hallucinate.GameSetup.Maze namespace and perform a broad refactor and cleanup. Make MapLocation a readonly struct, add Corridor/Wall/Path constants, and convert Maze into a clearer base class with serialized fields (width, depth, scale, mapParentObject), proper initialization, virtual Generate, and safer DrawMap behavior. Update neighbor-count helpers, tighten method visibility, and improve algorithm implementations (Crawler, Prims, Recursive, Wilsons) to use the new constants and more robust logic (including logging and loop guards). Add a Fisher–Yates Shuffle extension with a static RNG under Hallucinate.GameSetup.Maze.Extensions. Also update IDE metadata (.idea encodings.xml and workspace.xml) to record file encodings and some project settings.
41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using Hallucinate.GameSetup.Maze.Extensions;
|
|
using UnityEngine;
|
|
|
|
namespace Hallucinate.GameSetup.Maze
|
|
{
|
|
/// <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
|
|
{
|
|
/// <summary>
|
|
/// Entry point for the recursive generation.
|
|
/// Starts from a fixed position (5, 5).
|
|
/// </summary>
|
|
public override void Generate()
|
|
{
|
|
Generate(5, 5);
|
|
}
|
|
|
|
/// <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;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|