Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class MainScript : MonoBehaviour
- {
- public GameObject thisPrefab;
- public GameObject clone;
- public GameObject[] nodes;
- public List<GameObject> checkedNodes;
- public List<GameObject> testNodes;
- public List<GameObject> solutionPaths;
- public GameObject startingNode;
- public GameObject endNode;
- public bool foundSolution = false;
- public bool dijkstraEnabled = false;
- public List<GameObject> currentPath;
- public bool nodeTextsEnabled = false;
- public float searchSpeed = 0.1f;
- public int boardSize = 20;
- public void Reset()
- {
- checkedNodes = new List<GameObject>();
- solutionPaths = new List<GameObject>();
- currentPath = null;
- startingNode = null;
- endNode = null;
- foundSolution = false;
- // checkedNodes = null;
- nodes = GameObject.FindGameObjectsWithTag("Node");
- DestroyNodes();
- // nodes = null;
- SpawnNodes();
- StartCoroutine(SetupBoard(boardSize / 3));
- // nodes = GameObject.FindGameObjectsWithTag("Node");
- }
- public void SpawnNodes()
- {
- Camera.main.transform.position = new Vector3(boardSize / 2, boardSize / 2, (-boardSize));
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- float waitTime = (((i - j) + (boardSize * 2)));
- waitTime = waitTime * 0.1f;
- //Instantiate(thisPrefab, new Vector3(i, j), transform.rotation);
- StartCoroutine(SpawnEffect(waitTime, i, j));
- }
- }
- }
- public void UseDijkstra()
- {
- dijkstraEnabled = true;
- Reset();
- }
- public void changeN(int n)
- {
- boardSize = n;
- Reset();
- }
- public void changeSpeed(float n)
- {
- searchSpeed = n;
- // Reset();
- }
- public void SlowSpeed()
- {
- changeSpeed(1.0f);
- }
- public void MediumSpeed()
- {
- changeSpeed(0.1f);
- }
- public void FastSpeed()
- {
- changeSpeed(0.01f);
- }
- public void useAStar()
- {
- dijkstraEnabled = false;
- Reset();
- }
- public void InstantSolve()
- {
- while (!foundSolution)
- {
- stepThrough();
- }
- }
- public void AutoSolve()
- {
- StartCoroutine(AutoSolveCoroutine());
- }
- IEnumerator AutoSolveCoroutine()
- {
- while (!foundSolution)
- {
- yield return new WaitForSeconds(searchSpeed);
- stepThrough();
- }
- }
- IEnumerator SetupBoard(float time)
- {
- yield return new WaitForSeconds(time);
- nodes = GameObject.FindGameObjectsWithTag("Node");
- foreach (GameObject node in nodes)
- {
- node.transform.GetChild(0).gameObject.SetActive(true);
- }
- int index = Random.Range(0, nodes.Length);
- nodes[index].gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
- int targetNode = 0;
- Debug.Log("Picking targetNode");
- Debug.Log(targetNode);
- Debug.Log(index);
- if (targetNode == 0)
- {
- Debug.Log("Targetnode is 0");
- }
- // targetNode = Random.Range(0, nodes.Length);
- while ((targetNode == 0) && (targetNode != index))
- {
- Debug.Log(targetNode);
- targetNode = Random.Range(0, nodes.Length);
- }
- nodes[targetNode].gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
- startingNode = nodes[targetNode];
- endNode = nodes[index];
- foreach (GameObject node in nodes)
- {
- float distFromStart = Vector3.Distance(startingNode.transform.position, node.transform.position);
- float distFromEnd = Vector3.Distance(endNode.transform.position, node.transform.position);
- node.GetComponent<Node>().Gval = (int)distFromStart;
- if (dijkstraEnabled)
- {
- distFromEnd = 0;
- node.GetComponent<Node>().Hval = 0;
- }
- else
- {
- node.GetComponent<Node>().Hval = (int)distFromEnd;
- }
- float fVal = distFromStart + distFromEnd;
- node.GetComponent<Node>().Fval = (int)fVal;
- // print("Distance to other: " + dist);
- }
- StartCoroutine(LateStart(0.5f));
- }
- IEnumerator SpawnEffect(float time, int i, int j)
- {
- yield return new WaitForSeconds(time);
- Instantiate(thisPrefab, new Vector3(i, j), transform.rotation);
- }
- public void DestroyNodes()
- {
- for (int i = 0; i < nodes.Length; i++)
- {
- StartCoroutine(DestroyEffect(i * 0.005f, nodes[i]));
- nodes[i].tag = "Untagged";
- nodes[i].transform.GetChild(0).gameObject.SetActive(false);
- }
- }
- IEnumerator DestroyEffect(float waitTime, GameObject node)
- {
- yield return new WaitForSeconds(waitTime);
- node.GetComponent<Rigidbody>().isKinematic = false;
- node.GetComponent<Rigidbody>().useGravity = true;
- // node.GetComponent<Rigidbody>().AddExplosionForce(1000.0f, transform.position, 500.0f, 3.0f);
- float speed = Random.Range(0.0f, 100.0f);
- Vector3 impulse = new Vector3(speed, speed, speed);
- node.GetComponent<Rigidbody>().AddForce(impulse, ForceMode.Impulse);
- node.GetComponent<Node>().DestroyMe();
- }
- public void toggleNodeText()
- {
- if (nodeTextsEnabled)
- {
- foreach (GameObject node in nodes)
- {
- node.transform.GetChild(1).gameObject.SetActive(false);
- }
- nodeTextsEnabled = false;
- }
- else
- {
- foreach (GameObject node in nodes)
- {
- node.transform.GetChild(1).gameObject.SetActive(true);
- }
- nodeTextsEnabled = true;
- }
- }
- IEnumerator LateStart(float waitTime)
- {
- yield return new WaitForSeconds(waitTime);
- foreach (GameObject neighbor in startingNode.GetComponent<Node>().neighbors)
- {
- Debug.Log("Got neighbor");
- neighbor.GetComponent<Renderer>().material.SetColor("_Color", Color.yellow);
- Debug.Log(checkedNodes);
- // testNodes.Add(neighbor);
- checkedNodes.Add(neighbor);
- }
- }
- public void stepThrough()
- {
- if (!foundSolution)
- {
- foreach (GameObject sol in solutionPaths)
- {
- sol.GetComponent<Renderer>().material.SetColor("_Color", Color.cyan);
- }
- print("space key was pressed");
- GameObject bestNode = checkedNodes[0];
- foreach (GameObject test in checkedNodes)
- {
- if (test.GetComponent<Node>().isChecked == false)
- {
- bestNode = test;
- }
- }
- foreach (GameObject node in checkedNodes)
- {
- if ((node.GetComponent<Node>().Fval <= bestNode.GetComponent<Node>().Fval) && (node.GetComponent<Node>().isChecked == false))
- {
- if (node.GetComponent<Node>().Hval <= bestNode.GetComponent<Node>().Hval)
- {
- bestNode = node;
- }
- }
- }
- if (bestNode.Equals(endNode))
- {
- Debug.Log("Found path!");
- // GameObject backTrackBestNode =
- List<GameObject> options = new List<GameObject>();
- foreach (GameObject backNeighbor in endNode.GetComponent<Node>().neighbors)
- {
- if (solutionPaths.Contains(backNeighbor))
- {
- options.Add(backNeighbor);
- }
- }
- GameObject backTrackHead = options[0];
- foreach (GameObject option in options)
- {
- if (option.GetComponent<Node>().Gval < backTrackHead.GetComponent<Node>().Gval)
- {
- backTrackHead = option;
- }
- }
- backTrackHead.GetComponent<Renderer>().material.SetColor("_Color", Color.magenta);
- // while (!(backTrackHead.Equals(startingNode)))
- for (int k = 0; k < 500; k++)
- {
- if (!(backTrackHead.Equals(startingNode)))
- {
- backTrackHead.GetComponent<Node>().Gval = 1000;
- Debug.Log("iterating");
- options = new List<GameObject>();
- foreach (GameObject backNeighbor in backTrackHead.GetComponent<Node>().neighbors)
- {
- if (solutionPaths.Contains(backNeighbor))
- {
- Debug.Log("Adding options");
- options.Add(backNeighbor);
- }
- }
- options.Remove(backTrackHead);
- backTrackHead = options[0];
- foreach (GameObject option in options)
- {
- if (option.GetComponent<Node>().Gval < backTrackHead.GetComponent<Node>().Gval)
- {
- Debug.Log("Changing backtrack log");
- backTrackHead = option;
- }
- }
- backTrackHead.GetComponent<Renderer>().material.SetColor("_Color", Color.magenta);
- }
- foundSolution = true;
- }
- }
- bestNode.GetComponent<Node>().isChecked = true;
- bestNode.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
- solutionPaths.Add(bestNode);
- checkedNodes.Remove(bestNode);
- foreach (GameObject neighbor in bestNode.GetComponent<Node>().neighbors)
- {
- if (neighbor.GetComponent<Node>().isChecked == false && neighbor.GetComponent<Node>().isBlocked == false)
- {
- Debug.Log("Got neighbor");
- if (!(neighbor.Equals(endNode)))
- {
- neighbor.GetComponent<Renderer>().material.SetColor("_Color", Color.yellow);
- }
- checkedNodes.Add(neighbor);
- }
- }
- startingNode.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetMouseButton(0))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- hit.transform.gameObject.GetComponent<Node>().Fval = 100000;
- hit.transform.gameObject.GetComponent<Node>().isBlocked = true;
- hit.transform.GetComponent<Renderer>().material.SetColor("_Color", Color.black);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement