コード例 #1
0
    // Gets all tiles the enemy can walk on that action
    // Only floors (exclude tiles that have colliders or enemies on them)
    private Vector3Int[,] GetWalkableTiles()
    {
        Tilemap      floor     = LevelManager.CurrentLevel.floor;
        Tilemap      colliders = LevelManager.CurrentLevel.colliders;
        List <Enemy> enemies   = LevelManager.CurrentLevel.enemies;

        floor.CompressBounds();
        BoundsInt bounds = floor.cellBounds;

        Vector3Int[,] walkableTiles = new Vector3Int[bounds.size.x, bounds.size.y];
        for (int y = bounds.yMax, i = 0; i < bounds.size.y; y--, i++)
        {
            for (int x = bounds.xMin, j = 0; j < bounds.size.x; x++, j++)
            {
                Vector3Int currentPosition = new Vector3Int(x, y, 0);

                // Collider has tiles offset at y by 1, use this to check with HasTile()
                Vector3Int currentPositionShifted = new Vector3Int(currentPosition.x - 1, currentPosition.y - 1, currentPosition.z);

                // A z other than 0 will make the tile unwalkable on the A* algorithm
                if (colliders.HasTile(currentPositionShifted))
                {
                    currentPosition.z = 1;
                }
                else if (EnemyAt(currentPosition))
                {
                    currentPosition.z = 2;
                }

                walkableTiles[j, i] = currentPosition;
            }
        }

        // Debug: Map of walkable tiles
        string debugString = "\nClick To Expand\n";

        for (int i = 0; i <= walkableTiles.GetUpperBound(1); i++)
        {
            for (int j = 0; j <= walkableTiles.GetUpperBound(0); j++)
            {
                // 0 means walkable, anything else means unwalkable
                debugString += $"{walkableTiles[j, i].z} ";
            }
            debugString += "\n";
        }
        Debug.Log(debugString);

        return(walkableTiles);
    }