public void SetVoxel(int x, int y, int z, bool active, byte destruct, VoxelType type, Color top, Color side) { if (x < 0 || y < 0 || z < 0 || x >= X_SIZE || y >= Y_SIZE || z >= Z_SIZE) { return; } Chunk c = GetChunkAtWorldPosition(x, y, z); c.SetVoxel(x - ((x / Chunk.X_SIZE) * Chunk.X_SIZE), y - ((y / Chunk.Y_SIZE) * Chunk.Y_SIZE), z - ((z / Chunk.Z_SIZE) * Chunk.Z_SIZE), active, destruct, type, top, side); c.Updated = true; }
public static void LoadWorld(string fn, ref VoxelWorld gameWorld) { byte[] buffer; using (FileStream gstr = new FileStream(fn, FileMode.Open)) { byte[] lb = new byte[4]; gstr.Position = gstr.Length - 4; gstr.Read(lb, 0, 4); int msgLength = BitConverter.ToInt32(lb, 0); buffer = new byte[msgLength]; gstr.Position = 0; using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress)) { str.Read(buffer, 0, msgLength); } } int pos = 0; int xs = buffer[0]; int ys = buffer[1]; int zs = buffer[2]; gameWorld = new VoxelWorld(xs, ys, zs, false); pos = 3; for (int z = 0; z < gameWorld.Z_CHUNKS; z++) { for (int y = 0; y < gameWorld.Y_CHUNKS; y++) { for (int x = 0; x < gameWorld.X_CHUNKS; x++) { Chunk c = gameWorld.Chunks[x, y, z]; while (pos < buffer.Length) { if (Convert.ToChar(buffer[pos]) != 'c') { int vx = buffer[pos]; int vy = buffer[pos + 1]; int vz = buffer[pos + 2]; VoxelType type = (VoxelType)buffer[pos + 3]; byte destruct = buffer[pos + 4]; Color top = new Color(buffer[pos + 5], buffer[pos + 6], buffer[pos + 7]); Color side = new Color(buffer[pos + 8], buffer[pos + 9], buffer[pos + 10]); c.SetVoxel(vx, vy, vz, true, destruct, type, top, side); pos += 11; } else { pos++; break; } } } } } gameWorld.UpdateWorldMeshes(); GC.Collect(); }