Beispiel #1
0
    private void FindPath(PathFinding_Request request)
    {
        bool        foundPath = algorithmReferences[algoToUse].FindPath(request.start, request.goal, heuristic, ref request.nodeData, nodeGraph.MaxGridSize);
        List <Node> path;

        if (foundPath)
        {
            // Build the found path list from Goal to Start
            path = BuildPath(request);

            if (path.Count > 1)
            {
                // Reverse the found path to be from Start to Goal
                path.Reverse();

                // Simplify the path to take up less space and only track pivot points
                path = SimplifyPath(path);
            }
        }
        else
        {
            // If a path was not found return a null path
            path = null;
        }

        PathFinding_Result result = new PathFinding_Result(request.callBack, path, foundPath);

        // Lock the PathFinding class instance and decrement the active threads count
        lock (_instance)
        {
            _ActiveThreads--;
            pathRequesetResults.Enqueue(result);
        }
    }
Beispiel #2
0
    private void Update()
    {
        // If we aren't waiting to scan the grid, increment our timer for the next time we need to
        if (!waitingToScanGrid)
        {
            nodeGraph.rescanTimer += Time.deltaTime;

            if (nodeGraph.rescanTimer > nodeGraph.rescanRate)
            {
                // Flag that we would like to scan the grid
                waitingToScanGrid     = true;
                nodeGraph.rescanTimer = 0.0f;
            }
        }

        // Wait to scan the grid until there are no active threads which could be using the
        //previous scans grid data.
        if (waitingToScanGrid && _ActiveThreads == 0)
        {
            nodeGraph.ScanGrid();
            waitingToScanGrid = false;
        }

        // Check if we have any requests to spin up finding a path for
        if (pathRequests.Count > 0)
        {
            // Spin up a path finding thread if it's safe to do so and we haven't hit our max thread count
            if (!waitingToScanGrid && _ActiveThreads < maxNumberOfThreads)
            {
                StartPathFindingThread((PathFinding_Request)pathRequests.Dequeue());
                //FindPath((PathFinding_Request)pathRequests.Dequeue());
            }
        }

        // Check if we have any path results to call back to
        if (pathRequesetResults.Count > 0)
        {
            PathFinding_Result result = (PathFinding_Result)pathRequesetResults.Dequeue();
            result.callBack(result.path, result.foundPath);
        }
    }