IndieGame/client/Assets/Scripts/System/AStar/AStarGrid.cs

56 lines
1.3 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ether
{
public class AStarGrid
{
private int width;
private int height;
private AStarNode[,] gridNode;
/// <summary>
/// 构造函数初始化节点范围数组
/// </summary>
public AStarGrid(int gridWidth, int gridHeight)
{
SetData(gridWidth, gridHeight);
}
private void SetData(int gridWidth, int gridHeight)
{
this.width = gridWidth;
this.height = gridHeight;
gridNode = new AStarNode[gridWidth, gridHeight];
for (int x = 0; x < gridWidth; x++)
{
for (int y = 0; y < gridHeight; y++)
{
gridNode[x, y] = new AStarNode(new Vector2Int(x, y));
}
}
}
public AStarNode GetGridNode(int xPos, int yPos)
{
if (xPos < width && yPos < height)
{
return gridNode[xPos, yPos];
}
Debug.Log("超出寻路网格范围");
return null;
}
public void Reset(int gridWidth, int gridHeight)
{
SetData(gridWidth, gridHeight);
}
}
}