public void GetOutdoorPath(Vector3 pos, OnPathfindingArrival onArrival)
 {
     if (outdoorGrid == null)
     {
         outdoorGrid = GameObject.Find("OutdoorPlane").GetComponent <OutdoorGrid>();
     }
     outdoorTargetNode = outdoorGrid.NodeFromWorldPoint(pos);
     OutdoorPathRequestManager.RequestPath(transform.position, pos, OnOutdoorPathFound);
     SetOnArrival(onArrival);
 }
示例#2
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        OutdoorNode startNode  = grid.NodeFromWorldPoint(startPos);
        OutdoorNode targetNode = grid.NodeFromWorldPoint(targetPos);


        if (true)
        {
            Heap <OutdoorNode>    openSet   = new Heap <OutdoorNode>(grid.MaxSize);
            HashSet <OutdoorNode> closedSet = new HashSet <OutdoorNode>();
            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                OutdoorNode currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    pathSuccess = true;
                    break;
                }

                foreach (OutdoorNode neighbour in grid.GetNeighbours(currentNode))
                {
                    int extraMovementPenalty = 0;
                    foreach (OutdoorNode neighbour_ in grid.GetNeighbours(neighbour))
                    {
                        if (neighbour_.drivable || neighbour_.empty)
                        {
                            extraMovementPenalty = 10;
                        }
                    }
                    if (closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.movementPenalty + extraMovementPenalty;
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, targetPos, pathSuccess);
    }