static void UpdateWorldData(World world) { Matrix <BlockData> mat = new Matrix <BlockData>(Chunk.chunkSize + 2, Chunk.chunkSize + 2, Chunk.chunkSize + 2); MatrixView <BlockData> view = new MatrixView <BlockData>(mat, 0, 0, 0); for (int i = 0; i < world.chunkNb; i++) { for (int j = 0; j < world.chunkNb; j++) { var chunk = world.GetChunk(i, j); var layers = chunk.GetLayers(); foreach (var l in layers) { world.GetLocalMatrix(i * Chunk.chunkSize - 1, l * Chunk.chunkSize - 1, j * Chunk.chunkSize - 1, mat); for (int x = 0; x < Chunk.chunkSize; x++) { for (int y = 0; y < Chunk.chunkSize; y++) { for (int z = 0; z < Chunk.chunkSize; z++) { view.SetPos(x + 1, y + 1, z + 1); view.Set(0, 0, 0, BlockTypeList.instance.Get(view.GetCenter().id).UpdateBlock(view)); } } } for (int y = 0; y < Chunk.chunkSize; y++) { int height = chunk.LayerToHeight(l, y); for (int x = 0; x < Chunk.chunkSize; x++) { for (int z = 0; z < Chunk.chunkSize; z++) { chunk.SetBlock(x, height, z, mat.Get(x + 1, y + 1, z + 1)); } } } } } } }
public void SetBlock(int x, int y, int z, BlockData block, bool updateData = true) { if (updateData) { //create a local 5*5*5 matrix var matrix = GetLocalMatrix(x - 2, y - 2, z - 2, 5, 5, 5); var view = new MatrixView <BlockData>(matrix, 2, 2, 2); view.Set(0, 0, 0, block); Vector3Int[] updatePositions = new Vector3Int[] { //center new Vector3Int(2, 2, 2), //cross new Vector3Int(2, 1, 2), new Vector3Int(1, 2, 2), new Vector3Int(2, 2, 1), new Vector3Int(3, 2, 2), new Vector3Int(2, 2, 3), new Vector3Int(2, 3, 2), //borders new Vector3Int(1, 1, 2), //down new Vector3Int(2, 1, 1), new Vector3Int(3, 1, 2), new Vector3Int(2, 1, 3), new Vector3Int(1, 2, 1), //midle new Vector3Int(3, 2, 1), new Vector3Int(1, 2, 3), new Vector3Int(3, 2, 3), new Vector3Int(1, 3, 2), //up new Vector3Int(2, 3, 1), new Vector3Int(3, 3, 2), new Vector3Int(2, 3, 3), //corners new Vector3Int(1, 1, 1), //down new Vector3Int(1, 1, 3), new Vector3Int(3, 1, 1), new Vector3Int(3, 1, 3), new Vector3Int(1, 3, 1), //up new Vector3Int(1, 3, 3), new Vector3Int(3, 3, 1), new Vector3Int(3, 3, 3), }; foreach (var pos in updatePositions) { view.SetPos(pos.x, pos.y, pos.z); view.Set(0, 0, 0, BlockTypeList.instance.Get(view.GetCenter().id).UpdateBlock(view)); } //only update the 3*3* center of the matrix SetBlocks(x - 1, y - 1, z - 1, matrix, 1, 1, 1, 3, 3, 3, false); } else { int chunkX; int chunkZ; int blockX; int blockZ; PosToBlockPosAndChunkPos(x, z, out blockX, out blockZ, out chunkX, out chunkZ); var chunk = GetChunk(chunkX, chunkZ); Debug.Assert(chunk != null); lock (dataLock) { chunk.SetBlock(blockX, y, blockZ, block); } } }