예제 #1
0
 public void CoInitialize()
 {
     _grassId = Blocks.Register(new BlockType("Grass", true, false, true, 2));
     _rockId  = Blocks.Register(new BlockType("Rock", true, false, true, 2));
     _dirtId  = Blocks.Register(new BlockType("Dirt", true, false, true, 2));
     _sandId  = Blocks.Register(new BlockType("Sand", true, false, true, 2));
     _waterId = Blocks.Register(new BlockType("Water", false, true, false, 2));
     Chunk.SetGenerator(WorldGen.Generator);
     StaticChunkPool.Register("Main.RockChunk", new Chunk(new BlockData(_rockId)));
     StaticChunkPool.Register("Main.WaterChunk", new Chunk(new BlockData(_waterId)));
     EventBus.AddCollection(this);
 }
예제 #2
0
 public void GameLoads(object sender, GameLoadEvent load)
 {
     _rockChunkId  = StaticChunkPool.GetId("Main.RockChunk");
     _waterChunkId = StaticChunkPool.GetId("Main.WaterChunk");
 }
예제 #3
0
            public static unsafe void Generator(ChunkGeneratorContext context)
            {
                var pos     = context.Current.Position;
                var heights = new int[Chunk.RowSize, Chunk.RowSize];
                var low     = int.MaxValue;
                var high    = int.MinValue;
                {
                    for (var x = 0; x < Chunk.RowSize; x++)
                    {
                        for (var z = 0; z < Chunk.RowSize; z++)
                        {
                            var val = heights[x, z] = (int)PerlinNoise2D((pos.X * Chunk.RowSize + x) / NoiseScaleX,
                                                                         (pos.Z * Chunk.RowSize + z) / NoiseScaleZ) / 2 - 64;
                            if (val < low)
                            {
                                low = val;
                            }
                            if (val > high)
                            {
                                high = val;
                            }
                        }
                    }

                    if (pos.Y * Chunk.RowSize > high && high >= 0)
                    {
                        context.EnableCopyOnWrite(StaticChunkPool.GetAirChunk());
                        return;
                    }

                    if (0 - Chunk.RowSize >= pos.Y * Chunk.RowSize && pos.Y * Chunk.RowSize > high)
                    {
                        context.EnableCopyOnWrite(StaticChunkPool.GetAirChunk());
                        return;
                    }

                    if (pos.Y * Chunk.RowSize < low - Chunk.RowSize - 3)
                    {
                        context.EnableCopyOnWrite(_rockChunkId);
                        return;
                    }
                }
                {
                    context.EnableFullArray();
                    var blocks = context.Current.Blocks;
                    for (var x = 0; x < Chunk.RowSize; x++)
                    {
                        for (var z = 0; z < Chunk.RowSize; z++)
                        {
                            var absHeight  = heights[x, z];
                            var height     = absHeight - pos.Y * Chunk.RowSize;
                            var underWater = absHeight <= 0;
                            for (var y = 0; y < Chunk.RowSize; y++)
                            {
                                ref var block = ref blocks[x * Chunk.RowSize * Chunk.RowSize + y * Chunk.RowSize + z];
                                if (y <= height)
                                {
                                    if (y == height)
                                    {
                                        block.Id = underWater ? _sandId : _grassId;
                                    }
                                    else if (y >= height - 3)
                                    {
                                        block.Id = underWater ? _sandId : _dirtId;
                                    }
                                    else
                                    {
                                        block.Id = _rockId;
                                    }

                                    block.Brightness = 0;
                                    block.Data       = 0;
                                }
                                else
                                {
                                    block.Id         = pos.Y * Chunk.RowSize + y <= 0 ? _waterId : (ushort)0;
                                    block.Brightness = (byte)context.DaylightBrightness;
                                    block.Data       = 0;
                                }
                            }
                        }
                    }
                }