/// <summary> /// Ritorna la distanza euristica dei 2 nodi in parametro. /// </summary> /// <param name="nodeA"></param> /// <param name="nodeB"></param> /// <param name="_pathFindingSettings"></param> /// <returns></returns> public static int GetDistance(INode nodeA, INode nodeB, PathFindingSettings _pathFindingSettings) { if (nodeA == null || nodeB == null) { return(-10000); } int dstX = Mathf.Abs((int)nodeA.GetGridPosition().y - (int)nodeB.GetGridPosition().y); int dstY = Mathf.Abs((int)nodeA.GetGridPosition().x - (int)nodeB.GetGridPosition().x); if (dstX > dstY) { return(14 * dstY + 10 * (dstX - dstY)); } return(14 * dstX + 10 * (dstY - dstX)); }
public void Init(CellDoomstock _startPos) { //transform.position = new Vector3(_startPos.GetWorldPosition().x, _startPos.GetWorldPosition().y - 0.5f, _startPos.GetWorldPosition().z); _animator = GetComponent <Animator>(); switch (Type) { case enemyType.Tank: pathFindingSettings = PathFindingSettings.Tank; break; case enemyType.Combattenti: pathFindingSettings = PathFindingSettings.Tank; break; } transform.DOMove(new Vector3(_startPos.GetWorldPosition().x, _startPos.GetWorldPosition().y - 0.5f, _startPos.GetWorldPosition().z - 0.4f), MovementSpeed).OnComplete(() => { CurrentPosition = _startPos; }); }
public static List <INode> FindPath(this IPathFinding _this, INode startNode, INode targetNode, PathFindingSettings _settings) { List <INode> openSet = new List <INode>(); HashSet <INode> closedSet = new HashSet <INode>(); openSet.Add(startNode); // Ciclo la collezione open fino a quando ha elementi e scelgo quello con il costo minore per avanzare con la ricerca while (openSet.Count > 0) { INode node = openSet[0]; for (int i = 1; i < openSet.Count; i++) { if (openSet[i].F_Cost < node.F_Cost || openSet[i].F_Cost == node.F_Cost) { if (openSet[i].H_Cost < node.H_Cost) { node = openSet[i]; } } } openSet.Remove(node); closedSet.Add(node); // se il nodo attuale è uguale al target, ho raggiunto il target, ho terminato. if (node == targetNode) { return(RetracePath(startNode, targetNode)); } List <INode> nNodes = _this.GetNeighboursStar(node); // --------------------------------- // --------- Settings Eval --------- //if (!_settings.CanJump) { // nNodes = nNodes.Where(n => n.ContainsTag("walkable")).ToList<INode>(); //} // --------------------------------- foreach (INode neighbour in nNodes) { //if (!neighbour.isTraversable && !_settings.IgnoreObstacles || closedSet.Contains(neighbour)) { // continue; //} if (closedSet.Contains(neighbour)) { continue; } if (neighbour.isTraversable == false && neighbour != targetNode && _settings.IgnoreObstacles) { continue; } int newCostToNeighbour = node.G_Cost + GetDistance(node, neighbour, _settings); if (newCostToNeighbour < neighbour.G_Cost || !openSet.Contains(neighbour)) { neighbour.G_Cost = newCostToNeighbour; neighbour.H_Cost = GetDistance(neighbour, targetNode, _settings); neighbour.parent = node; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } return(new List <INode>()); }