예제 #1
0
    private void MoveToOpenList(GameObject _tile, int _GCost)
    {
        EnvironmentBlockScript _tileScript = _tile.GetComponent <EnvironmentBlockScript>();

        if (_tileScript.Fcost != -1)
        {
            if (_tileScript.Gcost > _GCost)
            {
                _tileScript.Gcost      = _GCost;
                _tileScript.Fcost      = _GCost + _tileScript.Hcost;
                _tileScript.ParentTile = CurrentTile;
            }
            _tileScript.Open = true;
        }
        else
        {
            Vector2 tilePosition        = _tile.GetComponent <Transform>().position;
            Vector2 destinationPosition = DestinationTile.GetComponent <Transform>().position;
            float   xDifference         = Mathf.Abs(destinationPosition.x - tilePosition.x) / 8;
            float   yDifference         = Mathf.Abs(destinationPosition.y - tilePosition.y) / 8;
            int     HCost = Mathf.RoundToInt(xDifference + yDifference);
            _tileScript.Hcost = HCost;
            _tileScript.Gcost = _GCost;
            _tileScript.Fcost = HCost + _GCost;
            if (_tile != CurrentTile)
            {
                _tileScript.ParentTile = CurrentTile;
            }
            OpenList.Add(_tile);
            _tileScript.Open = true;
        }
    }
예제 #2
0
    private GameObject AStar(GameObject _destinationTile)
    {
        // We need to define the path to the player, and then return the first tile in that path.
        // --- SETUP ---

        UpdateFloorTile();

        SourceTile      = CurrentFloorTile;
        DestinationTile = _destinationTile;

        CurrentTile = SourceTile;
        MoveToOpenList(CurrentTile, 0);
        int  currentGCost;
        bool Done = false;

        while (!Done)
        {
            EnvironmentBlockScript CurrentTileScript = CurrentTile.GetComponent <EnvironmentBlockScript>();
            currentGCost = CurrentTileScript.Gcost;

            // Going through tiles north, east, south, west.

            // The north tile
            GameObject northTile  = CurrentTileScript.northTile;
            bool       northValid = false;
            if (northTile != null)
            {
                northValid = IsTileValid(northTile);
                if (northValid)
                {
                    MoveToOpenList(northTile, currentGCost + 10);
                }
            }

            // The east tile
            GameObject eastTile  = CurrentTileScript.eastTile;
            bool       eastValid = false;
            if (eastTile != null)
            {
                eastValid = IsTileValid(eastTile);
                if (eastValid)
                {
                    MoveToOpenList(eastTile, currentGCost + 10);
                }
            }

            // The south tile
            GameObject southTile  = CurrentTileScript.southTile;
            bool       southValid = false;
            if (southTile != null)
            {
                southValid = IsTileValid(southTile);
                if (southValid)
                {
                    MoveToOpenList(southTile, currentGCost + 10);
                }
            }

            // The west tile
            GameObject westTile  = CurrentTileScript.westTile;
            bool       westValid = false;
            if (westTile != null)
            {
                westValid = IsTileValid(westTile);
                if (westValid)
                {
                    MoveToOpenList(westTile, currentGCost + 10);
                }
            }

            // Move that tile from the open list to the closed list
            MoveToClosedList(CurrentTile);

            if (OpenList.Count == 0)
            {
                Done = true;
                break;
            }

            int        lowestFCost     = 0;
            GameObject lowestFCostTile = null;
            for (int i = 0; i < OpenList.Count; i++)
            {
                int IFCost = OpenList[i].GetComponent <EnvironmentBlockScript>().Fcost;
                if (i == 0)
                {
                    lowestFCost     = IFCost;
                    lowestFCostTile = OpenList[i];
                }
                else if (IFCost < lowestFCost)
                {
                    lowestFCost     = IFCost;
                    lowestFCostTile = OpenList[i];
                }
            }
            CurrentTile = lowestFCostTile;

            if (CurrentTile == DestinationTile)
            {
                Done = true;
            }
        }

        if (CurrentTile == DestinationTile)
        {
            while (CurrentTile.GetComponent <EnvironmentBlockScript>().ParentTile != SourceTile)
            {
                CurrentTile = CurrentTile.GetComponent <EnvironmentBlockScript>().ParentTile;
            }
        }
        else
        {
            CurrentTile = CurrentFloorTile;
        }
        return(CurrentTile);
    }