예제 #1
0
        /// <summary>Instantiates a new chunk at a given position. If the chunk already exists, it returns it</summary>
        /// <param name="pos">Position to create this chunk on in the world coordinates.</param>
        /// <param name="chunk">Chunk at a given world position</param>
        /// <returns>True if a new chunk was created. False otherwise</returns>
        public bool CreateOrGetChunk(Vector3Int pos, out Chunk chunk, bool isDedicated)
        {
            // Let's keep it withing allowed world bounds
            Vector3Int chunkPos = Chunk.ContainingCoordinates(pos);

            if (
                (world.config.minY != world.config.maxY) &&
                (chunkPos.y > world.config.maxY || chunkPos.y < world.config.minY)
                )
            {
                chunk = null;
                return(false);
            }

            // Don't recreate the chunk if it already exists
            chunk = Get(chunkPos);
            if (chunk != null)
            {
                return(false);
            }

            // Create a new chunk
            chunk = Chunk.CreateChunk(world, chunkPos, isDedicated);
            chunks.Add(chunkPos, chunk);
            return(true);
        }
예제 #2
0
        /// <summary> Returns the chunk at the given position </summary>
        /// <param name="pos">Position of the chunk in the world coordinates</param>
        /// <returns>The chunk that contains the given block position or null if there is none</returns>
        public Chunk Get(Vector3Int pos)
        {
            pos = Chunk.ContainingCoordinates(pos);

            Chunk containerChunk;

            chunks.TryGetValue(pos, out containerChunk);

            return(containerChunk);
        }
예제 #3
0
        /// <summary> Returns the chunk at the given position </summary>
        /// <param name="pos">Position of the chunk in the world coordinates</param>
        /// <returns>The chunk that contains the given block position or null if there is none</returns>
        public Chunk Get(Vector3Int pos)
        {
            pos = Chunk.ContainingCoordinates(pos);

            // If we previously searched for this chunk there is no need to look it up again

            /*if (pos == lastChunkPos && lastChunk != null)
             *  return lastChunk;
             *
             * lastChunkPos = pos;*/

            Chunk containerChunk;

            chunks.TryGetValue(pos, out containerChunk);

            return(containerChunk);
        }
예제 #4
0
        /// <summary>
        /// Sets blocks to a given value in a given range
        /// </summary>
        /// <param name="posFrom">Starting position in local chunk coordinates</param>
        /// <param name="posTo">Ending position in local chunk coordinates</param>
        /// <param name="blockData">A block to be placed on a given position</param>
        public void SetRange(Vector3Int posFrom, Vector3Int posTo, BlockData blockData)
        {
            Vector3Int chunkPosFrom = Chunk.ContainingCoordinates(posFrom);
            Vector3Int chunkPosTo   = Chunk.ContainingCoordinates(posTo);

            // Update all chunks in range
            int minY = (posFrom.y + chunkPosFrom.y) & Env.ChunkMask;

            for (int cy = chunkPosFrom.y; cy <= chunkPosTo.y; cy += Env.ChunkSize, minY = 0)
            {
                int maxY = ((minY + Env.ChunkSize) >> Env.ChunkPow) << Env.ChunkPow;
                maxY = Math.Min(maxY + cy - 1, posTo.y) & Env.ChunkMask;
                int minZ = (posFrom.z + chunkPosFrom.z) & Env.ChunkMask;

                for (int cz = chunkPosFrom.z; cz <= chunkPosTo.z; cz += Env.ChunkSize, minZ = 0)
                {
                    int maxZ = ((minZ + Env.ChunkSize) >> Env.ChunkPow) << Env.ChunkPow;
                    maxZ = Math.Min(maxZ + cz - 1, posTo.z) & Env.ChunkMask;
                    int minX = (posFrom.x + chunkPosFrom.x) & Env.ChunkMask;

                    for (int cx = chunkPosFrom.x; cx <= chunkPosTo.x; cx += Env.ChunkSize, minX = 0)
                    {
                        Chunk chunk = world.chunks.Get(new Vector3Int(cx, cy, cz));
                        if (chunk == null)
                        {
                            continue;
                        }

                        int maxX = ((minX + Env.ChunkSize) >> Env.ChunkPow) << Env.ChunkPow;
                        maxX = Math.Min(maxX + cx - 1, posTo.x) & Env.ChunkMask;

                        Vector3Int from = new Vector3Int(minX, minY, minZ);
                        Vector3Int to   = new Vector3Int(maxX, maxY, maxZ);
                        chunk.blocks.SetRange(from, to, blockData);
                    }
                }
            }
        }
예제 #5
0
        /// <summary>Instantiates a new chunk at a given position. If the chunk already exists, it returns it</summary>
        /// <param name="pos">Position to create this chunk on in the world coordinates.</param>
        /// <param name="chunk">Chunk at a given world position</param>
        /// <param name="isDedicated"></param>
        /// <returns>True if a new chunk was created. False otherwise</returns>
        public bool CreateOrGetChunk(Vector3Int pos, out Chunk chunk, bool isDedicated)
        {
            Assert.IsTrue(Helpers.IsMainThread);
            pos = Chunk.ContainingCoordinates(pos);

            // Let's keep it withing allowed world bounds
            if (
                (world.config.minY != world.config.maxY) &&
                (pos.y > world.config.maxY || pos.y < world.config.minY)
                )
            {
                chunk = null;
                return(false);
            }

            /*// If we previously searched for this chunk there is no need to look it up again
             * if (pos==lastChunkPos && lastChunk!=null)
             * {
             *  chunk = lastChunk;
             *  return false;
             * }
             *
             * lastChunkPos = pos;*/

            // Don't recreate the chunk if it already exists
            chunk = Get(pos);
            if (chunk != null)
            {
                return(false);
            }

            // Create a new chunk
            chunk = Chunk.CreateChunk(world, pos, isDedicated);
            chunks.Add(pos, chunk);
            return(true);
        }
예제 #6
0
 public void Set(Vector3Int pos, Chunk chunk)
 {
     pos         = Chunk.ContainingCoordinates(pos);
     chunks[pos] = chunk;
 }