IEnumerator FindPath(Vector3 beginPos, Vector3 targetPos) { Node startNode = grid.GetNodeFromWorldPoint(beginPos); Node endNode = grid.GetNodeFromWorldPoint(targetPos); Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; if (startNode.walkable && endNode.walkable && startNode != endNode) { Heap <Node> openSet = new Heap <Node>(grid.NodesInGrid); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == endNode) { pathSuccess = true; break; } foreach (Node neighbour in grid.GetNeighboursOfNode(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int costToNeighbour = currentNode.gCost + grid.GetDistanceBetweenNodes(currentNode, neighbour); if (costToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = costToNeighbour; neighbour.hCost = grid.GetDistanceBetweenNodes(neighbour, endNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } yield return(null); if (pathSuccess) { waypoints = RetracePath(startNode, endNode); } requestManager.FinishedProcessingPath(waypoints, pathSuccess); }
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, NodeGrid grid) { Vector3[] waypoints = new Vector3[0]; bool pathSucces = false; if (grid != null) { Node startNode = grid.GetNodeFromWorldPoint(startPos); Node targetNode = grid.GetNodeFromWorldPoint(targetPos); if (/*startNode.walkable && */ targetNode.walkable) { NodeHeap openSet = new NodeHeap(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { pathSucces = true; break; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } yield return(null); if (pathSucces) { waypoints = RetracePath(startNode, targetNode, grid); pathSucces = (waypoints.Length > 0); } requestManager.FinishProcessingPath(waypoints, pathSucces); } }