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); } }
/// <summary> /// Create a pathfinding request from a start to a goal position. /// </summary> /// <param name="start">Start position Transform</param> /// <param name="goal">Goal position Transform</param> /// <param name="caller">Self reference to call back and update path data</param> public static void RequestPath(Transform start, Transform goal, Action <List <Node>, bool> callBack) { Node startNode = _instance.nodeGraph.NodeFromWorldPoint(start.position); Node goalNode = _instance.nodeGraph.NodeFromWorldPoint(goal.position); PathFinding_Request newRequest = new PathFinding_Request(startNode, goalNode, callBack); _instance.pathRequests.Enqueue(newRequest); }
/// <summary> /// Spin up a thread to find a path given PathFinding Request data /// </summary> /// <param name="request"></param> private void StartPathFindingThread(PathFinding_Request request) { Thread pathFindingThread = new Thread(() => FindPath(request)); // Set the threads priority to low so it doesn't take resources from the main thread pathFindingThread.Priority = System.Threading.ThreadPriority.Lowest; // Tell the thread to run in the background pathFindingThread.IsBackground = true; pathFindingThread.Start(); _ActiveThreads++; }