Ejemplo n.º 1
0
 public MeshTileCollection GetTerrainData(int x, int y)
 {
     using (StreamReader stream = new StreamReader(streamPath + x.ToString() + "_" + y.ToString() + ".json"))
     {
         string             json = stream.ReadToEnd();
         MeshTileCollection tc   = JsonUtility.FromJson <MeshTileCollection>(json);
         return(tc);
     }
 }
Ejemplo n.º 2
0
    public void SpawnTiles()
    {
        using (StreamReader stream = new StreamReader(streamPath))
        {
            string json = stream.ReadToEnd();

            tileCollection = JsonUtility.FromJson <MeshTileCollection>(json);
        }
    }
Ejemplo n.º 3
0
    void CreateTiles(float [,] heightNoiseArray)
    {
        int count = 0;

        tiles = new MeshTile[width * height];


        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                tiles[count] = LoadTile(i, j, heightNoiseArray[i, j]);
                count++;
            }
        }
        //y* width +x
        //Fix Tiles
        tileCollection = new MeshTileCollection(tiles, "MeshTiles");

        for (int x = 0; x < width - 1; x++)
        {
            for (int y = 0; y < height - 1; y++)
            {
                MeshTile[] neighbors = GetNeighbors(x, y, true);
                MeshTile   tile      = GetTile(x, y);
                int        sameCount = 0;
                int        diffCount = 0;
                int        type1     = 0;
                int        type2     = 0;
                foreach (MeshTile n in neighbors)
                {
                    if (n.t == tile.t)
                    {
                        sameCount++;
                    }
                    if (n.t != tile.t && n.t != MeshTile.Type.Empty)
                    {
                        diffCount++;
                    }
                }

                if (diffCount > sameCount)
                {
                    int rand = (int)Random.Range(0, 7);

                    while (tile.t != GetNeighbors(x, y, true)[rand].t)
                    {
                        if (GetNeighbors(x, y, true)[rand].t != MeshTile.Type.Empty)
                        {
                            tileCollection.tiles[y * width + x].t = GetNeighbors(x, y, true)[rand].t;
                            rand = (int)Random.Range(0, 7);
                        }
                        else
                        {
                            rand = (int)Random.Range(0, 7);
                        }
                    }
                }
            }
        }

        using (StreamWriter stream = new StreamWriter(streamPath))
        {
            string json = JsonUtility.ToJson(tileCollection, true);
            stream.Write(json);
        }
    }
Ejemplo n.º 4
0
    public void GenerateChunkedTiles(int startX, int startY, float[,] heightNoiseArray)
    {
        int count = 0;

        tiles = new MeshTile[chunkSize * chunkSize];

        for (int x = startX; x < startX + chunkSize; x++)
        {
            for (int y = startY; y < startY + chunkSize; y++)
            {
                float currentHeight = heightNoiseValues[x, y];
                float currentMoist  = moistNoiseValues[x, y];
                float currentTemp   = tempNoiseValues[x, y];

                for (int i = 0; i < regions.Length; i++)
                {
                    if (currentHeight <= regions[i].height)
                    {
                        //If region is grass spawn trees, rocks, and bushes

                        if (i == 3)
                        {
                            int biomeID = 4;

                            for (int f = 0; f < forests.Length; f++)
                            {
                                if (currentMoist >= forests[f].minMoist && currentMoist <= forests[f].maxMoist)
                                {
                                    if (currentTemp >= forests[f].minTemp && currentTemp <= forests[f].maxTemp)
                                    {
                                        biomeID = f;
                                    }
                                }
                            }

                            float r = Random.Range(0, 4000);
                            if (r <= 150)
                            {
                                tiles[count] = new MeshTile(i, x, y, 1, 100, 10, Mathf.RoundToInt(Random.Range(0, 4)), biomeID);
                            }
                            else if (r >= 750 && r < 900)
                            {
                                tiles[count] = new MeshTile(i, x, y, 2, 100, 10, Mathf.RoundToInt(Random.Range(0, 5)), biomeID);
                            }
                            else if (r >= 400 && r <= 500)
                            {
                                tiles[count] = new MeshTile(i, x, y, 3, 100, 10, Mathf.RoundToInt(Random.Range(0, 2)), biomeID);
                            }
                            else
                            {
                                tiles[count] = new MeshTile(i, x, y, 0, 0, 0, 0, biomeID);
                            }
                        }
                        else
                        {
                            tiles[count] = new MeshTile(i, x, y, 0);
                        }

                        count++;
                        break;
                    }
                }
            }
        }

        tileCollection = new MeshTileCollection(tiles, "worldTiles");

        using (StreamWriter stream = new StreamWriter(streamPath + startX.ToString() + "_" + startY.ToString() + ".json"))
        {
            string json = JsonUtility.ToJson(tileCollection, false);
            stream.Write(json);
            //   Debug.Log(streamPath + startX.ToString() + "_" + startY.ToString() + ".json");
        }
    }