Update
This commit is contained in:
71
Assets/Scripts/GameSetup/Maze/MazeGrid.cs
Normal file
71
Assets/Scripts/GameSetup/Maze/MazeGrid.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
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>
|
||||
public class MazeGrid
|
||||
{
|
||||
public int Width { get; }
|
||||
public int Depth { get; }
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user