public GraphNode GetClosestTarget(List <GraphNode> listOfNodes) { // Finds the closest node in list of target nodes AstarNavigation a = new AstarNavigation(); var f = graph.Find(q => q.Equals(new GraphNode(transform.position.x, transform.position.y))); if (f == null) { // WE ARE NOT INSIDE A NODE f = CreateTemporaryNode(); } var path = a.FindClosestGoalPath(f, listOfNodes); foreach (GraphNode g in graph) { g.ResetPrevious(); } foreach (GraphNode p in path) { nodeQueue.Enqueue(p); } return(path[path.Count - 1]); }
public void GetNewTarget() { // Tries to make some unvisited node a target // If all nodes explored, target random node AstarNavigation a = new AstarNavigation(); var f = graph.Find(q => q.Equals(new GraphNode(transform.position.x, transform.position.y))); if (f == null) { // WE ARE NOT INSIDE A NODE f = CreateTemporaryNode(); } List <GraphNode> path = new List <GraphNode>(); int random = 0; if (unvisitedNodes.Count > 0) { random = UnityEngine.Random.Range(0, unvisitedNodes.Count); path = a.FindPath(f, unvisitedNodes[random]); } else { random = UnityEngine.Random.Range(0, graph.Count); path = a.FindPath(f, graph[random]); } foreach (GraphNode g in graph) { g.ResetPrevious(); } foreach (GraphNode p in path) { nodeQueue.Enqueue(p); } CalculateMovementToNextTarget(nodeQueue.Dequeue()); }
public void GetShortestPathToTarget(GraphNode target) { // Finds shortest path to a target node from current position AstarNavigation a = new AstarNavigation(); var f = graph.Find(q => q.Equals(new GraphNode(transform.position.x, transform.position.y))); if (f == null) { // WE ARE NOT INSIDE A NODE f = CreateTemporaryNode(); } var path = a.FindPath(f, target); foreach (GraphNode g in graph) { g.ResetPrevious(); } foreach (GraphNode p in path) { nodeQueue.Enqueue(p); } }