Exemplo n.º 1
0
    void GenerateTilesFromHeightMap(List <float> heightMap)
    {
        //generate at origin
        SpriteRenderer spriteRenderer = OceanTile.GetComponent <SpriteRenderer>();
        Vector3        tileSize       = spriteRenderer.sprite.bounds.size;

        GameObject terrainGridGO = GameObject.Find(terrainGridGOName);

        Vector2 centeredOffset = new Vector2(-tileSize.x * (gridSize.x * 0.5f), -tileSize.y * (gridSize.y * 0.5f));

        if (terrainGridGO)
        {
            GameObject.DestroyObject(terrainGridGO);
        }

        terrainGridGO = new GameObject(terrainGridGOName);
        TerrainManager manager = terrainGridGO.AddComponent <TerrainManager>();

        manager.gridSize = gridSize;

        GameObject newTile;

        for (int x = 0; x < gridSize.x; x++)
        {
            for (int y = 0; y < gridSize.y; y++)
            {
                float heightValue = heightMap[getGridCoord(x, y)];

                if (heightValue > mountainThreshold)
                {
                    newTile = GameObject.Instantiate(MountainTile);
                }
                else if (heightValue > grassThreshold)
                {
                    newTile = GameObject.Instantiate(GrassTile);
                }
                else if (heightValue > sandThreshold)
                {
                    newTile = GameObject.Instantiate(SandTile);
                }
                else if (heightValue > waterThreshold)
                {
                    newTile = GameObject.Instantiate(WaterTile);
                }
                else //(heightValue > oceanThreshold)
                {
                    newTile = GameObject.Instantiate(OceanTile);
                }

                Tile tileScript = newTile.GetComponent <Tile>();
                newTile.name             = "Tile: " + x + "," + y;
                newTile.transform.parent = terrainGridGO.transform;
                newTile.transform.Translate(centeredOffset.x, centeredOffset.y, 0); //offset to generate centered
                newTile.transform.Translate(x * tileSize.x, y * tileSize.y, 0);
                tileScript.xCoord = x;
                tileScript.yCoord = y;
                manager.AddTile(tileScript);
            }
        }
        manager.CalculateBattlegrounds();
    }