Exemple #1
0
        public override void GenerateChunk(Bounds area, long seed, IVoxelDataSource <VoxelData> dataSource)
        {
            int xvol = (int)area.size.x;
            int yvol = (int)area.size.y;
            int zvol = (int)area.size.z;

            int xoff = (int)area.min.x;
            int yoff = (int)area.min.y;
            int zoff = (int)area.min.z;

            Block grass = Game.BlockRegistry["Grass"];
            Block dirt  = Game.BlockRegistry["Dirt"];
            Block stone = Game.BlockRegistry["Stone"];

            for (int xx = 0; xx < xvol; xx++)
            {
                for (int zz = 0; zz < zvol; zz++)
                {
                    int landHeight = (int)System.Math.Floor(Perlin.GetValue(xx + xoff, 0, zz + zoff) * HeightHalfDiff + HeightHalfDiff + MinHeight);
                    int dirtHeight = (int)System.Math.Floor((Hash(xx + xoff, zz + zoff) >> 8 & 0xf) / 15f * 3) + 1;
                    for (int yy = 0; yy < yvol; yy++)
                    {
                        if (yoff == 0 && yy == 0)
                        {
                            // We don't want to fall through the world now do we?
                            dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)stone.Id);
                        }
                        else if (yoff + yy > landHeight)
                        {
                            continue;
                        }
                        if (yoff + yy == landHeight)
                        {
                            dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)grass.Id);
                        }
                        else if (yoff + yy >= landHeight - dirtHeight)
                        {
                            dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)dirt.Id);
                        }
                        else
                        {
                            dataSource.Set(xoff + xx, yoff + yy, zoff + zz, (VoxelData)stone.Id);
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void ChangeBlock(Vector3 position, Block block)
        {
            int id = block == null ? 0 : block.Id;

            //If we are breaking a block, trigger it's on break event
            if (block == null)
            {
                Block previousBlock = GetBlockAt(position);
                previousBlock.Destroyed(position);
            }


            //Set the block in the data source
            Debug.Log("Placing: " + position.ToString());
            dataSource.Set(position.x, position.y, position.z, new VoxelData()
            {
                Material = (byte)id
            });

            //If the block isn't null, notify that it's been placed
            if (block != null)
            {
                block.Placed(position);
            }



            //Notify the chunk that it's data source has changed and it should probably remesh
            Chunk c = LoadedChunks.Values.SingleOrDefault(x => x.bounds.Contains(position));

            if (c != null)
            {
                //rebuild neighor chunks if we are on the border
                int xx = (int)position.x / 16;
                int yy = (int)position.y / 16;
                int zz = (int)position.z / 16;


                IEnumerable <Chunk> neighbors = LoadedChunks.Values.Where(x =>
                {
                    int cx = (int)x.bounds.min.x / 16;
                    int cy = (int)x.bounds.min.y / 16;
                    int cz = (int)x.bounds.min.z / 16;

                    return(Vector3.Distance(new Vector3(xx, yy, zz), new Vector3(cx, cy, cz)) <= 1);
                });
                foreach (Chunk cnk in neighbors)
                {
                    cnk.BuildChunk();
                }

                c.BuildChunk();
            }
            else
            {
                Debug.Log("Chunk Null!");
            }
        }
Exemple #3
0
        public override void GenerateChunk(Bounds area, long seed, IVoxelDataSource <VoxelData> dataSource)
        {
            var xvol = (int)area.size.x;
            var zvol = (int)area.size.z;

            var xoff = (int)area.min.x;
            var zoff = (int)area.min.z;

            for (var xx = 0; xx < xvol; xx++)
            {
                for (var yy = 0; yy < zvol; yy++)
                {
                    for (var d = 0; d <= Height; d++)
                    {
                        dataSource.Set(xx + xoff, d, yy + zoff, new VoxelData()
                        {
                            Material = (byte)ToGenerate.Id
                        });
                    }
                }
            }
        }