Ejemplo n.º 1
0
    /// <summary>
    /// Proper async method with task
    /// </summary>
    /// <returns></returns>
    public async Task GenerateTilesAsync()
    {
        if (h.GetLength(0) != g.GetLength(0) || g.GetLength(0) != dh.GetLength(0) ||
            h.GetLength(1) != g.GetLength(1) || g.GetLength(1) != dh.GetLength(1))
        {
            Debug.LogError("Cannot generate Tiles, dimensions disagree. Re-load heightmaps.");
            return;
        }

        int sizeX = h.GetLength(0);
        int sizeY = h.GetLength(1);

        Debug.Log($"Generating tiles (size = {TileSize}) for height map dimension: {sizeX} x {sizeY}");
        int numTilesX = Mathf.CeilToInt((float)sizeX / (float)TileSize);
        int numTilesY = Mathf.CeilToInt((float)sizeY / (float)TileSize);

        Debug.Log($"\t x-tiles ({numTilesX}), y-tiles ({numTilesY}), total ({numTilesX * numTilesY})");
        Tiles = new List <MapTile>();
        for (int x = 0; x < numTilesX; x++)
        {
            for (int y = 0; y < numTilesY; y++)
            {
                Location corner = new Location(x * TileSize, y * TileSize);
                Tiles.Add(new MapTile(
                              corner,
                              h.SubMatrix(x * TileSize, y * TileSize, TileSize, TileSize),
                              g.SubMatrix(x * TileSize, y * TileSize, TileSize, TileSize),
                              dh.SubMatrix(x * TileSize, y * TileSize, TileSize, TileSize)
                              ));
            }
        }

        Debug.Log($"\tPairing up tiles with neighbors...");
        foreach (var tile in Tiles)
        {
            foreach (var neighbor in Tiles)
            {
                if (tile != neighbor)
                {
                    if ((tile.Corner - neighbor.Corner).magnitude() == TileSize)
                    {
                        // this tile is within one TileSize, it is a neighbor
                        var dir = (neighbor.Corner - tile.Corner).ToDirection();
                        tile.AddNeighbor(neighbor, dir);
                    }
                    else if (
                        Mathf.Abs(tile.Corner.x - neighbor.Corner.x) == TileSize &&
                        Mathf.Abs(tile.Corner.y - neighbor.Corner.y) == TileSize
                        )
                    {
                        // this tile is a diagonal neighbor
                        //tile.DiagonalNeighbors.Add(neighbor);
                    }
                }
            }
        }

        // connect merged borders to neighboring tiles
        foreach (var tile in Tiles)
        {
            Debug.Log($"Assembling neighbors for {tile.Corner}...");
            tile.ConnectBordersAcrossTiles();
        }

        // purge all dangling borders that have no connections
        Debug.Log($"Deleting borders on outside edges, or with no neighbor...");
        foreach (var tile in Tiles)
        {
            tile.PurgeBorders();
        }

        // connect borders to neighbors
        Debug.Log($"Connecting all borders...");
        foreach (var tile in Tiles)
        {
            // TODO: Build a list of these assembly tasks and await them all
            //        to allow concurrect processing
            // connect all borders internal to the tile
            await tile.AssembleInternalBorderMesh();
        }

        Debug.Log("Completed generating Map Tiles");
    }