protected void AddNodeToCloseList(AStarNode node) { node.astarInListType = AStarNodeInListType.Close_List; handledDict[node.pos] = node; }
public List <Vector2Int> Find(Vector2Int startPoint, Vector2Int goalPoint) { Reset(); // 为起点赋初值 AStarNode startNode = PoolCatManagerUtil.Spawn <AStarNode>(null, astarNode => astarNode.Init(startPoint.x, startPoint.y)); startNode.h = GetH(startPoint, goalPoint); startNode.f = startNode.h + startNode.g; AddNodeToOpenList(startNode); while (openHeap.Size > 0) { // 寻找开启列表中F值最低的格子。我们称它为当前格 AStarNode checkNode = openHeap.Pop(); // 把目标格添加进了开启列表,这时候路径被找到 if (checkNode.pos.Equals(goalPoint)) { return(Solve(checkNode)); } // 获得当前附近的节点集合 SetNeighborList(checkNode.pos); foreach (var neighborNode in neighborList) { float neighborG = checkNode.g + GetG(checkNode.pos, neighborNode.pos); if (handledDict.ContainsKey(neighborNode.pos)) { var oldNeighborNode = handledDict[neighborNode.pos]; if (neighborG < oldNeighborNode.g) { switch (handledDict[neighborNode.pos].astarInListType) { case AStarNodeInListType.Close_List: neighborNode.parent = checkNode; neighborNode.g = neighborG; neighborNode.h = GetH(neighborNode.pos, goalPoint); neighborNode.f = neighborNode.g + neighborNode.h; //更新neighbor_node的值 AddNodeToOpenList(neighborNode); oldNeighborNode.Despawn(); break; case AStarNodeInListType.Open_List: neighborNode.parent = checkNode; neighborNode.g = neighborG; neighborNode.h = GetH(neighborNode.pos, goalPoint); neighborNode.f = neighborNode.g + neighborNode.h; //更新neighbor_node的值 openHeap.Remove(oldNeighborNode); AddNodeToOpenList(neighborNode); oldNeighborNode.Despawn(); break; } } else { //舍弃的进行回收 neighborNode.Despawn(); } } else { neighborNode.parent = checkNode; neighborNode.g = neighborG; neighborNode.h = GetH(neighborNode.pos, goalPoint); neighborNode.f = neighborNode.g + neighborNode.h; AddNodeToOpenList(neighborNode); // 排序插入 } } // 把当前格切换到关闭列表 AddNodeToCloseList(checkNode); } return(null); }
protected void AddNodeToOpenList(AStarNode node) { node.astarInListType = AStarNodeInListType.Open_List; openHeap.Push(node); handledDict[node.pos] = node; }
public static int Compare(AStarNode data1, AStarNode data2) { float value = data1.f - data2.f; return(value == 0 ? 0 : value < 0 ? -1 : 1); }