2026-04-21 23:28:49 +07:00
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Hallucinate.GameSetup.Maze
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Holds the logical state of the maze grid.
|
|
|
|
|
/// Notifies listeners whenever a cell changes to trigger visual updates.
|
|
|
|
|
/// </summary>
|
2026-05-06 01:47:40 +07:00
|
|
|
[Serializable]
|
2026-04-21 23:28:49 +07:00
|
|
|
public class MazeGrid
|
|
|
|
|
{
|
2026-05-06 01:47:40 +07:00
|
|
|
public int Width { get; set; }
|
|
|
|
|
public int Depth { get; set; }
|
2026-05-03 06:49:11 +07:00
|
|
|
public int Level { get; set; }
|
2026-04-21 23:28:49 +07:00
|
|
|
|
|
|
|
|
private readonly MazeCellType[,] _cells;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event fired when a cell's type is changed.
|
|
|
|
|
/// Useful for the Renderer to trigger animations/FX.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<int, int, MazeCellType> OnCellChanged;
|
|
|
|
|
|
|
|
|
|
public MazeGrid(int width, int depth)
|
|
|
|
|
{
|
|
|
|
|
Width = width;
|
|
|
|
|
Depth = depth;
|
|
|
|
|
_cells = new MazeCellType[width, depth];
|
|
|
|
|
|
|
|
|
|
// Initialize all as walls
|
|
|
|
|
for (int z = 0; z < depth; z++)
|
|
|
|
|
for (int x = 0; x < width; x++)
|
|
|
|
|
_cells[x, z] = MazeCellType.Wall;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetCell(int x, int z, MazeCellType type)
|
|
|
|
|
{
|
|
|
|
|
if (IsInBounds(x, z))
|
|
|
|
|
{
|
|
|
|
|
if (_cells[x, z] != type)
|
|
|
|
|
{
|
|
|
|
|
_cells[x, z] = type;
|
|
|
|
|
OnCellChanged?.Invoke(x, z, type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MazeCellType GetCell(int x, int z)
|
|
|
|
|
{
|
|
|
|
|
if (IsInBounds(x, z))
|
|
|
|
|
return _cells[x, z];
|
|
|
|
|
return MazeCellType.Wall; // Treat out of bounds as walls
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsInBounds(int x, int z)
|
|
|
|
|
{
|
|
|
|
|
return x >= 0 && x < Width && z >= 0 && z < Depth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int CountSquareNeighbours(int x, int z, MazeCellType targetType)
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
if (x <= 0 || x >= Width - 1 || z <= 0 || z >= Depth - 1) return 5;
|
|
|
|
|
|
|
|
|
|
if (GetCell(x - 1, z) == targetType) count++;
|
|
|
|
|
if (GetCell(x + 1, z) == targetType) count++;
|
|
|
|
|
if (GetCell(x, z + 1) == targetType) count++;
|
|
|
|
|
if (GetCell(x, z - 1) == targetType) count++;
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|