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

52 lines
1.5 KiB
C#
Raw Normal View History

2026-03-27 22:42:43 +07:00
using System.Collections.Generic;
using UnityEngine;
namespace Hallucinate.GameSetup.Maze
2026-03-27 22:42:43 +07:00
{
/// <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
2026-03-27 22:42:43 +07:00
{
/// <summary>
/// Generates the maze using Prim's algorithm logic.
/// </summary>
public override void Generate()
{
int x = 2;
int z = 2;
2026-03-27 22:42:43 +07:00
map[x, z] = Corridor;
2026-03-27 22:42:43 +07:00
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)
};
2026-03-27 22:42:43 +07:00
int countloops = 0;
while (walls.Count > 0 && countloops < 5000)
2026-03-27 22:42:43 +07:00
{
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));
}
2026-03-27 22:42:43 +07:00
countloops++;
}
2026-03-27 22:42:43 +07:00
}
}
}