public static Chunk NewFilledChunk(World world, Position chunkPos, int blockID) { BlockDef definition = BlockDefManager.GetBlockDef(blockID); Chunk chunk = new Chunk(world, chunkPos); Position globalChunkPos = chunkPos.MultAll(CHUNK_SIZE); int x, y, z; for (x = 0; x < CHUNK_SIZE; x++) { for (y = 0; y < CHUNK_SIZE; y++) { for (z = 0; z < CHUNK_SIZE; z++) { chunk.Blocks[x, y, z] = new Block(definition, globalChunkPos.Add(x, y, z), chunk); } } } if (definition.BlockType == BlockDef.TYPE_FLUID) { for (x = 0; x < CHUNK_SIZE; x++) { for (y = 0; y < CHUNK_SIZE; y++) { for (z = 0; z < CHUNK_SIZE; z++) { chunk.fluids.Add(new Fluid(chunk.Blocks[x, y, z])); } } } } return(chunk); }
/// <summary> /// Remplace le bloc présent aux coordonées indiquées. /// </summary> public bool SetLocalBlock(int id, Position localBlockPos) { if (!IsValid(localBlockPos)) { return(false); } BlockDef definition = BlockDefManager.GetBlockDef(id); if (definition == null) { return(false); } Block block = Blocks[localBlockPos.X, localBlockPos.Y, localBlockPos.Z]; if (block.Definition.BlockType == BlockDef.TYPE_FLUID) { RemoveFluid(block); } block.Definition = definition; if (definition.BlockType == BlockDef.TYPE_FLUID) { fluids.Add(new Fluid(block)); } altered = true; RecalculateMesh(); return(true); }
private static void SetBlocks3DNoise(Position globalChunkPos, Chunk chunk) { for (int x = 0; x < CHUNK_SIZE; x++) { for (int y = 0; y < CHUNK_SIZE; y++) { for (int z = 0; z < CHUNK_SIZE; z++) { Position blockPos = globalChunkPos.Add(x, y, z); float noiseValue = Noise.CalcPixel3D(blockPos.X, blockPos.Y, blockPos.Z, 0.015f); chunk.Blocks[x, y, z] = new Block(BlockDefManager.GetBlockDef(noiseValue > 80 ? 2 : 0), blockPos, chunk); } } } }
private static void SetBlocks2DNoise(Position globalChunkPos, Chunk chunk, int baseGlobalPosition) { int raise = globalChunkPos.Y - baseGlobalPosition + 90; float heightSmoother = 0.5f; for (int x = 0; x < CHUNK_SIZE; x++) { for (int z = 0; z < CHUNK_SIZE; z++) { Position blockPos = globalChunkPos.Add(x, 0, z); float noiseValue = Noise.CalcPixel2D(Mathf.Abs(blockPos.X), Mathf.Abs(blockPos.Z), 0.002f) * heightSmoother; for (int y = 0; y < CHUNK_SIZE; y++) { chunk.Blocks[x, y, z] = new Block(BlockDefManager.GetBlockDef(noiseValue > (raise + y) ? 1 : 0), blockPos.Add(0, y, 0), chunk); } } } }
/// <summary> /// Remplace les blocs présents entre les deux coordonées locales indiquées. /// </summary> public bool SetLocalBlockBatch(int id, Position first, Position second) { if (!IsValid(first, second)) { return(false); } BlockDef newDef = BlockDefManager.GetBlockDef(id); if (newDef == null) { return(false); } Position.Smooth(first, second); for (int x = first.X; x <= second.X; x++) { for (int y = first.Y; y <= second.Y; y++) { for (int z = first.Z; z <= second.Z; z++) { Block block = Blocks[x, y, z]; if (block.Definition.BlockType == BlockDef.TYPE_FLUID) { RemoveFluid(block); } block.Definition = newDef; if (newDef.BlockType == BlockDef.TYPE_FLUID) { fluids.Add(new Fluid(block)); } } } } altered = true; RecalculateMesh(); return(true); }
private static Chunk NewTestChunk(World world, Position position) { Chunk chunk = new Chunk(world, position); Position globalChunkPos = position.MultAll(CHUNK_SIZE); int x, y, z; for (x = 0; x < CHUNK_SIZE; x++) { for (y = 0; y < CHUNK_SIZE; y++) { for (z = 0; z < CHUNK_SIZE; z++) { float value = Random.value; chunk.Blocks[x, y, z] = new Block(BlockDefManager.GetBlockDef(value > 0.3f ? value < 0.7f ? 1 : 2 : 102), globalChunkPos.Add(x, y, z), chunk); } } } return(chunk); }
public static Chunk LoadChunk(World world, Position chunkPos) { if (!Directory.Exists(world.SaveDir)) { return(null); } string filePath = world.SaveDir + "[" + chunkPos.X + "." + chunkPos.Y + "." + chunkPos.Z + "].chunk"; if (!File.Exists(filePath)) { return(CreateChunk(world, chunkPos)); } Chunk newChunk = new Chunk(world, chunkPos); Position globalChunkPos = chunkPos.MultAll(CHUNK_SIZE); using (var reader = new BinaryReader(File.Open(filePath, FileMode.Open))) { int x = 0, y = 0, z = 0, len; bool filled = false; while (!filled) { len = reader.ReadInt32(); BlockDef definition = BlockDefManager.GetBlockDef(reader.ReadInt32()); while (len-- > 0) { newChunk.Blocks[x, y, z] = new Block(definition, globalChunkPos.Add(x, y, z), newChunk); if (definition.BlockType == BlockDef.TYPE_FLUID) { newChunk.fluids.Add(new Fluid(newChunk.Blocks[x, y, z])); } if (z >= 15) { z = 0; if (y >= 15) { y = 0; if (x >= 15) { filled = true; break; } ++x; } else { ++y; } } else { ++z; } } } } newChunk.altered = false; return(newChunk); }