Advertisement
KTVX94

Untitled

Dec 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AStar {
  6.  
  7.     public List<Node> openNodes;
  8.     public List<Node> closedNodes;
  9.     public List<Node> pathNodes;
  10.     public int exploreCount;
  11.  
  12.     public Node finalNode;
  13.  
  14. public AStar()
  15.     {
  16.         openNodes = new List<Node>();
  17.         closedNodes = new List<Node>();
  18.         pathNodes = new List<Node>();
  19.         exploreCount = 0;
  20.     }
  21.  
  22.     public List<Node> ExecuteAStar(Node startNode, Node endNode)
  23.     {
  24.         Debug.Log("Se llama a Execute");
  25.         Debug.Log(startNode.name);
  26.         Debug.Log(endNode.name);
  27.         ResetAStar();
  28.         finalNode = endNode;
  29.  
  30.         openNodes.Add(startNode);
  31.         startNode.weight = 0;
  32.  
  33.         Explore(startNode);
  34.  
  35.         pathNodes.Reverse();
  36.         //Manager.instance.UpdateLists(); //esto es para ver en el inspector las listas
  37.         if (pathNodes.Count == 0) Debug.Log("AStar failed");
  38.         return pathNodes;
  39.     }
  40.  
  41.     public void ResetAStar()
  42.     {
  43.         exploreCount = 0;
  44.         Debug.Log("Se llama a Reset");
  45.         foreach (Node open in openNodes)
  46.             open.ResetNode();
  47.  
  48.         foreach (Node closed in openNodes)
  49.             closed.ResetNode();
  50.  
  51.         foreach (Node pth in pathNodes)
  52.         {
  53.             pth.ResetNode();
  54.         }
  55.  
  56.         pathNodes.Clear();
  57.         openNodes.Clear();
  58.         closedNodes.Clear();
  59.     }
  60.  
  61.     private void Explore(Node toExplore)
  62.     {
  63.         exploreCount++;
  64.         Debug.Log(toExplore.name + " and explore count: " + exploreCount);
  65.         if (exploreCount > 500) return;
  66.         Debug.Log("Se llama a Explore");
  67.         for (int i = toExplore.neighbours.Count - 1; i >= 0; i--)  // busca en los neighbours
  68.         {
  69.             if (!closedNodes.Contains(toExplore.neighbours[i]))  //si no esta en closed
  70.             {
  71.                 if (!openNodes.Contains(toExplore.neighbours[i])) //y tampoco en open
  72.                 {
  73.                     openNodes.Add(toExplore.neighbours[i]); //lo agrega a open
  74.                 }
  75.                 float distance = toExplore.weight + toExplore.distanceToNode[i];
  76.                 if (distance < toExplore.neighbours[i].weight)
  77.                 {
  78.                     toExplore.neighbours[i].weight = distance;
  79.                     toExplore.neighbours[i].parent = toExplore;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         openNodes.Remove(toExplore);
  85.         closedNodes.Add(toExplore);
  86.  
  87.         if (toExplore == finalNode)
  88.         {
  89.             pathNodes.Add(toExplore);
  90.             Debug.Log("A* encontro al final");
  91.             Node father = toExplore.parent; //setea el primer parent, con esto va a ir pasando todo el camino
  92.             while (father != null)
  93.             {
  94.                 pathNodes.Add(father); //agrega al parent
  95.                // Debug.Log("Father: " + father.name);
  96.                 father = father.parent; //pone que el parent sea su propio parent para ir al siguiente del camino hasta llegar al start
  97.             }
  98.         }
  99.  
  100.         else
  101.         {
  102.             if (openNodes.Count > 0)
  103.             {
  104.  
  105.                 Node nextToExplore = openNodes[0]; //pasa al primero de open
  106.                 float fitness = Mathf.Infinity; //punto de partida para determinar el peso del nodo
  107.  
  108.                 foreach (Node item in openNodes)
  109.                 {
  110.                     float d = Vector3.Distance(toExplore.transform.position, finalNode.transform.position); //distancia pura al nodo final
  111.                     float h = Mathf.Abs(item.transform.position.x - finalNode.transform.position.x) + //suma las distancias por eje
  112.                             Mathf.Abs(item.transform.position.y - finalNode.transform.position.y) +
  113.                             Mathf.Abs(item.transform.position.z - finalNode.transform.position.z);
  114.                     float f = d + h; //peso del nodo
  115.                     if (fitness > f)
  116.                     {
  117.                         nextToExplore = item; //como arranca en infinito siempre va para ese, a no ser que haya uno con menos peso
  118.                         fitness = f;
  119.                     }
  120.                 }
  121.  
  122.                 Explore(nextToExplore);
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement