Advertisement
Guest User

A* pathfinding unity

a guest
Jul 11th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Collections;
  5.  
  6. public class AIEn : MonoBehaviour
  7. {
  8.  
  9.     public List<GameObject> open_list = new List<GameObject>(); //lista dei nodes che devono essere considerati per trovare il percorso
  10.     public List<GameObject> closed_list = new List<GameObject>(); //lista dei nodes già considerati
  11.     public List<GameObject> adjacent_nodes = new List<GameObject>();
  12.     public List<GameObject> path_nodes = new List<GameObject>(); //i nodes nella path finale
  13.     public GameObject target;
  14.     public GameObject my_node;
  15.     GetClosestNode targ_node;
  16.     GetClosestNode actual_node;
  17.     NodeManager node_manager;
  18.     EnemyVars en_vars;
  19.  
  20.     RaycastHit2D ray_dx;
  21.     RaycastHit2D ray_sx;
  22.     RaycastHit2D ray_up;
  23.     RaycastHit2D ray_down;
  24.    
  25.     //just for debug to give a color to the nodes
  26.     public Color a;
  27.     public Color b;
  28.     public Color c;
  29.     public Color d;
  30.  
  31.     bool start_ai;
  32.  
  33.      // Use this for initialization
  34.     void Start()
  35.     {
  36.  
  37.         targ_node = target.GetComponent<GetClosestNode>();
  38.         actual_node = gameObject.GetComponent<GetClosestNode>();
  39.         node_manager = GameObject.FindGameObjectWithTag("nodes manager").GetComponent<NodeManager>();
  40.         en_vars = gameObject.GetComponent<EnemyVars>();
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         //DEBUG colors the nodes
  47.         if (open_list.Count > 0)
  48.             foreach (GameObject go in open_list)
  49.             {
  50.                 go.GetComponent<SpriteRenderer>().color = a;
  51.             }
  52.  
  53.         if (closed_list.Count > 0 && start_ai)
  54.             foreach (GameObject go in closed_list)
  55.             {
  56.                 go.GetComponent<SpriteRenderer>().color = b;
  57.             }
  58.  
  59.         if (Input.GetKeyDown(KeyCode.R))
  60.         {
  61.             start_ai = true;
  62.         }
  63.         //adds the first node to the open list
  64.         my_node = actual_node.my_node;
  65.         if (!open_list.Contains(my_node))
  66.         {
  67.             open_list.Add(my_node);
  68.         }
  69.  
  70.         if (open_list.Count > 0 && start_ai)
  71.         {
  72.             //sorts by movement cost (movement cost here is F)
  73.             open_list = open_list.OrderBy(x => x.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject,x,targ_node.my_node)).ToList();
  74.             my_node = open_list.First();
  75.             //puts my_node in the closed_list
  76.             if (!closed_list.Contains(my_node))
  77.             {
  78.                 open_list.Remove(my_node);
  79.                 closed_list.Add(my_node);
  80.             }
  81.             //found a path!
  82.             if (closed_list.Contains(targ_node.my_node))
  83.             {
  84.                 start_ai = false;
  85.                 BackTracePath();
  86.  
  87.             }
  88.             //finds the adjacent nodes
  89.             FindAdjacentNodes(my_node);
  90.             foreach (GameObject node in adjacent_nodes)
  91.             {
  92.                 if (closed_list.Contains(node))
  93.                 {
  94.                     continue;
  95.                 }
  96.                 if (!open_list.Contains(node))
  97.                 {
  98.                     open_list.Add(node);
  99.                     node.GetComponent<NodeParent>().parent = my_node;
  100.                     node.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject, node, targ_node.my_node);
  101.                     node.GetComponent<GetNodeMovementCost>().GetGCost(gameObject, node);
  102.                 }
  103.                 else
  104.                 {
  105.                     int tentative_g_score;
  106.                     if (node.GetComponent<NodeParent>().parent != null)
  107.                     {
  108.                         tentative_g_score = node.GetComponent<GetNodeMovementCost>().GetGCost(gameObject,node);
  109.                     }
  110.                     else
  111.                     {
  112.                         tentative_g_score = 0;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.     void FindAdjacentNodes(GameObject node)
  120.     {
  121.         //8 directions raycast
  122.         RaycastHit2D ray_dx = Physics2D.Raycast(node.transform.position, Vector2.right, node_manager.NodesDistance);
  123.         RaycastHit2D ray_sx = Physics2D.Raycast(node.transform.position, -Vector2.right, node_manager.NodesDistance);
  124.         RaycastHit2D ray_up = Physics2D.Raycast(node.transform.position, Vector2.up, node_manager.NodesDistance);
  125.         RaycastHit2D ray_down = Physics2D.Raycast(node.transform.position, -Vector2.up, node_manager.NodesDistance);
  126.         //also made * square of 2 to find the lenght of the diagonal of a square because the nodes have all the same distance
  127.         RaycastHit2D ray_up_dx = Physics2D.Raycast(node.transform.position, Vector2.right + Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
  128.         RaycastHit2D ray_up_sx = Physics2D.Raycast(node.transform.position, -Vector2.right + Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
  129.         RaycastHit2D ray_down_dx = Physics2D.Raycast(node.transform.position, Vector2.right - Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
  130.         RaycastHit2D ray_down_sx = Physics2D.Raycast(node.transform.position, -Vector2.right - Vector2.up, node_manager.NodesDistance * Mathf.Sqrt(2));
  131.  
  132.         AddAdjacentNodes(ray_dx);
  133.         AddAdjacentNodes(ray_sx);
  134.         AddAdjacentNodes(ray_up);
  135.         AddAdjacentNodes(ray_down);
  136.         AddAdjacentNodes(ray_up_dx);
  137.         AddAdjacentNodes(ray_up_sx);
  138.         AddAdjacentNodes(ray_down_dx);
  139.         AddAdjacentNodes(ray_down_sx);
  140.  
  141.     }
  142.  
  143.     void AddAdjacentNodes(RaycastHit2D ray)
  144.     {
  145.         if (ray.collider != null && ray.collider.gameObject.tag == "node" && !adjacent_nodes.Contains(ray.collider.gameObject) &&
  146.             !closed_list.Contains(ray.collider.gameObject) && ray.collider.gameObject.GetComponent<GetIfWalkable>().is_node_walkable)
  147.         {
  148.             adjacent_nodes.Add(ray.collider.gameObject);
  149.         }
  150.     }
  151.  
  152.     //Finds the H cost using the manhattand distance method
  153.     static public int Dist(GameObject node1, GameObject node2)
  154.     {
  155.         return Mathf.Abs(node2.GetComponent<GetNodeCoord>().NodeCoordX - node1.GetComponent<GetNodeCoord>().NodeCoordX) +
  156.             Mathf.Abs(node2.GetComponent<GetNodeCoord>().NodeCoordY - node1.GetComponent<GetNodeCoord>().NodeCoordY);
  157.     }
  158.  
  159.  
  160.  
  161.     //makes the final path
  162.     void BackTracePath()
  163.     {
  164.         GameObject node = targ_node.my_node;
  165.         do
  166.         {
  167.             if (!path_nodes.Contains(node))
  168.             {
  169.                 path_nodes.Add(node);
  170.             }
  171.  
  172.             node.GetComponent<SpriteRenderer>().color = d;
  173.             node = node.GetComponent<NodeParent>().parent;
  174.  
  175.         } while (node != null);
  176.  
  177.         //mette all'inizio della lista il node del gameobject
  178.         if(!path_nodes.Contains(actual_node.my_node))
  179.         {
  180.             path_nodes.Insert(0, actual_node.my_node);
  181.         }
  182.         //inverte l'ordine dei nodes nella lista
  183.         path_nodes.Reverse();
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement