PathFinder.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace Robot_Rampage
  7. {
  8. static class PathFinder
  9. {
  10. #region Declarations
  11. private enum NodeStatus { Open, Closed };
  12. private static Dictionary<Vector2, NodeStatus> nodeStatus =
  13. new Dictionary<Vector2, NodeStatus>();
  14. private const int CostStraight = 10;
  15. private const int CostDiagonal = 15;
  16. private static List<PathNode> openList = new List<PathNode>();
  17. private static Dictionary<Vector2, float> nodeCosts =
  18. new Dictionary<Vector2, float>();
  19. #endregion
  20. #region Helper Methods
  21. static private void addNodeToOpenList(PathNode node)
  22. {
  23. int index = 0;
  24. float cost = node.TotalCost;
  25. while ((openList.Count() > index) &&
  26. (cost < openList[index].TotalCost))
  27. {
  28. index++;
  29. }
  30. openList.Insert(index, node);
  31. nodeCosts[node.GridLocation] = node.TotalCost;
  32. nodeStatus[node.GridLocation] = NodeStatus.Open;
  33. }
  34. static private List<PathNode> findAdjacentNodes(
  35. PathNode currentNode,
  36. PathNode endNode)
  37. {
  38. List<PathNode> adjacentNodes = new List<PathNode>();
  39. int X = currentNode.GridX;
  40. int Y = currentNode.GridY;
  41. bool upLeft = true;
  42. bool upRight = true;
  43. bool downLeft = true;
  44. bool downRight = true;
  45. if ((X > 0) && (!TileMap.IsWallTile(X - 1, Y)))
  46. {
  47. adjacentNodes.Add(new PathNode(
  48. currentNode,
  49. endNode,
  50. new Vector2(X - 1, Y),
  51. CostStraight + currentNode.DirectCost));
  52. }
  53. else
  54. {
  55. upLeft = false;
  56. downLeft = false;
  57. }
  58. if ((X < 49) && (!TileMap.IsWallTile(X + 1, Y)))
  59. {
  60. adjacentNodes.Add(new PathNode(
  61. currentNode,
  62. endNode,
  63. new Vector2(X + 1, Y),
  64. CostStraight + currentNode.DirectCost));
  65. }
  66. else
  67. {
  68. upRight = false;
  69. downRight = false;
  70. }
  71. if ((Y > 0) && (!TileMap.IsWallTile(X, Y - 1)))
  72. {
  73. adjacentNodes.Add(new PathNode(
  74. currentNode,
  75. endNode,
  76. new Vector2(X, Y - 1),
  77. CostStraight + currentNode.DirectCost));
  78. }
  79. else
  80. {
  81. upLeft = false;
  82. upRight = false;
  83. }
  84. if ((Y < 49) && (!TileMap.IsWallTile(X, Y + 1)))
  85. {
  86. adjacentNodes.Add(new PathNode(
  87. currentNode,
  88. endNode,
  89. new Vector2(X, Y + 1),
  90. CostStraight + currentNode.DirectCost));
  91. }
  92. else
  93. {
  94. downLeft = false;
  95. downRight = false;
  96. }
  97. if ((upLeft) && (!TileMap.IsWallTile(X - 1, Y - 1)))
  98. {
  99. adjacentNodes.Add(new PathNode(
  100. currentNode,
  101. endNode,
  102. new Vector2(X - 1, Y - 1),
  103. CostDiagonal + currentNode.DirectCost));
  104. }
  105. if ((upRight) && (!TileMap.IsWallTile(X + 1, Y - 1)))
  106. {
  107. adjacentNodes.Add(new PathNode(
  108. currentNode,
  109. endNode,
  110. new Vector2(X + 1, Y - 1),
  111. CostDiagonal + currentNode.DirectCost));
  112. }
  113. if ((downLeft) && (!TileMap.IsWallTile(X - 1, Y + 1)))
  114. {
  115. adjacentNodes.Add(new PathNode(
  116. currentNode,
  117. endNode,
  118. new Vector2(X - 1, Y + 1),
  119. CostDiagonal + currentNode.DirectCost));
  120. }
  121. if ((downRight) && (!TileMap.IsWallTile(X + 1, Y + 1)))
  122. {
  123. adjacentNodes.Add(new PathNode(
  124. currentNode,
  125. endNode,
  126. new Vector2(X + 1, Y + 1),
  127. CostDiagonal + currentNode.DirectCost));
  128. }
  129. return adjacentNodes;
  130. }
  131. #endregion
  132. #region Public Methods
  133. static public List<Vector2> FindPath(
  134. Vector2 startTile,
  135. Vector2 endTile)
  136. {
  137. if (TileMap.IsWallTile(endTile) ||
  138. TileMap.IsWallTile(startTile))
  139. {
  140. return null;
  141. }
  142. openList.Clear();
  143. nodeCosts.Clear();
  144. nodeStatus.Clear();
  145. PathNode startNode;
  146. PathNode endNode;
  147. endNode = new PathNode(null, null, endTile, 0);
  148. startNode = new PathNode(null, endNode, startTile, 0);
  149. addNodeToOpenList(startNode);
  150. while (openList.Count > 0)
  151. {
  152. PathNode currentNode = openList[openList.Count - 1];
  153. if (currentNode.IsEqualToNode(endNode))
  154. {
  155. List<Vector2> bestPath = new List<Vector2>();
  156. while (currentNode != null)
  157. {
  158. bestPath.Insert(0, currentNode.GridLocation);
  159. currentNode = currentNode.ParentNode;
  160. }
  161. return bestPath;
  162. }
  163. openList.Remove(currentNode);
  164. nodeCosts.Remove(currentNode.GridLocation);
  165. foreach (
  166. PathNode possibleNode in
  167. findAdjacentNodes(currentNode, endNode))
  168. {
  169. if (nodeStatus.ContainsKey(possibleNode.GridLocation))
  170. {
  171. if (nodeStatus[possibleNode.GridLocation] ==
  172. NodeStatus.Closed)
  173. {
  174. continue;
  175. }
  176. if (
  177. nodeStatus[possibleNode.GridLocation] ==
  178. NodeStatus.Open)
  179. {
  180. if (possibleNode.TotalCost >=
  181. nodeCosts[possibleNode.GridLocation])
  182. {
  183. continue;
  184. }
  185. }
  186. }
  187. addNodeToOpenList(possibleNode);
  188. }
  189. nodeStatus[currentNode.GridLocation] = NodeStatus.Closed;
  190. }
  191. return null;
  192. }
  193. #endregion
  194. }
  195. }