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 AIEn : MonoBehaviour
- {
- public List<GameObject> open_list = new List<GameObject>(); //lista dei nodes che devono essere considerati per trovare il percorso
- public List<GameObject> closed_list = new List<GameObject>(); //lista dei nodes già considerati
- public List<GameObject> adjacent_nodes = new List<GameObject>();
- public List<GameObject> path_nodes = new List<GameObject>(); //i nodes nella path finale
- public GameObject target;
- public GameObject my_node;
- GetClosestNode targ_node;
- GetClosestNode actual_node;
- NodeManager node_manager;
- EnemyVars en_vars;
- RaycastHit2D ray_dx;
- RaycastHit2D ray_sx;
- RaycastHit2D ray_up;
- RaycastHit2D ray_down;
- //just for debug to give a color to the nodes
- public Color a;
- public Color b;
- public Color c;
- public Color d;
- bool start_ai;
- // 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>();
- }
- // Update is called once per frame
- void Update()
- {
- //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 && start_ai)
- foreach (GameObject go in closed_list)
- {
- go.GetComponent<SpriteRenderer>().color = b;
- }
- if (Input.GetKeyDown(KeyCode.R))
- {
- start_ai = true;
- }
- //adds the first node to the open list
- my_node = actual_node.my_node;
- if (!open_list.Contains(my_node))
- {
- open_list.Add(my_node);
- }
- if (open_list.Count > 0 && start_ai)
- {
- //sorts by movement cost (movement cost here is F)
- open_list = open_list.OrderBy(x => x.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject,x,targ_node.my_node)).ToList();
- my_node = open_list.First();
- //puts my_node in the closed_list
- if (!closed_list.Contains(my_node))
- {
- open_list.Remove(my_node);
- closed_list.Add(my_node);
- }
- //found a path!
- if (closed_list.Contains(targ_node.my_node))
- {
- start_ai = false;
- BackTracePath();
- }
- //finds the adjacent nodes
- FindAdjacentNodes(my_node);
- foreach (GameObject node in adjacent_nodes)
- {
- if (closed_list.Contains(node))
- {
- continue;
- }
- if (!open_list.Contains(node))
- {
- open_list.Add(node);
- node.GetComponent<NodeParent>().parent = my_node;
- node.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject, node, targ_node.my_node);
- node.GetComponent<GetNodeMovementCost>().GetGCost(gameObject, node);
- }
- else
- {
- int tentative_g_score;
- if (node.GetComponent<NodeParent>().parent != null)
- {
- tentative_g_score = node.GetComponent<GetNodeMovementCost>().GetGCost(gameObject,node);
- }
- else
- {
- tentative_g_score = 0;
- }
- }
- }
- }
- }
- void FindAdjacentNodes(GameObject node)
- {
- //8 directions raycast
- RaycastHit2D ray_dx = Physics2D.Raycast(node.transform.position, Vector2.right, node_manager.NodesDistance);
- RaycastHit2D ray_sx = Physics2D.Raycast(node.transform.position, -Vector2.right, node_manager.NodesDistance);
- RaycastHit2D ray_up = Physics2D.Raycast(node.transform.position, Vector2.up, node_manager.NodesDistance);
- RaycastHit2D ray_down = Physics2D.Raycast(node.transform.position, -Vector2.up, node_manager.NodesDistance);
- //also made * square of 2 to find the lenght of the diagonal of a square because the nodes have all the same distance
- RaycastHit2D ray_up_dx = Physics2D.Raycast(node.transform.position, Vector2.right + Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
- RaycastHit2D ray_up_sx = Physics2D.Raycast(node.transform.position, -Vector2.right + Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
- RaycastHit2D ray_down_dx = Physics2D.Raycast(node.transform.position, Vector2.right - Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
- RaycastHit2D ray_down_sx = Physics2D.Raycast(node.transform.position, -Vector2.right - Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
- AddAdjacentNodes(ray_dx);
- AddAdjacentNodes(ray_sx);
- AddAdjacentNodes(ray_up);
- AddAdjacentNodes(ray_down);
- AddAdjacentNodes(ray_up_dx);
- AddAdjacentNodes(ray_up_sx);
- AddAdjacentNodes(ray_down_dx);
- AddAdjacentNodes(ray_down_sx);
- }
- void AddAdjacentNodes(RaycastHit2D ray)
- {
- if (ray.collider != null && ray.collider.gameObject.tag == "node" && !adjacent_nodes.Contains(ray.collider.gameObject) &&
- !closed_list.Contains(ray.collider.gameObject) && ray.collider.gameObject.GetComponent<GetIfWalkable>().is_node_walkable)
- {
- adjacent_nodes.Add(ray.collider.gameObject);
- }
- }
- //Finds the H cost using the manhattand distance method
- 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);
- }
- //makes the final path
- void BackTracePath()
- {
- GameObject node = targ_node.my_node;
- do
- {
- if (!path_nodes.Contains(node))
- {
- path_nodes.Add(node);
- }
- node.GetComponent<SpriteRenderer>().color = d;
- node = node.GetComponent<NodeParent>().parent;
- } while (node != null);
- //mette all'inizio della lista il node del gameobject
- if(!path_nodes.Contains(actual_node.my_node))
- {
- path_nodes.Insert(0, actual_node.my_node);
- }
- //inverte l'ordine dei nodes nella lista
- path_nodes.Reverse();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement