Esempio n. 1
0
        public void BuildColumn(IEnumerable <BatchUpdateItem <LightBlockItem> > fill)
        {
            var lightProcessor = new LightProcessor(ChunkSize, ChunkSize, ChunkSize);
            var chunkPool      = PoolManager.GetObjectPool <Chunk>();

            foreach (var batchUpdateItem in fill)
            {
                var    pos        = batchUpdateItem.Position;
                var    chunkIndex = pos.y / ChunkSize;
                IChunk chunk;
                if (chunkIndex < _chunks.Count)
                {
                    chunk = _chunks[chunkIndex];
                }
                else
                {
                    if (batchUpdateItem.Item.Block.Equals(BlockFactory.Empty) || batchUpdateItem.Item.Block.Equals(BlockFactory.BottomOfWorld))
                    {
                        continue;
                    }
                    var newChunk = chunkPool.Pop();
                    newChunk.Initiailize(new Vector3Int(Offset.x, chunkIndex * ChunkSize, Offset.y), ColumnId + chunkIndex);
                    _chunks.Add(newChunk);
                    chunk    = newChunk;
                    _height += ChunkSize;
                    lightProcessor.GrowArray(_height);
                }
                lightProcessor.SetupBufferStep(batchUpdateItem);
                chunk.UpdateBlock(pos.x, pos.y % ChunkSize, pos.z, batchUpdateItem.Item.Block);
            }
            var buffer = lightProcessor.Light(GetLight);

            _lightData = PoolManager.GetObjectPool <CompressableArray <uint> >().Pop();
            _lightData.Initialize(buffer, ChunkSize, _height);
            lightProcessor.Dispose();

            var missingChunks = MaxHeight / ChunkSize - _chunks.Count;

            for (var y = 0; y < missingChunks; ++y)
            {
                var fake = PoolManager.GetObjectPool <FakeChunk>().Pop();
                fake.Initiailize(new Vector3Int(Offset.x, _chunks.Count * ChunkSize, Offset.y), ColumnId + _chunks.Count, BlockFactory.Empty);
                _chunks.Add(fake);
            }

            for (var y = 0; y < _chunks.Count; y++)
            {
                var chunk = _chunks[y];
                chunk.PhysicsState    = LoadingState.Empty;
                chunk.BlockDataLoaded = true;
                foreach (var neighbor in _neighbors)
                {
                    chunk.SetNeighbor(neighbor.Key, neighbor.Value[y * ChunkSize]);
                }

                IChunk bottomNeighbor;
                if (y > 0)
                {
                    bottomNeighbor = _chunks[y - 1];
                }
                else
                {
                    var bottomOfWorld = new FakeChunk();
                    bottomOfWorld.Initiailize(Vector3Int.zero, long.MinValue, BlockFactory.BottomOfWorld);
                    bottomOfWorld.SetNeighbor(FaceDirection.XIncreasing, bottomOfWorld);
                    bottomOfWorld.SetNeighbor(FaceDirection.ZIncreasing, bottomOfWorld);
                    bottomNeighbor = bottomOfWorld;
                }
                chunk.SetNeighbor(FaceDirection.YDecreasing, bottomNeighbor);

                if (y < _chunks.Count - 1)
                {
                    chunk.SetNeighbor(FaceDirection.YIncreasing, _chunks[y + 1]);
                }
            }
        }