コード例 #1
0
    private void CreateChunkSpawn(Chunk chunk, Vector2 startPos)
    {
        GameObject chunkParentBlock = new GameObject();

        chunkParentBlock.name = "Chunk: " + startPos.x + " / " + startPos.y;

        RenderedChunkData renderedChunkData = new RenderedChunkData();

        renderedChunkData.ChunkGameObject = chunkParentBlock;
        renderedChunkData.Position        = startPos;

        RenderedChunkData.Add(renderedChunkData);

        for (int y = 0; y < chunkSize.y; y++)
        {
            for (int x = 0; x < chunkSize.x; x++)
            {
                Vector2 checkPos = new Vector2(startPos.x + x, startPos.y + y);

                if (chunk.VoxelsInChunkData.TryGetValue(checkPos, out var voxelData))
                {
                    GameObject temp;

                    if (voxelData.Water)
                    {
                        //Spawn Water
                        temp = normalWaterBlock;
                    }
                    else if (voxelData.Sand)
                    {
                        temp = normalGroundSandBlock;
                    }
                    else
                    {
                        if (voxelData.BiomeType == BiomeTypes.Green)
                        {
                            temp = normalGroundBlock;
                        }
                        else if (voxelData.BiomeType == BiomeTypes.Saharah)
                        {
                            temp = normalGroundBlockSaharah;
                        }
                        else if (voxelData.BiomeType == BiomeTypes.Tundra)
                        {
                            temp = normalGroundBlockTundra;
                        }
                        else
                        {
                            Debug.LogError("Biome not set on voxel!!!!");
                            temp = null;
                        }
                    }

                    GameObject go = Instantiate(temp, checkPos, Quaternion.identity);


                    go.transform.SetParent(chunkParentBlock.transform);

                    GameObject Tree;

                    if (voxelData.Tree && voxelData.Land)
                    {
                        Tree = normalTreeGameObject;
                        GameObject goTree = Instantiate(Tree, checkPos, Quaternion.identity);
                        goTree.transform.SetParent(go.transform);
                    }

                    GameObject Grass;

                    if (voxelData.Grass && voxelData.Land)
                    {
                        var index = Random.Range(0, grassBlocks.Count);
                        Grass = grassBlocks[index];
                        GameObject goGrass = Instantiate(Grass, checkPos, Quaternion.identity);
                        goGrass.transform.SetParent(go.transform);
                    }

                    GameObject Flower;

                    if (voxelData.Flower && voxelData.Land)
                    {
                        Flower = normalFlowerGameObject;
                        GameObject goFlower = Instantiate(Flower, checkPos, Quaternion.identity);
                        goFlower.transform.SetParent(go.transform);
                    }

                    GameObject Bush;

                    if (voxelData.Bush && voxelData.Land)
                    {
                        Bush = normalBushGameObject;
                        GameObject goBush = Instantiate(Bush, checkPos, Quaternion.identity);
                        goBush.transform.SetParent(go.transform);
                    }
                }
            }
        }
    }
コード例 #2
0
    IEnumerator CreateChunkNew(Chunk chunk, Vector2 startPos)
    {
        GameObject chunkParentBlock = new GameObject();

        chunkParentBlock.name = "Chunk: " + startPos.x + " / " + startPos.y;

        RenderedChunkData renderedChunkData = new RenderedChunkData();

        renderedChunkData.ChunkGameObject = chunkParentBlock;
        renderedChunkData.Position        = startPos;

        RenderedChunkData.Add(renderedChunkData);

        for (int y = 0; y < chunkSize.y; y++)
        {
            for (int x = 0; x < chunkSize.x; x++)
            {
                Vector2 checkPos = new Vector2(startPos.x + x, startPos.y + y);

                if (chunk.VoxelsInChunkData.TryGetValue(checkPos, out var voxelData))
                {
                    GameObject temp;

                    if (voxelData.Water)
                    {
                        //Spawn Water
                        temp = normalWaterBlock;
                    }
                    else if (voxelData.Sand)
                    {
                        temp = normalGroundSandBlock;
                    }
                    else
                    {
                        temp = normalGroundBlock;
                    }

                    GameObject go = Instantiate(temp, checkPos, Quaternion.identity);


                    go.transform.SetParent(chunkParentBlock.transform);

                    GameObject Tree;

                    if (voxelData.Tree && voxelData.Land)
                    {
                        Tree = normalTreeGameObject;
                        GameObject goTree = Instantiate(Tree, checkPos, Quaternion.identity);
                        goTree.transform.SetParent(go.transform);
                    }

                    GameObject Grass;

                    if (voxelData.Grass && voxelData.Land)
                    {
                        var index = Random.Range(0, grassBlocks.Count);
                        Grass = grassBlocks[index];
                        GameObject goGrass = Instantiate(Grass, checkPos, Quaternion.identity);
                        goGrass.transform.SetParent(go.transform);
                    }

                    GameObject Flower;

                    if (voxelData.Flower && voxelData.Land)
                    {
                        Flower = normalFlowerGameObject;
                        GameObject goFlower = Instantiate(Flower, checkPos, Quaternion.identity);
                        goFlower.transform.SetParent(go.transform);
                    }

                    GameObject Bush;

                    if (voxelData.Bush && voxelData.Land)
                    {
                        Bush = normalBushGameObject;
                        GameObject goBush = Instantiate(Bush, checkPos, Quaternion.identity);
                        goBush.transform.SetParent(go.transform);
                    }
                }
                yield return(new WaitForEndOfFrame());
            }
        }
    }