Refactor maze scripts: namespaces & cleanup

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.
This commit is contained in:
2026-04-21 21:44:26 +07:00
parent d2e3dbf653
commit 3a687a4d58
8 changed files with 460 additions and 286 deletions

View File

@@ -1,25 +1,40 @@
using System.Collections;
using System.Collections.Generic;
using Hallucinate.GameSetup.Maze.Extensions;
using UnityEngine;
public class Recursive : Maze
namespace Hallucinate.GameSetup.Maze
{
public override void Generate()
/// <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
{
Generate(5, 5);
/// <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);
}
}
void Generate(int x, int z)
{
if (CountSquareNeighbours(x, z) >= 2) return;
map[x, z] = 0;
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);
}
}