public Block getBlock(Vector3i position, bool createChunkIfNeeded = false) { Vector3i p = MathCustom.chunkFromBlock(position, Chunk.chunkSize); if (createChunkIfNeeded) { loadChunk(p, false); } else if(!loadedChunks.ContainsKey(p)) { return Block.air; } return loadedChunks[p].getBlock(MathCustom.negModFix(position.x, Chunk.chunkSize.x), MathCustom.negModFix(position.y, Chunk.chunkSize.y), MathCustom.negModFix(position.z, Chunk.chunkSize.z)); }
public bool setBlock(Vector3i position, Block block, bool createChunkIfNeeded = false) { Vector3i p = MathCustom.chunkFromBlock(position, Chunk.chunkSize); if (createChunkIfNeeded) { loadChunk(p, false); } else { return false; } loadedChunks[p].setBlock(MathCustom.negModFix(position.x, Chunk.chunkSize.x), MathCustom.negModFix(position.y, Chunk.chunkSize.y), MathCustom.negModFix(position.z, Chunk.chunkSize.z), block); return true; }
public Chunk(Vector3i pos, World world, Game game) { this.chunkPosition = new Vector3i(pos.x, pos.y, pos.z); vbo = new VboGroup(game); fileName = world.directory + "\\chunk_" + chunkPosition.getSafeString()+".chunk"; bool loaded = false; if (File.Exists(fileName)) { loaded = load(); } if (!loaded) { generate(game, world); save(); Console.WriteLine("Generate Chunk"); } else { Console.WriteLine("Loaded Chunk"); } this.world = world; }
internal static Vector3i chunkFromBlock(int x, int y, int z, Vector3i chunkSize) { return new Vector3i(chunkFromBlock(x, chunkSize.x), chunkFromBlock(y, chunkSize.y), chunkFromBlock(z, chunkSize.z)); }
public static Vector3i chunkFromBlock(Vector3i blockPosition, Vector3i chunkSize) { return new Vector3i(chunkFromBlock(blockPosition.x, chunkSize.x), chunkFromBlock(blockPosition.y, chunkSize.y), chunkFromBlock(blockPosition.z, chunkSize.z)); }
private void unloadChunk(Vector3i p) { if (loadedChunks.ContainsKey(p)) { loadedChunks[p].save(); loadedChunks.Remove(p); } }
private bool outOfView(Vector3i p, int x, int y, int z, Game game) { return (p.x < x - game.renderDistance) || (p.y < y - game.renderDistance) || (p.z < z - game.renderDistance) || (p.x > x + game.renderDistance) || (p.y > y + game.renderDistance) || (p.z > z + game.renderDistance); }
private void loadChunk(Vector3i p, bool forceLoad = false) { if(!loadedChunks.ContainsKey(p) || forceLoad){ loadedChunks.Add(p, new Chunk(p, this, game)); } }
private void addToLoadedIfNeeded(Vector3i p) { if(!loadedChunks.ContainsKey(p)){ if(!chunksToLoad.Contains(p)){ chunksToLoad.Add(p); } } }