Ejemplo n.º 1
0
        public Chunk(int sideSize = Env.ChunkSize)
        {
            m_sideSize = sideSize;

            // Associate Chunk with a certain thread and make use of its memory pool
            // This is necessary in order to have lock-free caches
            ThreadID = Globals.WorkPool.GetThreadIDFromIndex(s_id++);
            pools    = Globals.WorkPool.GetPool(ThreadID);

            stateManager = new ChunkStateManagerClient(this);
            blocks       = new ChunkBlocks(this, sideSize);
        }
Ejemplo n.º 2
0
        public Chunk()
        {
            // Associate Chunk with a certain thread and make use of its memory pool
            // This is necessary in order to have lock-free caches
            ThreadID = Globals.WorkPool.GetThreadIDFromIndex(s_id++);
            pools    = Globals.WorkPool.GetPool(ThreadID);

            stateManager = new ChunkStateManagerClient(this);
            blocks       = new ChunkBlocks(this);
            logic        = new ChunkLogic(this);

            GeometryHandler = new ChunkRenderGeometryHandler(this);
            ChunkColliderGeometryHandler = new ChunkColliderGeometryHandler(this);
        }
Ejemplo n.º 3
0
        public void HandleNeighbors(BlockData block, Vector3Int pos)
        {
            if (!NeedToHandleNeighbors(ref pos))
            {
                return;
            }

            int cx = chunk.pos.x;
            int cy = chunk.pos.y;
            int cz = chunk.pos.z;

            ChunkStateManagerClient stateManager = chunk.stateManager;

            // If it is an edge position, notify neighbor as well
            // Iterate over neighbors and decide which ones should be notified to rebuild their geometry
            var listeners = stateManager.Listeners;

            for (int i = 0; i < listeners.Length; i++)
            {
                ChunkEvent listener = listeners[i];
                if (listener == null)
                {
                    continue;
                }

                ChunkStateManagerClient listenerClient = (ChunkStateManagerClient)listener;
                Chunk       listenerChunk       = listenerClient.chunk;
                ChunkBlocks listenerChunkBlocks = listenerChunk.blocks;

                int lx = listenerChunk.pos.x;
                int ly = listenerChunk.pos.y;
                int lz = listenerChunk.pos.z;

                if (ly == cy || lz == cz)
                {
                    // Section to the left
                    if ((pos.x == 0) && (lx + m_sideSize == cx))
                    {
                        rebuildMaskGeometry = rebuildMaskGeometry | (1 << i);

                        // Mirror the block to the neighbor edge
                        int neighborIndex = Helpers.GetChunkIndex1DFrom3D(m_sideSize, pos.y, pos.z, m_pow);
                        listenerChunkBlocks[neighborIndex] = block;
                    }
                    // Section to the right
                    else if ((pos.x == (m_sideSize - 1)) && (lx - m_sideSize == cx))
                    {
                        rebuildMaskGeometry = rebuildMaskGeometry | (1 << i);

                        // Mirror the block to the neighbor edge
                        int neighborIndex = Helpers.GetChunkIndex1DFrom3D(-1, pos.y, pos.z, m_pow);
                        listenerChunkBlocks[neighborIndex] = block;
                    }
                }

                if (lx == cx || lz == cz)
                {
                    // Section to the bottom
                    if ((pos.y == 0) && (ly + m_sideSize == cy))
                    {
                        rebuildMaskGeometry = rebuildMaskGeometry | (1 << i);

                        // Mirror the block to the neighbor edge
                        int neighborIndex = Helpers.GetChunkIndex1DFrom3D(pos.x, m_sideSize, pos.z, m_pow);
                        listenerChunkBlocks[neighborIndex] = block;
                    }
                    // Section to the top
                    else if ((pos.y == (m_sideSize - 1)) && (ly - m_sideSize == cy))
                    {
                        rebuildMaskGeometry = rebuildMaskGeometry | (1 << i);

                        // Mirror the block to the neighbor edge
                        int neighborIndex = Helpers.GetChunkIndex1DFrom3D(pos.x, -1, pos.z, m_pow);
                        listenerChunkBlocks[neighborIndex] = block;
                    }
                }

                if (ly == cy || lx == cx)
                {
                    // Section to the back
                    if ((pos.z == 0) && (lz + m_sideSize == cz))
                    {
                        rebuildMaskGeometry = rebuildMaskGeometry | (1 << i);

                        // Mirror the block to the neighbor edge
                        int neighborIndex = Helpers.GetChunkIndex1DFrom3D(pos.x, pos.y, m_sideSize, m_pow);
                        listenerChunkBlocks[neighborIndex] = block;
                    }
                    // Section to the front
                    else if ((pos.z == (m_sideSize - 1)) && (lz - m_sideSize == cz))
                    {
                        rebuildMaskGeometry = rebuildMaskGeometry | (1 << i);

                        // Mirror the block to the neighbor edge
                        int neighborIndex = Helpers.GetChunkIndex1DFrom3D(pos.x, pos.y, -1, m_pow);
                        listenerChunkBlocks[neighborIndex] = block;
                    }
                }

                // No further checks needed once we know all neighbors need to be notified
                if (rebuildMaskGeometry == 0x3f)
                {
                    break;
                }
            }
        }
Ejemplo n.º 4
0
 public unsafe void Copy(ChunkBlocks src, int srcIndex, int dstIndex, int length)
 {
     Utils.MemoryCopy(&m_blocks[dstIndex << 1], &src.m_blocks[srcIndex << 1], (uint)length << 1);
 }
Ejemplo n.º 5
0
 public void Copy(ChunkBlocks src, int srcIndex, int dstIndex, int length)
 {
     Array.Copy(src.blocks, srcIndex, blocks, dstIndex, length);
 }