Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class MainScript : MonoBehaviour
  7. {
  8. public GameObject thisPrefab;
  9. public GameObject clone;
  10. public GameObject[] nodes;
  11. public List<GameObject> checkedNodes;
  12. public List<GameObject> testNodes;
  13. public List<GameObject> solutionPaths;
  14. public GameObject startingNode;
  15. public GameObject endNode;
  16.  
  17. public bool foundSolution = false;
  18.  
  19. public bool dijkstraEnabled = false;
  20. public List<GameObject> currentPath;
  21.  
  22. public bool nodeTextsEnabled = false;
  23. public float searchSpeed = 0.1f;
  24. public int boardSize = 20;
  25.  
  26. public void Reset()
  27. {
  28. checkedNodes = new List<GameObject>();
  29. solutionPaths = new List<GameObject>();
  30. currentPath = null;
  31. startingNode = null;
  32. endNode = null;
  33. foundSolution = false;
  34. // checkedNodes = null;
  35. nodes = GameObject.FindGameObjectsWithTag("Node");
  36. DestroyNodes();
  37. // nodes = null;
  38. SpawnNodes();
  39. StartCoroutine(SetupBoard(boardSize / 3));
  40. // nodes = GameObject.FindGameObjectsWithTag("Node");
  41.  
  42. }
  43.  
  44. public void SpawnNodes()
  45. {
  46. Camera.main.transform.position = new Vector3(boardSize / 2, boardSize / 2, (-boardSize));
  47. for (int i = 0; i < boardSize; i++)
  48. {
  49. for (int j = 0; j < boardSize; j++)
  50. {
  51. float waitTime = (((i - j) + (boardSize * 2)));
  52. waitTime = waitTime * 0.1f;
  53. //Instantiate(thisPrefab, new Vector3(i, j), transform.rotation);
  54. StartCoroutine(SpawnEffect(waitTime, i, j));
  55. }
  56.  
  57. }
  58. }
  59.  
  60. public void UseDijkstra()
  61. {
  62. dijkstraEnabled = true;
  63. Reset();
  64. }
  65.  
  66. public void changeN(int n)
  67. {
  68. boardSize = n;
  69. Reset();
  70. }
  71.  
  72. public void changeSpeed(float n)
  73. {
  74. searchSpeed = n;
  75. // Reset();
  76. }
  77.  
  78. public void SlowSpeed()
  79. {
  80. changeSpeed(1.0f);
  81. }
  82.  
  83. public void MediumSpeed()
  84. {
  85. changeSpeed(0.1f);
  86. }
  87.  
  88. public void FastSpeed()
  89. {
  90. changeSpeed(0.01f);
  91. }
  92.  
  93.  
  94.  
  95. public void useAStar()
  96. {
  97. dijkstraEnabled = false;
  98. Reset();
  99. }
  100.  
  101. public void InstantSolve()
  102. {
  103. while (!foundSolution)
  104. {
  105. stepThrough();
  106. }
  107. }
  108.  
  109. public void AutoSolve()
  110. {
  111. StartCoroutine(AutoSolveCoroutine());
  112. }
  113.  
  114. IEnumerator AutoSolveCoroutine()
  115. {
  116. while (!foundSolution)
  117. {
  118. yield return new WaitForSeconds(searchSpeed);
  119. stepThrough();
  120. }
  121. }
  122.  
  123. IEnumerator SetupBoard(float time)
  124. {
  125. yield return new WaitForSeconds(time);
  126. nodes = GameObject.FindGameObjectsWithTag("Node");
  127. foreach (GameObject node in nodes)
  128. {
  129. node.transform.GetChild(0).gameObject.SetActive(true);
  130. }
  131.  
  132. int index = Random.Range(0, nodes.Length);
  133. nodes[index].gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
  134. int targetNode = 0;
  135. Debug.Log("Picking targetNode");
  136. Debug.Log(targetNode);
  137. Debug.Log(index);
  138. if (targetNode == 0)
  139. {
  140. Debug.Log("Targetnode is 0");
  141. }
  142. // targetNode = Random.Range(0, nodes.Length);
  143. while ((targetNode == 0) && (targetNode != index))
  144. {
  145. Debug.Log(targetNode);
  146. targetNode = Random.Range(0, nodes.Length);
  147. }
  148. nodes[targetNode].gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
  149. startingNode = nodes[targetNode];
  150. endNode = nodes[index];
  151.  
  152. foreach (GameObject node in nodes)
  153. {
  154. float distFromStart = Vector3.Distance(startingNode.transform.position, node.transform.position);
  155. float distFromEnd = Vector3.Distance(endNode.transform.position, node.transform.position);
  156. node.GetComponent<Node>().Gval = (int)distFromStart;
  157. if (dijkstraEnabled)
  158. {
  159. distFromEnd = 0;
  160. node.GetComponent<Node>().Hval = 0;
  161. }
  162. else
  163. {
  164. node.GetComponent<Node>().Hval = (int)distFromEnd;
  165. }
  166.  
  167. float fVal = distFromStart + distFromEnd;
  168. node.GetComponent<Node>().Fval = (int)fVal;
  169. // print("Distance to other: " + dist);
  170. }
  171.  
  172. StartCoroutine(LateStart(0.5f));
  173.  
  174. }
  175.  
  176. IEnumerator SpawnEffect(float time, int i, int j)
  177. {
  178. yield return new WaitForSeconds(time);
  179. Instantiate(thisPrefab, new Vector3(i, j), transform.rotation);
  180.  
  181. }
  182.  
  183. public void DestroyNodes()
  184. {
  185. for (int i = 0; i < nodes.Length; i++)
  186. {
  187. StartCoroutine(DestroyEffect(i * 0.005f, nodes[i]));
  188. nodes[i].tag = "Untagged";
  189. nodes[i].transform.GetChild(0).gameObject.SetActive(false);
  190. }
  191. }
  192.  
  193. IEnumerator DestroyEffect(float waitTime, GameObject node)
  194. {
  195. yield return new WaitForSeconds(waitTime);
  196. node.GetComponent<Rigidbody>().isKinematic = false;
  197. node.GetComponent<Rigidbody>().useGravity = true;
  198. // node.GetComponent<Rigidbody>().AddExplosionForce(1000.0f, transform.position, 500.0f, 3.0f);
  199. float speed = Random.Range(0.0f, 100.0f);
  200. Vector3 impulse = new Vector3(speed, speed, speed);
  201. node.GetComponent<Rigidbody>().AddForce(impulse, ForceMode.Impulse);
  202. node.GetComponent<Node>().DestroyMe();
  203. }
  204.  
  205. public void toggleNodeText()
  206. {
  207. if (nodeTextsEnabled)
  208. {
  209. foreach (GameObject node in nodes)
  210. {
  211. node.transform.GetChild(1).gameObject.SetActive(false);
  212. }
  213. nodeTextsEnabled = false;
  214. }
  215. else
  216. {
  217. foreach (GameObject node in nodes)
  218. {
  219. node.transform.GetChild(1).gameObject.SetActive(true);
  220. }
  221. nodeTextsEnabled = true;
  222. }
  223. }
  224.  
  225.  
  226. IEnumerator LateStart(float waitTime)
  227. {
  228. yield return new WaitForSeconds(waitTime);
  229. foreach (GameObject neighbor in startingNode.GetComponent<Node>().neighbors)
  230. {
  231. Debug.Log("Got neighbor");
  232. neighbor.GetComponent<Renderer>().material.SetColor("_Color", Color.yellow);
  233. Debug.Log(checkedNodes);
  234. // testNodes.Add(neighbor);
  235. checkedNodes.Add(neighbor);
  236. }
  237. }
  238.  
  239.  
  240. public void stepThrough()
  241. {
  242. if (!foundSolution)
  243. {
  244.  
  245. foreach (GameObject sol in solutionPaths)
  246. {
  247. sol.GetComponent<Renderer>().material.SetColor("_Color", Color.cyan);
  248. }
  249.  
  250. print("space key was pressed");
  251. GameObject bestNode = checkedNodes[0];
  252. foreach (GameObject test in checkedNodes)
  253. {
  254. if (test.GetComponent<Node>().isChecked == false)
  255. {
  256. bestNode = test;
  257. }
  258. }
  259. foreach (GameObject node in checkedNodes)
  260. {
  261. if ((node.GetComponent<Node>().Fval <= bestNode.GetComponent<Node>().Fval) && (node.GetComponent<Node>().isChecked == false))
  262. {
  263. if (node.GetComponent<Node>().Hval <= bestNode.GetComponent<Node>().Hval)
  264. {
  265. bestNode = node;
  266.  
  267. }
  268. }
  269. }
  270. if (bestNode.Equals(endNode))
  271. {
  272. Debug.Log("Found path!");
  273. // GameObject backTrackBestNode =
  274.  
  275. List<GameObject> options = new List<GameObject>();
  276. foreach (GameObject backNeighbor in endNode.GetComponent<Node>().neighbors)
  277. {
  278. if (solutionPaths.Contains(backNeighbor))
  279. {
  280. options.Add(backNeighbor);
  281. }
  282. }
  283.  
  284. GameObject backTrackHead = options[0];
  285. foreach (GameObject option in options)
  286. {
  287. if (option.GetComponent<Node>().Gval < backTrackHead.GetComponent<Node>().Gval)
  288. {
  289. backTrackHead = option;
  290. }
  291. }
  292.  
  293. backTrackHead.GetComponent<Renderer>().material.SetColor("_Color", Color.magenta);
  294.  
  295. // while (!(backTrackHead.Equals(startingNode)))
  296. for (int k = 0; k < 500; k++)
  297. {
  298. if (!(backTrackHead.Equals(startingNode)))
  299. {
  300. backTrackHead.GetComponent<Node>().Gval = 1000;
  301. Debug.Log("iterating");
  302. options = new List<GameObject>();
  303. foreach (GameObject backNeighbor in backTrackHead.GetComponent<Node>().neighbors)
  304. {
  305. if (solutionPaths.Contains(backNeighbor))
  306. {
  307. Debug.Log("Adding options");
  308. options.Add(backNeighbor);
  309. }
  310. }
  311. options.Remove(backTrackHead);
  312. backTrackHead = options[0];
  313. foreach (GameObject option in options)
  314. {
  315. if (option.GetComponent<Node>().Gval < backTrackHead.GetComponent<Node>().Gval)
  316. {
  317. Debug.Log("Changing backtrack log");
  318. backTrackHead = option;
  319. }
  320. }
  321.  
  322. backTrackHead.GetComponent<Renderer>().material.SetColor("_Color", Color.magenta);
  323.  
  324.  
  325. }
  326. foundSolution = true;
  327. }
  328.  
  329. }
  330. bestNode.GetComponent<Node>().isChecked = true;
  331. bestNode.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
  332. solutionPaths.Add(bestNode);
  333.  
  334. checkedNodes.Remove(bestNode);
  335. foreach (GameObject neighbor in bestNode.GetComponent<Node>().neighbors)
  336. {
  337. if (neighbor.GetComponent<Node>().isChecked == false && neighbor.GetComponent<Node>().isBlocked == false)
  338. {
  339. Debug.Log("Got neighbor");
  340. if (!(neighbor.Equals(endNode)))
  341. {
  342. neighbor.GetComponent<Renderer>().material.SetColor("_Color", Color.yellow);
  343. }
  344. checkedNodes.Add(neighbor);
  345. }
  346.  
  347. }
  348. startingNode.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
  349.  
  350. }
  351.  
  352. }
  353.  
  354.  
  355. // Update is called once per frame
  356. void Update()
  357. {
  358. if (Input.GetMouseButton(0))
  359. {
  360. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  361. RaycastHit hit;
  362. if (Physics.Raycast(ray, out hit))
  363. {
  364. hit.transform.gameObject.GetComponent<Node>().Fval = 100000;
  365. hit.transform.gameObject.GetComponent<Node>().isBlocked = true;
  366. hit.transform.GetComponent<Renderer>().material.SetColor("_Color", Color.black);
  367. }
  368. }
  369.  
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement