/// <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(ref Vector3Int pos, out Chunk chunk) { Assert.IsTrue(Helpers.IsMainThread); Vector3Int p = Chunk.ContainingChunkPos(ref pos); // Let's keep it within allowed world bounds if (!world.IsCoordInsideWorld(ref p)) { 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(ref p); if (chunk != null) { return(false); } // Create a new chunk chunk = Chunk.CreateChunk(world, p); lock (chunks) { chunks.Add(p, chunk); } return(true); }
/// <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); }