Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- public class AStar : MonoBehaviour
- {
- public List<GameObject> open_list = new List<GameObject>(); //list of the nodes that have to be taken into account
- public List<GameObject> closed_list = new List<GameObject>(); //list of the nodes that have already been considered
- public List<GameObject> path_nodes = new List<GameObject>(); //the nodes of the final path
- public GameObject target;
- public GameObject my_node;
- GetClosestNode targ_node;
- GetClosestNode actual_node;
- NodeManager node_manager;
- EnemyVars en_vars;
- AdjacentNodes my_node_adjacent_nodes;//the nodes adjacent to my_node
- RaycastHit2D ray_dx;
- RaycastHit2D ray_sx;
- RaycastHit2D ray_up;
- RaycastHit2D ray_down;
- public Color a;
- public Color b;
- public Color c;
- public Color d;
- public bool found_path;
- public bool recalculate_path;
- //for path follow
- public bool reached_dest; //if has reached the target
- public int current_node = 0;
- public float node_dist;
- Vector3 dest;
- Vector3 offset;
- float start_recalculate_time;
- public float recalculate_time;
- public bool delete_lists;
- // Use this for initialization
- void Start()
- {
- targ_node = target.GetComponent<GetClosestNode>();
- actual_node = gameObject.GetComponent<GetClosestNode>();
- node_manager = GameObject.FindGameObjectWithTag("nodes manager").GetComponent<NodeManager>();
- en_vars = gameObject.GetComponent<EnemyVars>();
- start_recalculate_time = recalculate_time;
- recalculate_path = true;
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.R))
- {
- ResetPathLists();
- delete_lists = true;
- }
- //starts the pathfinding
- if (recalculate_path)
- {
- FindPath(actual_node.my_node, targ_node.my_node);
- recalculate_path = false;
- }
- recalculate_time -= Time.deltaTime;
- if (recalculate_time <= 0)
- {
- recalculate_time = start_recalculate_time;
- recalculate_path = true;
- }
- //if lines have been spawned recalculate everything
- if (Input.GetKeyDown(KeyCode.Space))
- {
- delete_lists = true;
- ResetPathLists();
- FindPath(actual_node.my_node, targ_node.my_node);
- found_path = false;
- }
- if (found_path)
- {
- FollowPath();
- }
- }
- public void FindPath(GameObject start_node, GameObject target_node)
- {
- #region DEBUG colors the nodes
- //DEBUG colors the nodes
- if (open_list.Count > 0)
- foreach (GameObject go in open_list)
- {
- go.GetComponent<SpriteRenderer>().color = a;
- }
- if (closed_list.Count > 0)
- foreach (GameObject go in closed_list)
- {
- if (!path_nodes.Contains(go))
- go.GetComponent<SpriteRenderer>().color = b;
- }
- #endregion
- #region A*
- //adds the first node in the open list
- my_node = start_node;
- if (!open_list.Contains(my_node))
- {
- open_list.Add(my_node);
- }
- if (open_list.Count > 0)
- {
- //sorts by movement cost
- open_list = open_list.OrderBy(x => x.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject, x, target_node)).ToList();
- my_node = open_list.First();
- my_node_adjacent_nodes = my_node.GetComponent<AdjacentNodes>();
- //puts my node in the closed list
- if (!closed_list.Contains(my_node))
- {
- open_list.Remove(my_node);
- closed_list.Add(my_node);
- }
- //we found a way!
- if (closed_list.Contains(target_node))
- {
- BackTracePath();
- found_path = true;
- }
- else
- {
- found_path = false;
- }
- //finds the adjacent nodes
- foreach (GameObject node in my_node_adjacent_nodes.adjacent_nodes)
- {
- if (closed_list.Contains(node))
- {
- continue;
- }
- if (!open_list.Contains(node) && node.GetComponent<GetIfWalkable>().is_node_walkable)
- {
- open_list.Add(node);
- node.GetComponent<NodeParent>().parent = my_node;
- node.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject, node, target_node);
- }
- }
- }
- }
- void ResetPathLists()
- {
- if (open_list.Count > 0 && closed_list.Count > 0 && path_nodes.Count > 0 && delete_lists)
- {
- open_list.Clear();
- closed_list.Clear();
- path_nodes.Clear();
- foreach (GameObject node in node_manager.nodes)
- {
- node.GetComponent<SpriteRenderer>().color = Color.black;
- }
- }
- delete_lists = false;
- }
- //manhattan distance
- static public int Dist(GameObject node1, GameObject node2)
- {
- return Mathf.Abs(node2.GetComponent<GetNodeCoord>().NodeCoordX - node1.GetComponent<GetNodeCoord>().NodeCoordX) +
- Mathf.Abs(node2.GetComponent<GetNodeCoord>().NodeCoordY - node1.GetComponent<GetNodeCoord>().NodeCoordY);
- }
- #region path backtracing
- //makes the final path
- void BackTracePath()
- {
- GameObject node = targ_node.my_node;
- //finds the parent
- do
- {
- if (!path_nodes.Contains(node))
- {
- path_nodes.Add(node);
- }
- node.GetComponent<SpriteRenderer>().color = d;
- node = node.GetComponent<NodeParent>().parent;
- } while (node != null);
- //reverses the list
- path_nodes.Reverse();
- //finds the children nodes
- for (int i = path_nodes.Count - 1; i > 0; i--)
- {
- GameObject child_node = path_nodes[i];
- //if the parent isn't null
- if (child_node.GetComponent<NodeParent>().parent != null)
- {
- GameObject curr = child_node.GetComponent<NodeParent>().parent;
- curr.GetComponent<NodeParent>().child = child_node;
- }
- else//otherwise stop
- {
- break;
- }
- }
- }
- #endregion
- #endregion
- #region path follow
- //follow the path
- void FollowPath()
- {
- node_dist = Vector2.Distance(transform.position, dest);
- GameObject curr = path_nodes[current_node];
- Vector2 dir = curr.transform.position - transform.position;
- if (dir.magnitude > 0.2f)
- {
- transform.position = Vector3.MoveTowards(transform.position, curr.transform.position, en_vars.speed * Time.deltaTime);
- transform.rotation = UtilityFunctions.FaceTo(curr.transform.position, transform.position);
- }
- else
- {
- current_node++;
- if (current_node >= path_nodes.Count)
- {
- current_node = 0;
- }
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement