示例#1
0
    public static void RequestPath(aPathRequest request)
    {
        ThreadStart threadStart = delegate { instance.pathfinding.FindPath(request, instance.FinishedProcessingPath); };

        threadStart.Invoke();
    }
示例#2
0
    public void FindPath(aPathRequest request, Action <aPathResult> callback)
    {
        Vector3[] wayPoints   = new Vector3[0];
        bool      pathSuccess = false;

        aNode startNode  = grid.NodeFromWorldPoint(request.pathStart);
        aNode targetNode = grid.NodeFromWorldPoint(request.pathEnd);

        if (startNode.walkable && targetNode.walkable)
        {
            aHeap        openSet   = new aHeap(grid.MaxSize);
            List <aNode> closedSet = new List <aNode>();

            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                aNode currentNode = openSet.RemoveFirst();

                closedSet.Add(currentNode);

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

                foreach (aNode neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))
                    {
                        continue;
                    }

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

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

        if (pathSuccess)
        {
            wayPoints = RetracePath(startNode, targetNode);
        }

        pathSuccess = wayPoints.Length > 0;
        callback(new aPathResult(wayPoints, pathSuccess, request.callback));
    }