/// <summary> /// Run the path between the two positions /// </summary> /// <param name="start"> the starting position </param> /// <param name="end"> the ending position </param> /// <param name="path"> The path number to create </param> void runPathCreation(Vector3 start, Vector3 end, int path) { PathfindingParameters pfm = pfo.pathParameters; pfm.pathFinderObject = null; pfm.startPosition = start; pfm.EndPosition = end; Paths[path].Init(pfm); }
public PathfindingResult FindPath(PathfindingParameters parameters) { PathfindingResult result = new PathfindingResult(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Node startNode = grid.GetNodeAt(parameters.startPos); Node endNode = grid.GetNodeAt(parameters.endPos); Node bestNode = null; Heap<Node> openList = new Heap<Node>(grid.gridMaxSize); HashSet<Node> closedList = new HashSet<Node>(); if (drawPathfinding) grid.ResetColors(); openList.Add(startNode); while (openList.Count > 0) { Node currentNode = openList.RemoveFirst(); if (currentNode == endNode || (parameters.endNodes != null && parameters.endNodes.Contains(currentNode))) { sw.Stop(); //print(sw.ElapsedMilliseconds + "ms"); result.path = GetPath(RetracePath(currentNode, startNode), parameters); return result; } if (drawPathfinding) currentNode.color = Color.red; closedList.Add(currentNode); foreach (Node n in grid.GetNeighbours(currentNode)) { //if (!n.walkable || closedList.Contains(n)) if (!grid.GetWalkable(n, parameters.radius) || closedList.Contains(n)) continue; int newGCost = currentNode.gCost + grid.GetDistance(n, currentNode); if (newGCost < n.gCost || !openList.Contains(n)) { n.gCost = newGCost; n.hCost = grid.GetDistance(n, endNode); n.parent = currentNode; if (drawPathfinding) n.color = Color.yellow; if (bestNode == null || n.hCost < bestNode.hCost) bestNode = n; if (!openList.Contains(n)) openList.Add(n); else openList.UpdateItem(n); } } } sw.Stop(); //print("no path found, " + sw.ElapsedMilliseconds + "ms"); if (bestNode != null) result.path = GetPath(RetracePath(bestNode, startNode), parameters); else result.path = new List<Vector3>(); return result; }
public static void RequestPath(PathfindingParameters parameters, Action<PathfindingResult> callback) { requests.Enqueue(new PathfindingRequest(parameters, callback)); if (!isProcessing) instance.StartCoroutine("CalculPaths"); }
public PathfindingRequest(PathfindingParameters _parameters, Action<PathfindingResult> _callback) { parameters = _parameters; callback = _callback; }
public List<Vector3> GetPath(List<Node> path, PathfindingParameters parameters) { if (path.Count > 0) { List<Vector3> bestPath = BestSmooth(GetAllNodePositions(path), parameters.radius); bestPath.RemoveAt(0); if (grid.InBounds(parameters.endPos)) { if (!grid.IsObstacle(parameters.endPos, parameters.radius)) { bestPath.RemoveAt(bestPath.Count - 1); bestPath.Add(parameters.endPos); } } waypoints = bestPath; return bestPath; } return new List<Vector3>(); }