149 lines
4.5 KiB
C#
149 lines
4.5 KiB
C#
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
/// <summary>
|
|
/// BlockPuzzle网格系统使用示例
|
|
/// 演示如何使用网格系统和可视化组件
|
|
/// </summary>
|
|
public class BlockPuzzleGridExample : MonoBehaviour
|
|
{
|
|
[Header("组件引用")]
|
|
[SerializeField] private BlockPuzzleGrid gridComponent;
|
|
[SerializeField] private GridVisualizer gridVisualizer;
|
|
|
|
[Header("测试设置")]
|
|
[SerializeField] private KeyCode testPlaceKey = KeyCode.Space;
|
|
[SerializeField] private KeyCode testClearKey = KeyCode.C;
|
|
|
|
private IBlockPuzzleGrid _grid;
|
|
|
|
private void Start()
|
|
{
|
|
// 初始化网格组件
|
|
if (gridComponent != null)
|
|
{
|
|
gridComponent.Initialize();
|
|
_grid = gridComponent; // 组件直接实现IBlockPuzzleGrid接口
|
|
}
|
|
|
|
// 设置可视化器
|
|
if (gridVisualizer != null && _grid != null)
|
|
{
|
|
gridVisualizer.SetGrid(_grid);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_grid == null) return;
|
|
|
|
// 测试:按空格键随机放置一个格子
|
|
if (Input.GetKeyDown(testPlaceKey))
|
|
{
|
|
TestPlaceRandomCell();
|
|
}
|
|
|
|
// 测试:按C键清空网格
|
|
if (Input.GetKeyDown(testClearKey))
|
|
{
|
|
TestClearGrid();
|
|
}
|
|
|
|
// 测试:鼠标点击放置格子
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
TestPlaceCellAtMouse();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试:在随机位置放置一个格子
|
|
/// </summary>
|
|
private void TestPlaceRandomCell()
|
|
{
|
|
if (_grid == null) return;
|
|
|
|
int x = Random.Range(0, _grid.Width);
|
|
int y = Random.Range(0, _grid.Height);
|
|
|
|
if (_grid.IsCellEmpty(x, y))
|
|
{
|
|
_grid.SetCellState(x, y, GridCellState.Occupied);
|
|
Debug.Log($"放置格子到位置 ({x}, {y})");
|
|
|
|
// 更新可视化
|
|
if (gridVisualizer != null)
|
|
{
|
|
gridVisualizer.UpdateCellVisual(x, y);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试:在鼠标位置放置格子
|
|
/// </summary>
|
|
private void TestPlaceCellAtMouse()
|
|
{
|
|
if (_grid == null) return;
|
|
|
|
// 获取鼠标世界坐标(需要根据你的相机设置调整)
|
|
Camera cam = Camera.main;
|
|
if (cam == null) return;
|
|
|
|
Vector3 mousePos = Input.mousePosition;
|
|
mousePos.z = cam.nearClipPlane + 1f; // 设置合适的Z距离
|
|
Vector3 worldPos = cam.ScreenToWorldPoint(mousePos);
|
|
|
|
// 转换为网格坐标
|
|
if (_grid.WorldToGrid(worldPos, out int gridX, out int gridY))
|
|
{
|
|
if (_grid.IsCellEmpty(gridX, gridY))
|
|
{
|
|
_grid.SetCellState(gridX, gridY, GridCellState.Occupied);
|
|
Debug.Log($"鼠标点击放置格子到位置 ({gridX}, {gridY})");
|
|
|
|
// 更新可视化
|
|
if (gridVisualizer != null)
|
|
{
|
|
gridVisualizer.UpdateCellVisual(gridX, gridY);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试:清空网格
|
|
/// </summary>
|
|
private void TestClearGrid()
|
|
{
|
|
if (_grid == null) return;
|
|
|
|
_grid.Clear();
|
|
Debug.Log("清空网格");
|
|
|
|
// 更新可视化
|
|
if (gridVisualizer != null)
|
|
{
|
|
gridVisualizer.UpdateAllCellsVisual();
|
|
}
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (_grid == null) return;
|
|
|
|
GUILayout.BeginArea(new Rect(10, 10, 300, 200));
|
|
GUILayout.Label($"网格尺寸: {_grid.Width} x {_grid.Height}");
|
|
GUILayout.Label($"单元格大小: {_grid.CellSize}");
|
|
GUILayout.Label($"已占用: {_grid.GetOccupiedCount()} / {_grid.GetTotalCellCount()}");
|
|
GUILayout.Space(10);
|
|
GUILayout.Label($"按 {testPlaceKey} 键:随机放置格子");
|
|
GUILayout.Label($"按 {testClearKey} 键:清空网格");
|
|
GUILayout.Label("鼠标左键:在鼠标位置放置格子");
|
|
GUILayout.EndArea();
|
|
}
|
|
}
|
|
}
|
|
|