Example #1
0
        public void setBlock(BlockPos pos, EnumBlock blockType, int meta, bool redraw)
        {
            var chunk = getChunkFromPos(pos);

            if (chunk == null)
            {
                var chp = pos.ChunkPos();

                ThreadPool.RunTask(true, () =>
                {
                    generateChunk(chp, false);
                    setBlock(pos, blockType, meta, redraw);
                });

                return;
            }

            chunk.setBlock(pos - chunk.chunkPos, blockType, meta);

            if (redraw)
            {
                updateModelForChunk(chunk.chunkPos);
                markNeighbourChunksForUpdate(chunk, pos);
            }
        }
Example #2
0
        public Chunk getChunkFromPos(BlockPos pos)
        {
            if (!_chunks.TryGetValue(pos.ChunkPos(), out var chunkData))
            {
                return(null);
            }

            return(chunkData?.chunk);
        }
Example #3
0
        public void updateModelForChunk(BlockPos pos)
        {
            if (_chunks.TryGetValue(pos.ChunkPos(), out var node))
            {
                if (!node.chunkGenerated)
                {
                    return;
                }

                node.modelGenerated = true;

                ThreadPool.RunTask(false, () =>
                {
                    var model  = node.chunk.generateModel(this, node.model);
                    node.model = model;
                });
            }
        }
Example #4
0
 public bool doesChunkHaveModel(BlockPos pos)
 {
     return(_chunks[pos.ChunkPos()].modelGenerated);
 }
Example #5
0
        public void generateChunk(BlockPos pos, bool redraw)
        {
            var chunkPos = pos.ChunkPos();

            if (_chunks.ContainsKey(chunkPos))
            {
                return;
            }

            var chunk = new Chunk(chunkPos);
            var data  = new ChunkData(chunk, new ChunkModel());

            _chunks.TryAdd(chunkPos, data);

            for (int z = 0; z < 16; z++)
            {
                for (int x = 0; x < 16; x++)
                {
                    var X = (x + chunkPos.x) / 1.25f;
                    var Y = (z + chunkPos.z) / 1.25f;

                    int peakY = 32 + (int)Math.Abs(MathHelper.Clamp(0.35f + noise.GetPerlinFractal(X, Y), 0, 1) * 30);

                    for (int y = peakY; y >= 0; y--)
                    {
                        var p = new BlockPos(x, y, z);

                        if (y == peakY)
                        {
                            chunk.setBlock(p, EnumBlock.GRASS, 0);
                        }
                        else if (y > 0 && peakY - y > 0 && peakY - y < 3) // for 2 blocks
                        {
                            chunk.setBlock(p, EnumBlock.DIRT, 0);
                        }
                        else if (y == 0)
                        {
                            chunk.setBlock(p, EnumBlock.BEDROCK, 0);
                        }
                        else
                        {
                            var f = 0.35f + noise.GetNoise(X * 32 - y * 16, Y * 32 + x * 16);

                            chunk.setBlock(p, f >= 0.75f ? EnumBlock.RARE : EnumBlock.STONE, 0);
                        }
                    }
                }
            }

            if (redraw)
            {
                updateModelForChunk(chunkPos);
            }

            markNeighbourChunksForUpdate(chunk, chunk.chunkPos);

            data.chunkGenerated = true;

            /* var sides = (EnumFacing[]) Enum.GetValues(typeof(EnumFacing));
             *
             * for (var index = 0; index < sides.Length - 2; index++)
             * {
             *   var side = sides[index];
             *
             *   var vec = new BlockPos().offset(side).vector;
             *   var offset = new BlockPos(vec * 16);
             *
             *   var c = getChunkFromPos(offset + chunkPos);
             *
             *   if (c != null)
             *       updateModelForChunk(c.chunkPos);
             * }*/
        }