Пример #1
0
        // build the data for a specific block in the world
        public override void BuildWorldChunk(WorldData world, Chunk chunk)
        {
            IntVec3 size = world.ChunkSizeBlocks;

            if (m_HeightField == null)
            {
                return;
            }

            for (int x = 0; x < size.x; x++)
            {
                int blockX = chunk.WorldPos.x + x;

                for (int z = 0; z < size.z; z++)
                {
                    int blockZ = chunk.WorldPos.z + z;
                    GenerateTerrain(chunk, x, z, blockX, blockZ, world.WorldSizeBlocks.y);
                }
            }

            if (chunk.MarkDirty())
            {
                world.AddDirtyChunk(chunk);
            }
        }
Пример #2
0
        public void SetBlock(int x, int y, int z, byte blockType, bool bMarkDirty = true)
        {
            if (PosOutsideWorld(x, y, z))
            {
                Debug.Log(string.Format("SetBlock failed point outside world: {0},{1},{2}", x, y, z));
                return;
            }

            // calc chunk and block coords
            int chunkX = x / m_ChunkSizeBlocks.x;
            int chunkY = y / m_ChunkSizeBlocks.y;
            int chunkZ = z / m_ChunkSizeBlocks.z;
            int blockX = x % m_ChunkSizeBlocks.x;
            int blockY = y % m_ChunkSizeBlocks.y;
            int blockZ = z % m_ChunkSizeBlocks.z;

            Chunk chunk = m_Chunks[chunkX, chunkY, chunkZ];

            //Debug.Log(string.Format("Setting chunk at {0},{1},{2}",chunkX, chunkY, chunkZ));

            // create new chunk if this one is empty
            // (we could check against setting air as block type and early out ?)
            if (chunk == null)
            {
                chunk = CreateChunk(chunkX, chunkY, chunkZ);
            }

            chunk.Blocks[blockX, blockY, blockZ].m_Type = blockType;

            // Mark block dirty and process surrounding blocks if needed
            if (!bMarkDirty)
            {
                return;
            }

            if (chunk.MarkDirty())
            {
                AddDirtyChunk(chunk);
            }

            // If we set a block on the chunk edge set neigbouring chunk as dirty
            if (blockX == 0 && chunkX > 0)
            {
                Chunk neighbour = m_Chunks[chunkX - 1, chunkY, chunkZ];
                if (neighbour != null && neighbour.MarkDirty())
                {
                    AddDirtyChunk(neighbour);
                }
            }

            if (blockX == m_ChunkSizeBlocks.x - 1 && chunkX < m_WorldSizeChunks.x - 1)
            {
                Chunk neighbour = m_Chunks[chunkX + 1, chunkY, chunkZ];
                if (neighbour != null && neighbour.MarkDirty())
                {
                    AddDirtyChunk(neighbour);
                }
            }

            //  Y neigbours
            if (blockY == 0 && chunkY > 0)
            {
                Chunk neighbour = m_Chunks[chunkX, chunkY - 1, chunkZ];
                if (neighbour != null && neighbour.MarkDirty())
                {
                    AddDirtyChunk(neighbour);
                }
            }

            if (blockY == m_ChunkSizeBlocks.y - 1 && chunkY < m_WorldSizeChunks.y - 1)
            {
                Chunk neighbour = m_Chunks[chunkX, chunkY + 1, chunkZ];
                if (neighbour != null && neighbour.MarkDirty())
                {
                    AddDirtyChunk(neighbour);
                }
            }

            // Z neigbours
            if (blockZ == 0 && chunkZ > 0)
            {
                Chunk neighbour = m_Chunks[chunkX, chunkY, chunkZ - 1];
                if (neighbour != null && neighbour.MarkDirty())
                {
                    AddDirtyChunk(neighbour);
                }
            }

            if (blockZ == m_ChunkSizeBlocks.z - 1 && chunkZ < m_WorldSizeChunks.z - 1)
            {
                Chunk neighbour = m_Chunks[chunkX, chunkY, chunkZ + 1];
                if (neighbour != null && neighbour.MarkDirty())
                {
                    AddDirtyChunk(neighbour);
                }
            }
        }