Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AStar {
- public List<Node> openNodes;
- public List<Node> closedNodes;
- public List<Node> pathNodes;
- public int exploreCount;
- public Node finalNode;
- public AStar()
- {
- openNodes = new List<Node>();
- closedNodes = new List<Node>();
- pathNodes = new List<Node>();
- exploreCount = 0;
- }
- public List<Node> ExecuteAStar(Node startNode, Node endNode)
- {
- Debug.Log("Se llama a Execute");
- Debug.Log(startNode.name);
- Debug.Log(endNode.name);
- ResetAStar();
- finalNode = endNode;
- openNodes.Add(startNode);
- startNode.weight = 0;
- Explore(startNode);
- pathNodes.Reverse();
- //Manager.instance.UpdateLists(); //esto es para ver en el inspector las listas
- if (pathNodes.Count == 0) Debug.Log("AStar failed");
- return pathNodes;
- }
- public void ResetAStar()
- {
- exploreCount = 0;
- Debug.Log("Se llama a Reset");
- foreach (Node open in openNodes)
- open.ResetNode();
- foreach (Node closed in openNodes)
- closed.ResetNode();
- foreach (Node pth in pathNodes)
- {
- pth.ResetNode();
- }
- pathNodes.Clear();
- openNodes.Clear();
- closedNodes.Clear();
- }
- private void Explore(Node toExplore)
- {
- exploreCount++;
- Debug.Log(toExplore.name + " and explore count: " + exploreCount);
- if (exploreCount > 500) return;
- Debug.Log("Se llama a Explore");
- for (int i = toExplore.neighbours.Count - 1; i >= 0; i--) // busca en los neighbours
- {
- if (!closedNodes.Contains(toExplore.neighbours[i])) //si no esta en closed
- {
- if (!openNodes.Contains(toExplore.neighbours[i])) //y tampoco en open
- {
- openNodes.Add(toExplore.neighbours[i]); //lo agrega a open
- }
- float distance = toExplore.weight + toExplore.distanceToNode[i];
- if (distance < toExplore.neighbours[i].weight)
- {
- toExplore.neighbours[i].weight = distance;
- toExplore.neighbours[i].parent = toExplore;
- }
- }
- }
- openNodes.Remove(toExplore);
- closedNodes.Add(toExplore);
- if (toExplore == finalNode)
- {
- pathNodes.Add(toExplore);
- Debug.Log("A* encontro al final");
- Node father = toExplore.parent; //setea el primer parent, con esto va a ir pasando todo el camino
- while (father != null)
- {
- pathNodes.Add(father); //agrega al parent
- // Debug.Log("Father: " + father.name);
- father = father.parent; //pone que el parent sea su propio parent para ir al siguiente del camino hasta llegar al start
- }
- }
- else
- {
- if (openNodes.Count > 0)
- {
- Node nextToExplore = openNodes[0]; //pasa al primero de open
- float fitness = Mathf.Infinity; //punto de partida para determinar el peso del nodo
- foreach (Node item in openNodes)
- {
- float d = Vector3.Distance(toExplore.transform.position, finalNode.transform.position); //distancia pura al nodo final
- float h = Mathf.Abs(item.transform.position.x - finalNode.transform.position.x) + //suma las distancias por eje
- Mathf.Abs(item.transform.position.y - finalNode.transform.position.y) +
- Mathf.Abs(item.transform.position.z - finalNode.transform.position.z);
- float f = d + h; //peso del nodo
- if (fitness > f)
- {
- nextToExplore = item; //como arranca en infinito siempre va para ese, a no ser que haya uno con menos peso
- fitness = f;
- }
- }
- Explore(nextToExplore);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement