PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Semester 6/AI Programming/Week 4/Pathfinding Project/Assets/OldScript/ArrayScript.cs

https://gitlab.com/smurmann/KDUProjects
C# | 322 lines | 259 code | 49 blank | 14 comment | 54 complexity | 37d27f7a40bdad943ef34f7ebc345fa6 MD5 | raw file
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. public class ArrayScript : MonoBehaviour
  6. {
  7. public Node[,] nodeArray;
  8. public int X, Z;
  9. public bool readyToRunPathFind = false;
  10. public enum SearchMethod
  11. {
  12. Dijkstra = 0,
  13. Astar
  14. }
  15. public SearchMethod method;
  16. public class ImpNode
  17. {
  18. public int X, Y;
  19. public void SetImpNode(int x, int y)
  20. {
  21. X = x;
  22. Y = y;
  23. }
  24. public ImpNode()
  25. {
  26. X = -1;
  27. Y = -1;
  28. }
  29. }
  30. public void ToggleSearchMethod()
  31. {
  32. method++;
  33. if((int)method > 1)
  34. {
  35. method = 0;
  36. }
  37. }
  38. public void ReloadScene()
  39. {
  40. Application.LoadLevel(0);
  41. }
  42. public class Node
  43. {
  44. public List<Node> neighbourNodes;
  45. public GameObject visual;
  46. public int X, Y;
  47. public NodeScript.TileState currentState;
  48. public float distanceToSource;
  49. public Node previousNode;
  50. public Node()
  51. {
  52. neighbourNodes = new List<Node>();
  53. visual = Instantiate(Resources.Load("Node")) as GameObject;
  54. currentState = 0;
  55. }
  56. public void SetVisualLocation(Vector3 location)
  57. {
  58. visual.transform.position = location;
  59. }
  60. public void SetVisualColour(Color colour)
  61. {
  62. visual.GetComponent<SpriteRenderer>().color = colour;
  63. }
  64. public void SetParent(Transform parent)
  65. {
  66. visual.transform.parent = parent;
  67. }
  68. public void XY(int x, int y, ArrayScript script)
  69. {
  70. X = x;
  71. Y = y;
  72. var tempVisual = visual.GetComponent<NodeScript>();
  73. //tempVisual.TilePosX = x;
  74. //tempVisual.TilePosY = y;
  75. //tempVisual.array = script;
  76. }
  77. }
  78. void Start()
  79. {
  80. SpawnArray(X, Z);
  81. }
  82. void SpawnArray(int x, int z)
  83. {
  84. var tile = Resources.Load("Node") as GameObject;
  85. var size = tile.GetComponent<SpriteRenderer>().bounds.size.x;
  86. var container = new GameObject();
  87. nodeArray = new Node[x, z];
  88. container.name = "ArrayContainer";
  89. for (int i = 0; i < x; i++)
  90. {
  91. for (int j = 0; j < z; j++)
  92. {
  93. var node = nodeArray[i, j] = new Node();
  94. node.SetVisualLocation(new Vector3(size * i - (size * x - 1) / 2, size * j - (size * z - 1) / 2, 0));
  95. node.SetParent(container.transform);
  96. node.XY(i, j, gameObject.GetComponent<ArrayScript>());
  97. }
  98. }
  99. }
  100. void TracePath(Node end)
  101. {
  102. if (end.previousNode == null)
  103. {
  104. return;
  105. }
  106. end.previousNode.SetVisualColour(Color.magenta);
  107. TracePath(end.previousNode);
  108. }
  109. IEnumerator Delay(float delay)
  110. {
  111. yield return null;
  112. //yield return new WaitForSeconds(delay);
  113. }
  114. IEnumerator PathFind(ImpNode startNode, ImpNode endNode)
  115. {
  116. // float distance = 0;
  117. Dictionary<Node, float> distanceToSource = new Dictionary<Node, float>();
  118. Dictionary<Node, float> heuristicDistance = new Dictionary<Node, float>();
  119. Node start = nodeArray[startNode.X, startNode.Y];
  120. Node end = nodeArray[endNode.X, endNode.X];
  121. List<Node> unvisitedNodes = new List<Node>();
  122. List<Node> openList = new List<Node>();
  123. distanceToSource[start] = 0;
  124. heuristicDistance[start] = 0;
  125. openList.Add(start);
  126. foreach (Node i in nodeArray)
  127. {
  128. i.previousNode = null;
  129. i.SetVisualColour(Color.white);
  130. if (i != start)
  131. {
  132. distanceToSource[i] = Mathf.Infinity;
  133. heuristicDistance[i] = Mathf.Infinity;
  134. //previousNode[i] = null;
  135. }
  136. unvisitedNodes.Add(i);
  137. }
  138. Debug.Log(openList.Count);
  139. while (openList.Count > 0)
  140. {
  141. // Debug.Log("OpenList Count: " + openList.Count);
  142. Node tempNode = null;
  143. foreach (Node lowest in openList)
  144. {
  145. if(method == SearchMethod.Dijkstra)
  146. {
  147. //Dijkstra
  148. if (tempNode == null || distanceToSource[lowest] < distanceToSource[tempNode])
  149. {
  150. if (unvisitedNodes.IndexOf(lowest) > -1)
  151. {
  152. tempNode = lowest;
  153. }
  154. }
  155. }
  156. else if(method == SearchMethod.Astar)
  157. {
  158. //Astar
  159. float fLowest = Mathf.Infinity;
  160. float fTempNode = Mathf.Infinity;
  161. if (tempNode != null)
  162. {
  163. fLowest = distanceToSource[lowest] + heuristicDistance[lowest];
  164. fTempNode = distanceToSource[tempNode] + heuristicDistance[tempNode];
  165. }
  166. if (tempNode == null || fLowest < fTempNode)
  167. {
  168. if (unvisitedNodes.IndexOf(lowest) > -1)
  169. {
  170. tempNode = lowest;
  171. }
  172. }
  173. }
  174. }
  175. tempNode.SetVisualColour(Color.grey);
  176. //var tempNode = openList[0];
  177. openList.Remove(tempNode);
  178. if (tempNode.X == endNode.X && tempNode.Y == endNode.Y)
  179. {
  180. TracePath(tempNode);
  181. Debug.Log("End Found");
  182. yield break;
  183. }
  184. unvisitedNodes.Remove(tempNode);
  185. tempNode.neighbourNodes = new List<Node>();
  186. if (tempNode.X > 0)
  187. tempNode.neighbourNodes.Add(nodeArray[tempNode.X - 1, tempNode.Y]);
  188. if (tempNode.Y > 0)
  189. tempNode.neighbourNodes.Add(nodeArray[tempNode.X, tempNode.Y - 1]);
  190. if (tempNode.X < nodeArray.GetLength(0) - 1)
  191. tempNode.neighbourNodes.Add(nodeArray[tempNode.X + 1, tempNode.Y]);
  192. if (tempNode.Y < nodeArray.GetLength(1) - 1)
  193. tempNode.neighbourNodes.Add(nodeArray[tempNode.X, tempNode.Y + 1]);
  194. foreach (Node v in tempNode.neighbourNodes)
  195. {
  196. if (unvisitedNodes.IndexOf(v) > -1 && v.currentState != NodeScript.TileState.Obstacle)
  197. {
  198. if(method == SearchMethod.Dijkstra)
  199. {
  200. // Dijkstra
  201. if (distanceToSource[tempNode]+10 < distanceToSource[v])
  202. {
  203. distanceToSource[v] = distanceToSource[tempNode] + 10;
  204. v.previousNode = tempNode;
  205. }
  206. if (openList.IndexOf(v) == -1)
  207. {
  208. openList.Add(v);
  209. }
  210. }
  211. else if(method == SearchMethod.Astar)
  212. {
  213. if (openList.IndexOf(v) == -1)
  214. {
  215. openList.Add(v);
  216. v.previousNode = tempNode;
  217. distanceToSource[v] = distanceToSource[tempNode] + 1;
  218. heuristicDistance[v] = Mathf.Abs(v.X - endNode.X) + Mathf.Abs(v.Y - endNode.Y);
  219. }
  220. else
  221. {
  222. if (distanceToSource[v] < distanceToSource[tempNode])
  223. {
  224. v.previousNode = tempNode;
  225. distanceToSource[tempNode] = distanceToSource[v];
  226. heuristicDistance[v] = Mathf.Abs(v.X - endNode.X) + Mathf.Abs(v.Y - endNode.Y);
  227. }
  228. }
  229. }
  230. v.SetVisualColour(Color.yellow);
  231. }
  232. }
  233. yield return null;
  234. //yield return StartCoroutine(Delay(0.001f));
  235. }
  236. }
  237. void FindStartandEnd()
  238. {
  239. ImpNode startPoint = new ImpNode();
  240. ImpNode endPoint = new ImpNode();
  241. foreach (Node node in nodeArray)
  242. {
  243. node.distanceToSource = Mathf.Infinity;
  244. if (node.currentState == NodeScript.TileState.EndNode)
  245. {
  246. endPoint.SetImpNode(node.X, node.Y);
  247. }
  248. else if (node.currentState == NodeScript.TileState.StartNode)
  249. {
  250. startPoint.SetImpNode(node.X, node.Y);
  251. }
  252. }
  253. if (startPoint.X == -1 || endPoint.X == -1)
  254. {
  255. //Debug.Log("No end or start found");
  256. readyToRunPathFind = false;
  257. return;
  258. }
  259. else
  260. {
  261. readyToRunPathFind = true;
  262. StopAllCoroutines();
  263. StartCoroutine(PathFind(startPoint, endPoint));
  264. Debug.Log(startPoint.X);
  265. //Debug.Log("End or start found");
  266. }
  267. }
  268. public void SetNodeState(int x, int y, NodeScript.TileState state)
  269. {
  270. nodeArray[x, y].currentState = state;
  271. FindStartandEnd();
  272. }
  273. }