Files
BABA_YAGA/Assets/Scripts/GameSetup/Maze/Prims.cs
scove 3a687a4d58 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.
2026-04-21 21:44:26 +07:00

52 lines
1.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace Hallucinate.GameSetup.Maze
{
/// <summary>
/// Implements a simplified version of Prim's algorithm for maze generation.
/// It picks walls at random and converts them into corridors if they only have one corridor neighbor.
/// </summary>
public class Prims : Maze
{
/// <summary>
/// Generates the maze using Prim's algorithm logic.
/// </summary>
public override void Generate()
{
int x = 2;
int z = 2;
map[x, z] = Corridor;
List<MapLocation> walls = new List<MapLocation>
{
new MapLocation(x + 1, z),
new MapLocation(x - 1, z),
new MapLocation(x, z + 1),
new MapLocation(x, z - 1)
};
int countloops = 0;
while (walls.Count > 0 && countloops < 5000)
{
int rwall = Random.Range(0, walls.Count);
x = walls[rwall].x;
z = walls[rwall].z;
walls.RemoveAt(rwall);
if (CountSquareNeighbours(x, z) == 1)
{
map[x, z] = Corridor;
walls.Add(new MapLocation(x + 1, z));
walls.Add(new MapLocation(x - 1, z));
walls.Add(new MapLocation(x, z + 1));
walls.Add(new MapLocation(x, z - 1));
}
countloops++;
}
}
}
}