iTz_WyTe

Pathfinding

Mar 18th, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. public class Pathfinding : MonoBehaviour {
  7.  
  8. PathRequestManager rmInstance;
  9. Grid grid;
  10.  
  11. void Awake() {
  12. rmInstance = GetComponent<PathRequestManager> ();
  13. grid = GetComponent<Grid> ();
  14. }
  15.  
  16. public void StartFindPath(Vector3 startPos, Vector3 targetPos) {
  17. StartCoroutine(FindPath(startPos,targetPos));
  18. }
  19.  
  20. IEnumerator FindPath(Vector3 startPos, Vector3 targetPos) {
  21. Vector3[] waypoints = new Vector3[0];
  22. bool pathSuccess = false;
  23.  
  24. Node startNode = grid.NodeFromWorldPoint(startPos);
  25. Node targetNode = grid.NodeFromWorldPoint(targetPos);
  26.  
  27. if ((startNode.walkable && startNode.unoccupied) && (targetNode.walkable && targetNode.unoccupied)) {
  28.  
  29. List<Node> openSet = new List<Node> ();
  30. HashSet<Node> closedSet = new HashSet<Node> ();
  31. openSet.Add (startNode);
  32.  
  33. // current node = node in OPEN set with lowest f_cost
  34. while (openSet.Count > 0) {
  35. Node node = openSet [0];
  36. for (int i = 1; i < openSet.Count; i++) {
  37. if (openSet [i].fCost < node.fCost || openSet [i].fCost == node.fCost) {
  38. if (openSet [i].hCost < node.hCost)
  39. node = openSet [i];
  40. }
  41. }
  42.  
  43. //Remove the last node with a lower f_cost from open set to closed set
  44. openSet.Remove (node);
  45. closedSet.Add (node);
  46.  
  47. //Path has been found
  48. if (node == targetNode) {
  49. pathSuccess = true;
  50. break;
  51. }
  52.  
  53. //Foreach neighbour of the current node
  54. foreach (Node neighbour in grid.GetNeighbours(node)) {
  55. //if the neighbour is not traversable or is in closed set
  56. if (!neighbour.walkable || !neighbour.unoccupied|| closedSet.Contains (neighbour)) {
  57. //skip to next neighbour
  58. continue;
  59. }
  60.  
  61. int newCostToNeighbour = node.gCost + GetDistance (node, neighbour);
  62. if (newCostToNeighbour < neighbour.gCost || !openSet.Contains (neighbour)) {
  63. //Set f_cost of neighbour
  64. neighbour.gCost = newCostToNeighbour;
  65. neighbour.hCost = GetDistance (neighbour, targetNode);
  66. //Set parent of neighbour to current
  67. neighbour.parent = node;
  68.  
  69. //If neighbour is not in open set, add neighbour to open set
  70. if (!openSet.Contains (neighbour))
  71. openSet.Add (neighbour);
  72. }
  73. }
  74. }
  75. }
  76. yield return null;
  77. if (pathSuccess) {
  78. waypoints = RetracePath(startNode,targetNode);
  79. }
  80. rmInstance.FinishedProcessingPath (waypoints, pathSuccess);
  81. }
  82.  
  83. Vector3[] RetracePath(Node startNode, Node endNode) {
  84. List<Node> path = new List<Node>();
  85. Node currentNode = endNode;
  86.  
  87. while (currentNode != startNode) {
  88. path.Add(currentNode);
  89. currentNode = currentNode.parent;
  90. }
  91. Vector3[] waypoints = SimplifyPath(path);
  92. Array.Reverse (waypoints);
  93. return waypoints;
  94.  
  95. }
  96.  
  97. //Simplify Path by removing wp until change in direction
  98. Vector3[] SimplifyPath(List<Node> path) {
  99. List<Vector3> waypoints = new List<Vector3> ();
  100. Vector2 directionOld = Vector2.zero;
  101.  
  102. for (int i = 1; i < path.Count; i++) {
  103. Vector2 directionNew = new Vector2 (path [i - 1].gridX - path [i].gridX, path [i - 1].gridY - path [i].gridY);
  104. if (directionNew != directionOld) {
  105. waypoints.Add(path[i-1].worldPosition);
  106. }
  107. directionOld = directionNew;
  108. }
  109. return waypoints.ToArray ();
  110. }
  111.  
  112. int GetDistance(Node nodeA, Node nodeB) {
  113. int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
  114. int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY);
  115.  
  116. if (dstX > dstY)
  117. return 14*dstY + 10* (dstX-dstY);
  118. return 14*dstX + 10 * (dstY-dstX);
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment