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

360 lines
12 KiB
C#
Raw Normal View History

2026-04-21 23:28:49 +07:00
using System.Collections;
using System.Collections.Generic;
2026-06-09 02:05:00 +07:00
using Sirenix.OdinInspector;
2026-04-21 23:28:49 +07:00
using UnityEngine;
namespace Hallucinate.GameSetup.Maze
{
/// <summary>
/// Central controller for the Maze system.
/// Manages algorithm selection, debug speed, and regeneration.
/// </summary>
public class MazeManager : MonoBehaviour
{
2026-06-09 22:48:04 +07:00
public enum AlgorithmType { Recursive, Wilsons, Prims, Crawler, NoiseRecursive }
2026-04-21 23:28:49 +07:00
2026-06-09 02:05:00 +07:00
[BoxGroup("Generation")]
[InfoBox("Set the array size to control how many maze floors are generated. Runtime grid data is rebuilt when Regenerate runs.")]
[ValidateInput(nameof(HasAtLeastOneFloor), "Maze Manager needs at least one floor.")]
2026-05-03 06:49:11 +07:00
public MazeGrid[] mazes;
2026-06-09 02:05:00 +07:00
[BoxGroup("Generation")]
[MinValue(0.001f)]
2026-05-03 06:49:11 +07:00
public float floorHeight = 3.5f;
2026-06-09 02:05:00 +07:00
[BoxGroup("Generation")]
[MinValue(0)]
2026-05-03 06:49:11 +07:00
public int connectionsPerFloor = 2;
2026-06-09 02:05:00 +07:00
[BoxGroup("Generation")]
2026-04-21 23:28:49 +07:00
[SerializeField] private AlgorithmType selectedAlgorithm;
2026-06-09 02:05:00 +07:00
[BoxGroup("Generation/Grid Size")]
[PropertyRange(5, 200)]
2026-04-21 23:28:49 +07:00
[SerializeField] private int width = 30;
2026-06-09 02:05:00 +07:00
[BoxGroup("Generation/Grid Size")]
[PropertyRange(5, 200)]
2026-04-21 23:28:49 +07:00
[SerializeField] private int depth = 30;
2026-06-26 00:21:29 +07:00
[BoxGroup("Animation")]
[SerializeField] private bool animateGeneration = true;
2026-06-09 02:05:00 +07:00
2026-06-26 00:21:29 +07:00
[BoxGroup("Animation")]
[ShowIf(nameof(animateGeneration))]
[PropertyRange(1, 500)]
[LabelText("Generation Speed (Cells/Frame)")]
[SerializeField] private int generationSpeed = 50;
public static int cellsProcessedThisFrame;
[BoxGroup("Animation")]
[ShowIf(nameof(animateGeneration))]
public MazeRenderer.CellAnimationType cellAnimationType = MazeRenderer.CellAnimationType.ScaleUp;
[BoxGroup("Progress")]
[ProgressBar(0, 100)]
[ShowInInspector, ReadOnly]
private float completionPercentage;
2026-04-21 23:28:49 +07:00
2026-06-09 02:05:00 +07:00
[BoxGroup("References")]
[Required]
2026-04-21 23:28:49 +07:00
[SerializeField] private MazeRenderer mazeRenderer;
2026-06-09 02:05:00 +07:00
2026-06-26 02:04:50 +07:00
[BoxGroup("Rooms (Phase 2)")]
public bool generateRooms = true;
[BoxGroup("Rooms (Phase 2)")]
[ShowIf(nameof(generateRooms))]
public int numberOfRooms = 2;
[BoxGroup("Rooms (Phase 2)")]
[ShowIf(nameof(generateRooms))]
public Vector2Int minRoomSize = new Vector2Int(2, 2);
[BoxGroup("Rooms (Phase 2)")]
[ShowIf(nameof(generateRooms))]
public Vector2Int maxRoomSize = new Vector2Int(4, 4);
2026-06-09 02:05:00 +07:00
[BoxGroup("References")]
[Required]
2026-04-21 23:28:49 +07:00
[SerializeField] private Transform mazeContainer;
2026-06-09 02:05:00 +07:00
[FoldoutGroup("Manhole Prefabs")]
2026-05-03 06:49:11 +07:00
public GameObject straightManHoleLadder;
2026-06-09 02:05:00 +07:00
[FoldoutGroup("Manhole Prefabs")]
2026-05-03 06:49:11 +07:00
public GameObject straightManHoleUp;
2026-06-09 02:05:00 +07:00
[FoldoutGroup("Manhole Prefabs")]
2026-05-03 06:49:11 +07:00
public GameObject deadendManHoleLadder;
2026-06-09 02:05:00 +07:00
[FoldoutGroup("Manhole Prefabs")]
2026-05-03 06:49:11 +07:00
public GameObject deadendManHoleUp;
2026-06-09 02:05:00 +07:00
[ShowInInspector]
[ReadOnly]
[BoxGroup("Runtime")]
private int FloorCount => mazes?.Length ?? 0;
[ShowInInspector]
[ReadOnly]
[BoxGroup("Runtime")]
private string CurrentGrid => _grid == null ? "None" : $"{_grid.Width}x{_grid.Depth}, Level {_grid.Level}";
2026-05-03 06:49:11 +07:00
2026-04-21 23:28:49 +07:00
private MazeGrid _grid;
private Coroutine _generationCoroutine;
2026-06-26 00:21:29 +07:00
private HashSet<Vector3Int> _modifiedCells = new HashSet<Vector3Int>();
2026-04-21 23:28:49 +07:00
private void Start()
{
Regenerate();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
Regenerate();
}
}
2026-06-26 00:21:29 +07:00
[ContextMenu("Clear Maze")]
[Button("Clear Maze", ButtonSizes.Large)]
public void ClearMaze()
{
if (_generationCoroutine != null)
{
StopCoroutine(_generationCoroutine);
_generationCoroutine = null;
}
if (mazeRenderer != null)
{
mazeRenderer.Clear();
}
completionPercentage = 0f;
_modifiedCells?.Clear();
}
2026-04-21 23:28:49 +07:00
[ContextMenu("Regenerate")]
2026-06-09 02:05:00 +07:00
[Button("Regenerate Maze", ButtonSizes.Large)]
2026-04-21 23:28:49 +07:00
public void Regenerate()
{
2026-06-09 02:05:00 +07:00
if (mazeRenderer == null)
{
Debug.LogError("MazeManager needs a MazeRenderer reference before regenerating.", this);
return;
}
if (mazeContainer == null)
{
Debug.LogError("MazeManager needs a maze container reference before regenerating.", this);
return;
}
if (mazes == null || mazes.Length == 0)
{
mazes = new MazeGrid[1];
}
2026-06-26 00:21:29 +07:00
ClearMaze();
mazeRenderer.currentAnimationType = cellAnimationType;
if (animateGeneration)
2026-05-06 01:47:40 +07:00
{
2026-06-26 00:21:29 +07:00
_generationCoroutine = StartCoroutine(GenerateMazeRoutine());
}
else
{
GenerateMazeInstant();
2026-05-06 01:47:40 +07:00
}
2026-06-26 00:21:29 +07:00
}
2026-05-06 01:47:40 +07:00
2026-06-26 00:21:29 +07:00
private void GenerateMazeInstant()
{
_modifiedCells.Clear();
completionPercentage = 0f;
2026-05-06 01:47:40 +07:00
2026-05-03 06:49:11 +07:00
for (int i = 0; i < mazes.Length; i++)
{
2026-05-06 01:47:40 +07:00
mazes[i] = new MazeGrid(width, depth);
mazes[i].Level = i;
2026-06-26 02:04:50 +07:00
CarveRooms(mazes[i]);
2026-05-06 01:47:40 +07:00
IMazeAlgorithm algorithmForFloor = GetAlgorithm(selectedAlgorithm);
algorithmForFloor.Generate(mazes[i]);
2026-05-03 06:49:11 +07:00
}
2026-06-26 00:21:29 +07:00
GenerateConnections();
for (int i = 0; i < mazes.Length; i++)
{
mazeRenderer.Initialize(mazes[i], mazeContainer, i == 0);
}
if (mazes.Length > 0) _grid = mazes[0];
completionPercentage = 100f;
}
private IEnumerator GenerateMazeRoutine()
{
_modifiedCells.Clear();
completionPercentage = 0f;
for (int i = 0; i < mazes.Length; i++)
{
mazes[i] = new MazeGrid(width, depth);
mazes[i].Level = i;
int floorIndex = i;
mazes[i].OnCellChanged += (x, z, type) =>
{
if (type != MazeCellType.Wall)
{
_modifiedCells.Add(new Vector3Int(x, floorIndex, z));
int totalCells = width * depth * mazes.Length;
// Approximate the progress to reach roughly 100% since algorithms don't visit all cells
float fillRatio = 0.6f;
completionPercentage = Mathf.Clamp((_modifiedCells.Count / ((float)totalCells * fillRatio)) * 100f, 0, 99f);
}
};
2026-06-26 02:04:50 +07:00
CarveRooms(mazes[i]);
2026-06-26 00:21:29 +07:00
mazeRenderer.Initialize(mazes[i], mazeContainer, i == 0);
IMazeAlgorithm algorithmForFloor = GetAlgorithm(selectedAlgorithm);
cellsProcessedThisFrame = 0;
yield return StartCoroutine(algorithmForFloor.GenerateStepByStep(mazes[i], generationSpeed));
}
GenerateConnections();
if (mazes.Length > 0) _grid = mazes[0];
completionPercentage = 100f;
_generationCoroutine = null;
}
private void GenerateConnections()
{
2026-05-06 01:47:40 +07:00
// Step 2: Create connections between adjacent floors
2026-05-03 06:49:11 +07:00
for (int i = 0; i < mazes.Length - 1; i++)
{
MazeGrid currentFloor = mazes[i];
MazeGrid nextFloor = mazes[i + 1];
List<Vector2Int> possibleConnections = new List<Vector2Int>();
for (int z = 0; z < depth; z++)
{
for (int x = 0; x < width; x++)
{
2026-05-06 01:47:40 +07:00
// Check if both floors have a corridor at this position
bool isCurrentFloorPath = currentFloor.GetCell(x, z) == MazeCellType.Corridor;
bool isNextFloorPath = nextFloor.GetCell(x, z) == MazeCellType.Corridor;
2026-05-03 06:49:11 +07:00
if (isCurrentFloorPath && isNextFloorPath)
{
possibleConnections.Add(new Vector2Int(x, z));
}
}
}
ShuffleList(possibleConnections);
2026-05-06 01:47:40 +07:00
int connectionsMade = 0;
2026-05-03 06:49:11 +07:00
foreach (Vector2Int pos in possibleConnections)
{
2026-05-06 01:47:40 +07:00
if (connectionsMade >= connectionsPerFloor) break;
2026-05-03 06:49:11 +07:00
int x = pos.x;
int z = pos.y;
2026-05-06 01:47:40 +07:00
// Set stair cells
2026-05-08 09:50:34 +07:00
currentFloor.SetCell(x, z, MazeCellType.StairsUp);
nextFloor.SetCell(x, z, MazeCellType.StairsDown);
2026-05-03 06:49:11 +07:00
connectionsMade++;
}
}
2026-04-21 23:28:49 +07:00
}
2026-06-26 00:21:29 +07:00
2026-06-26 02:04:50 +07:00
private void CarveRooms(MazeGrid grid)
{
if (!generateRooms) return;
for (int i = 0; i < numberOfRooms; i++)
{
int w = Random.Range(minRoomSize.x, maxRoomSize.x + 1);
int d = Random.Range(minRoomSize.y, maxRoomSize.y + 1);
int startX = Random.Range(1, width - w - 1);
int startZ = Random.Range(1, depth - d - 1);
for (int x = startX; x < startX + w; x++)
{
for (int z = startZ; z < startZ + d; z++)
{
grid.SetCell(x, z, MazeCellType.Room);
}
}
// Carve guaranteed door to seed pathfinding
if (Random.value > 0.5f)
{
int doorX = Random.Range(startX, startX + w);
int doorZ = Random.value > 0.5f ? startZ + d : startZ - 1;
grid.SetCell(doorX, doorZ, MazeCellType.Corridor);
}
else
{
int doorX = Random.value > 0.5f ? startX + w : startX - 1;
int doorZ = Random.Range(startZ, startZ + d);
grid.SetCell(doorX, doorZ, MazeCellType.Corridor);
}
}
}
2026-05-03 06:49:11 +07:00
private void ShuffleList<T>(List<T> list)
{
for (int i = 0; i < list.Count; i++)
{
T temp = list[i];
int randomIndex = Random.Range(i, list.Count);
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
2026-04-21 23:28:49 +07:00
private IMazeAlgorithm GetAlgorithm(AlgorithmType type)
{
return type switch
{
AlgorithmType.Recursive => new RecursiveAlgorithm(),
AlgorithmType.Wilsons => new WilsonsAlgorithm(),
AlgorithmType.Prims => new PrimsAlgorithm(),
AlgorithmType.Crawler => new CrawlerAlgorithm(),
2026-06-09 22:48:04 +07:00
AlgorithmType.NoiseRecursive => new NoiseRecursiveGenerator(),
2026-04-21 23:28:49 +07:00
_ => new RecursiveAlgorithm()
};
}
2026-06-09 02:05:00 +07:00
[Button("Find Scene References")]
private void FindSceneReferences()
{
if (mazeRenderer == null)
{
mazeRenderer = GetComponentInChildren<MazeRenderer>();
}
if (mazeContainer == null)
{
mazeContainer = transform;
}
}
private bool HasAtLeastOneFloor(MazeGrid[] floors)
{
return floors != null && floors.Length > 0;
}
2026-04-21 23:28:49 +07:00
}
}