Ejemplo n.º 1
0
    void BuildChunk()
    {
        bool isSaved = LoadChunk();

        blocksInChunk = new Block[World.chunkSize, World.chunkSize, World.chunkSize];

        //create blocks and add them to 3D array
        for (int z = 0; z < World.chunkSize; z++)
        {
            for (int y = 0; y < World.chunkSize; y++)
            {
                for (int x = 0; x < World.chunkSize; x++)
                {
                    Vector3 newBlockLocalPosition = new Vector3(x, y, z);
                    Vector3 newBlockWorldPosition = new Vector3
                                                    (
                        (int)(x + chunkGameObject.transform.position.x),
                        (int)(y + chunkGameObject.transform.position.y),
                        (int)(z + chunkGameObject.transform.position.z)
                                                    );

                    // if this chunk is saved, load the block data from the save file a continue to next iteration
                    if (isSaved)
                    {
                        blocksInChunk[x, y, z] = new Block(SaveLoad.blockData.blockTypeMatrix[x, y, z], newBlockLocalPosition, this);
                        continue;
                    }

                    // build block with a block type acording to the height (y position)
                    if ((int)newBlockWorldPosition.y <= HeightGenerator.GenerateUnbreakableHeight(newBlockWorldPosition.x, newBlockWorldPosition.z))
                    {
                        blocksInChunk[x, y, z] = new Block(Block.BlockType.UNBREAKABLE, newBlockLocalPosition, this);
                    }
                    else if ((int)newBlockWorldPosition.y <= HeightGenerator.GenerateStoneHeight(newBlockWorldPosition.x, newBlockWorldPosition.z))
                    {
                        blocksInChunk[x, y, z] = new Block(Block.BlockType.STONE, newBlockLocalPosition, this);
                    }
                    else if ((int)newBlockWorldPosition.y < HeightGenerator.GenerateTerrainHeight(newBlockWorldPosition.x, newBlockWorldPosition.z))
                    {
                        blocksInChunk[x, y, z] = new Block(Block.BlockType.DIRT, newBlockLocalPosition, this);
                    }
                    else if ((int)newBlockWorldPosition.y == HeightGenerator.GenerateTerrainHeight(newBlockWorldPosition.x, newBlockWorldPosition.z))
                    {
                        blocksInChunk[x, y, z] = new Block(Block.BlockType.GRASS, newBlockLocalPosition, this);
                    }
                    else
                    {
                        blocksInChunk[x, y, z] = new Block(Block.BlockType.AIR, newBlockLocalPosition, this);
                    }
                }
            }
        }
        toBeDrawn = true;
    }
Ejemplo n.º 2
0
    public void NewGame()
    {
        ResetWorldToDefault();

        // set random offset for the height generator algorithm
        heightGeneratorOffsetX = UnityEngine.Random.Range(10000, 30000);
        heightGeneratorOffsetZ = UnityEngine.Random.Range(10000, 30000);

        HeightGenerator.offsetX = heightGeneratorOffsetX;
        HeightGenerator.offsetZ = heightGeneratorOffsetZ;

        player.transform.position = new Vector3(
            0f,
            HeightGenerator.GenerateTerrainHeight(0f, 0f) + 1,
            0f
            );
        lastPlayerPosition = player.transform.position;

        BuildWorld();
    }