Exemplo n.º 1
0
    public GameObject[,] DrawTerrain()
    {
        int mapwidth = (int)Mathf.Pow(2, heightMapSize) + 1;

        map = new GameObject[mapwidth, mapwidth];
        //seed of 1233 is good
        float[,] heightmap = DiamondSquare.CreateHeightmap(heightMapSize, 12348, 1.0f, 0.5f);
        for (int x = 0; x < heightmap.GetLength(0); x++)
        {
            for (int y = 0; y < heightmap.GetLength(1); y++)
            {
                float height = heightmap[x, y];

                GameObject newTile    = null;
                GameObject tileSprite = GetSprite(height);
                GameObject tileItem   = GetItem(tileSprite.GetComponent <Tile>().Type, x, y);

                newTile = Instantiate(tileSprite, new Vector3(x * tileWidth, y * tileWidth, 0), Quaternion.identity) as GameObject;
                Tile t = newTile.GetComponent <Tile>();
                t.AddItem(tileItem);
                newTile.name = t.Type.ToString() + " (" + x + "," + y + ")";
                t.X          = x;
                t.Y          = y;
                newTile.GetComponent <Node>().X = x;
                newTile.GetComponent <Node>().Y = y;

                map[x, y] = newTile;
            }
        }
        return(map);
    }
Exemplo n.º 2
0
    public void Generate()
    {
        GameObject  terrain = new GameObject("Terrain");
        Terrain     t       = terrain.AddComponent <Terrain>();
        TerrainData td      = t.terrainData = new TerrainData();

        td.heightmapResolution = (int)Mathf.Pow(2, n);
        td.alphamapResolution  = (int)Mathf.Pow(2, n);
        t.heightmapPixelError  = 0;
        td.SetHeights(0, 0, DiamondSquare.CreateHeightmap(n, seed, spread, spreadReductionRate));
        td.size = new Vector3(td.heightmapWidth, height, td.heightmapHeight);
        terrain.AddComponent <TerrainCollider>();
        GetComponent <TextureTerrain>().DoItBaby();
    }