Files
aibis-dream/Assets/Scripts/MiniGame/BlockPuzzle/BlockPuzzleGrid.cs
T
2025-11-20 18:07:15 +08:00

215 lines
5.9 KiB
C#

using UnityEngine;
namespace AibisDream
{
/// <summary>
/// BlockPuzzle网格组件
/// 实现IBlockPuzzleGrid接口,管理整个游戏网格的状态和操作
/// </summary>
public class BlockPuzzleGrid : MonoBehaviour, IBlockPuzzleGrid
{
[Header("网格设置")]
[SerializeField] private int width = 10;
[SerializeField] private int height = 10;
[SerializeField] private float cellSize = 1f;
[SerializeField] private bool initializeOnStart = true;
private GridCellState[,] _grid;
private bool _isInitialized = false;
// IBlockPuzzleGrid 接口实现
public int Width { get; private set; }
public int Height { get; private set; }
public float CellSize { get; private set; }
public Vector3 Origin { get; private set; }
private void Start()
{
if (initializeOnStart)
{
Initialize(width, height, cellSize, transform.position);
}
}
/// <summary>
/// 初始化网格
/// </summary>
public void Initialize(int width, int height, float cellSize, Vector3 origin)
{
if (width <= 0 || height <= 0)
{
Debug.LogError($"BlockPuzzleGridComponent: 无效的网格尺寸 {width}x{height}");
return;
}
if (cellSize <= 0)
{
Debug.LogError($"BlockPuzzleGridComponent: 无效的单元格大小 {cellSize}");
return;
}
this.width = width;
this.height = height;
this.cellSize = cellSize;
Width = width;
Height = height;
CellSize = cellSize;
Origin = origin;
_grid = new GridCellState[width, height];
_isInitialized = true;
Clear();
}
/// <summary>
/// 使用Inspector中的参数初始化网格
/// </summary>
public void Initialize()
{
Initialize(width, height, cellSize, transform.position);
}
/// <summary>
/// 重新初始化网格(使用新的参数)
/// </summary>
public void Reinitialize(int newWidth, int newHeight, float newCellSize)
{
Initialize(newWidth, newHeight, newCellSize, transform.position);
}
public GridCellState GetCellState(int x, int y)
{
if (!_isInitialized)
{
Debug.LogWarning("BlockPuzzleGridComponent: 网格未初始化");
return GridCellState.Empty;
}
if (!IsValidPosition(x, y))
{
Debug.LogWarning($"BlockPuzzleGridComponent: 尝试访问无效位置 ({x}, {y})");
return GridCellState.Empty;
}
return _grid[x, y];
}
public void SetCellState(int x, int y, GridCellState state)
{
if (!_isInitialized)
{
Debug.LogWarning("BlockPuzzleGridComponent: 网格未初始化");
return;
}
if (!IsValidPosition(x, y))
{
Debug.LogWarning($"BlockPuzzleGridComponent: 尝试设置无效位置 ({x}, {y})");
return;
}
_grid[x, y] = state;
}
public bool IsValidPosition(int x, int y)
{
return _isInitialized && x >= 0 && x < Width && y >= 0 && y < Height;
}
public bool IsCellEmpty(int x, int y)
{
return IsValidPosition(x, y) && GetCellState(x, y) == GridCellState.Empty;
}
public bool WorldToGrid(Vector3 worldPos, out int gridX, out int gridY)
{
if (!_isInitialized)
{
gridX = 0;
gridY = 0;
return false;
}
Vector3 localPos = worldPos - Origin;
gridX = Mathf.FloorToInt(localPos.x / CellSize);
gridY = Mathf.FloorToInt(localPos.y / CellSize);
// 检查是否在有效范围内
if (!IsValidPosition(gridX, gridY))
{
return false;
}
return true;
}
public Vector3 GridToWorld(int gridX, int gridY)
{
if (!_isInitialized)
{
return transform.position;
}
// 返回单元格中心的世界坐标
float worldX = Origin.x + (gridX + 0.5f) * CellSize;
float worldY = Origin.y + (gridY + 0.5f) * CellSize;
return new Vector3(worldX, worldY, Origin.z);
}
public void Clear()
{
if (!_isInitialized || _grid == null) return;
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
_grid[x, y] = GridCellState.Empty;
}
}
}
public int GetOccupiedCount()
{
if (!_isInitialized || _grid == null) return 0;
int count = 0;
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
if (_grid[x, y] == GridCellState.Occupied)
{
count++;
}
}
}
return count;
}
public int GetTotalCellCount()
{
return Width * Height;
}
/// <summary>
/// 检查网格是否已初始化
/// </summary>
public bool IsInitialized()
{
return _isInitialized;
}
private void OnValidate()
{
// 确保参数有效
width = Mathf.Max(1, width);
height = Mathf.Max(1, height);
cellSize = Mathf.Max(0.1f, cellSize);
}
}
}