Esempio n. 1
0
        IEnumerator Path(Square startSquare, Square targetSquare)
        {
            List <Square> openSet   = new List <Square>();
            List <Square> closedSet = new List <Square>();

            openSet.Add(startSquare);
            BoardSpec boardSpec = BoardSpec.getInstance();

            while (openSet.Count > 0)
            {
                Square currentSquare = openSet[0];
                for (int i = 1; i < openSet.Count; i++)
                {
                    if (openSet[i].fCost < currentSquare.fCost || openSet[i].fCost == currentSquare.fCost && openSet[i].hCost < currentSquare.hCost)
                    {
                        currentSquare = openSet[i];
                    }
                }

                openSet.Remove(currentSquare);
                closedSet.Add(currentSquare);

                if (currentSquare == targetSquare)
                {
                    RetracePath(startSquare, targetSquare);
                    yield break;
                }

                foreach (Square neighbour in boardSpec.GetNeighbours(currentSquare))
                {
                    if ((!neighbour.canWalk(startSquare.character) && (neighbour.character == null || (neighbour.character != targetSquare.character))) || closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbour = currentSquare.gCost + BoardSpec.GetDistance(currentSquare, neighbour);
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = BoardSpec.GetDistance(neighbour, targetSquare);
                        neighbour.parent = currentSquare;
                        if (neighbour.hCost == 10 && neighbour.character != null)
                        {
                            continue;
                        }
                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                }
                yield return(null);
            }
        }
Esempio n. 2
0
        public Square getNearCharacter(Vector2 startPos, string tag)
        {
            Square startSquare = BoardSpec.getInstance().SquareFromWorldPoint(startPos);
            Square finalSquare = null;

            foreach (GameObject character in GameObject.FindGameObjectsWithTag(tag))
            {
                List <Square> openSet   = new List <Square>();
                List <Square> closedSet = new List <Square>();
                openSet.Add(startSquare);
                Character.Player player = character.gameObject.GetComponent <Character.Player>();
                if (player.status.vida <= 0)
                {
                    continue;
                }
                Square targetSquare = BoardSpec.getInstance().SquareFromWorldPoint(character.transform.position);
                if (targetSquare == null)
                {
                    continue;
                }
                while (openSet.Count > 0)
                {
                    Square currentSquare = openSet[0];
                    for (int i = 1; i < openSet.Count; i++)
                    {
                        if (openSet[i].fCost < currentSquare.fCost || openSet[i].fCost == currentSquare.fCost && openSet[i].hCost < currentSquare.hCost)
                        {
                            currentSquare = openSet[i];
                        }
                    }

                    openSet.Remove(currentSquare);
                    closedSet.Add(currentSquare);

                    if (currentSquare == targetSquare)
                    {
                        if (closedSet.Count > 1)
                        {
                            Square nearLastSquare = closedSet[closedSet.Count - 1];
                            if (finalSquare == null || nearLastSquare.fCost < finalSquare.fCost || (nearLastSquare.fCost == finalSquare.fCost && nearLastSquare.hCost < finalSquare.hCost))
                            {
                                finalSquare = nearLastSquare;
                            }
                        }
                        break;
                    }

                    foreach (Square neighbour in BoardSpec.getInstance().GetNeighbours(currentSquare))
                    {
                        if (!neighbour.canWalk(startSquare.character) && (neighbour.character == null || neighbour.character != targetSquare.character) || closedSet.Contains(neighbour))
                        {
                            continue;
                        }

                        int newMovementCostToNeighbour = currentSquare.gCost + BoardSpec.GetDistance(currentSquare, neighbour);
                        if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                        {
                            neighbour.gCost = newMovementCostToNeighbour;
                            neighbour.hCost = BoardSpec.GetDistance(neighbour, targetSquare);

                            if (!openSet.Contains(neighbour))
                            {
                                openSet.Add(neighbour);
                            }
                        }
                    }
                }
            }
            return(finalSquare);
        }