예제 #1
0
        private TerrainBlock.Type SetBlockTypeAt(TerrainChunk chunk, Index3D blockLocalIndex, TerrainBlock.Type blockNewType)
        {
            var blockType = chunk.GetBlock(blockLocalIndex).BlockType;

            chunk.SetBlockType(blockLocalIndex, blockNewType);
            chunkGenerator.BuildMeshFor(chunk);
            return(blockType);
        }
        /// <summary>
        /// This was also inspired by Sam's code: https://github.com/samhogan/Minecraft-Unity3D
        /// However, I've changed the algorithm to fix an issue with trees too close to the
        /// border of the chunk. I calculate the size of the leaves first, then I use this size
        /// to constrain where the trunk can be inside the chunk: I avoid placing it too close
        /// to the border of the chunk.
        /// </summary>
        public void Generate(FastNoise noise)
        {
            var simplex = noise.GetSimplex(chunk.Index.X * 0.8f, chunk.Index.Z * 0.8f);

            if (simplex > 0.0f)
            {
                var numberOfTrees = rand.Next(0, (int)(10.0f * simplex));

                for (var i = 0; i < numberOfTrees; ++i)
                {
                    var treeHeight      = rand.Next(3, 14);
                    var leavesWidth     = rand.Next(2, 8);
                    var leavesHalfWidth = leavesWidth / 2;

                    var trunkX = rand.Next(chunk.MinBlockIndex.X + leavesHalfWidth, chunk.MaxBlockIndex.X - leavesHalfWidth + 1);
                    var trunkZ = rand.Next(chunk.MinBlockIndex.Z + leavesHalfWidth, chunk.MaxBlockIndex.Z - leavesHalfWidth + 1);

                    var trunkBlocks = CalculateTreeTrunkIndices(treeHeight, trunkX, trunkZ).ToList();

                    if (trunkBlocks.Count() > 0)
                    {
                        foreach (var index in trunkBlocks)
                        {
                            chunk.SetBlockType(index, TerrainBlock.Type.TreeTrunk);
                        }

                        var trunkBaseIndex = trunkBlocks.FirstOrDefault();

                        foreach (var index in CalculateTreeLeavesIndices(trunkBaseIndex, treeHeight, leavesHalfWidth))
                        {
                            chunk.SetBlockType(index, TerrainBlock.Type.TreeLeaves);
                        }
                    }
                }
            }
        }