public void SetBlock(int x, int y, int z, BlockData block) { int l, b; HeightToLayerAndBlock(y, out l, out b); if (m_layers.ContainsKey(l)) { var layer = m_layers[l]; layer.SetBlock(x, b, z, block); if (layer.IsEmpty()) { m_layers.Remove(l); } } else { if (block == BlockData.GetDefault()) { return; } var layer = new ChunkLayer(); layer.SetBlock(x, b, z, block); m_layers.Add(l, layer); } }
public BlockData GetBlock(int x, int y, int z) { int layer, block; HeightToLayerAndBlock(y, out layer, out block); if (m_layers.ContainsKey(layer)) { return(m_layers[layer].GetBlock(x, block, z)); } return(BlockData.GetDefault()); }
public void SetBlock(int x, int y, int z, BlockData bloc) { Debug.Assert(x >= 0 && x < Chunk.chunkSize && y >= 0 && y < Chunk.chunkSize && z >= 0 && z < Chunk.chunkSize); BlockData empty = BlockData.GetDefault(); int index = PosToIndex(x, y, z); if (m_blocks[index] != empty) { m_blockNb--; } if (bloc != empty) { m_blockNb++; } m_blocks[index] = bloc; m_updateTime = Time.time; }
public int GetBottomBlockHeight(int x, int z) { Debug.Assert(HaveLayer()); if (!HaveLayer()) { return(int.MaxValue); } int topLayerIndex = GetTopLayerIndex(); int bottomLayerIndex = GetBottomLayerIndex(); int layerIndex; int height = 0; bool found = false; for (layerIndex = bottomLayerIndex; layerIndex <= topLayerIndex; layerIndex++) { var layer = m_layers[layerIndex]; height = 0; for (height = 0; height < chunkSize; height++) { if (layer.GetBlock(x, height, z) != BlockData.GetDefault()) { found = true; break; } } if (found) { break; } } Debug.Assert(height >= 0); return(LayerToHeight(layerIndex, height)); }
public void GetLocalMatrix(int x, int y, int z, Matrix <BlockData> mat) { lock (dataLock) { //todo optimize layers to do the same than chunk (not get multiple time the same layer) int maxX = x + mat.width - 1; int maxZ = z + mat.depth - 1; int minChunkX; int minChunkZ; int maxChunkX; int maxChunkZ; PosToUnclampedChunkPos(x, z, out minChunkX, out minChunkZ); PosToUnclampedChunkPos(maxX, maxZ, out maxChunkX, out maxChunkZ); for (int i = minChunkX; i <= maxChunkX; i++) { for (int j = minChunkZ; j <= maxChunkZ; j++) { int currentMinX; int currentMinZ; int currentMaxX; int currentMaxZ; BlockPosInChunkToPos(0, 0, i, j, out currentMinX, out currentMinZ); BlockPosInChunkToPos(Chunk.chunkSize - 1, Chunk.chunkSize - 1, i, j, out currentMaxX, out currentMaxZ); int localMinX = 0; int localMinZ = 0; int localMaxX = Chunk.chunkSize - 1; int localMaxZ = Chunk.chunkSize - 1; int tileMinX = 0; int tileMinZ = 0; if (currentMinX < x) { localMinX = x - currentMinX; } else { tileMinX = currentMinX - x; } if (currentMinZ < z) { localMinZ = z - currentMinZ; } else { tileMinZ = currentMinZ - z; } if (localMaxX - localMinX + 1 > mat.width - tileMinX) { localMaxX = mat.width + localMinX - 1 - tileMinX; } if (localMaxZ - localMinZ + 1 > mat.depth - tileMinZ) { localMaxZ = mat.depth + localMinZ - 1 - tileMinZ; } var chunk = GetChunk(i, j); for (int m = 0; m < mat.height; m++) { int layerIndex, blockY; chunk.HeightToLayerAndBlock(y + m, out layerIndex, out blockY); var layer = chunk.GetLayer(layerIndex); for (int k = 0; k <= localMaxX - localMinX; k++) { for (int l = 0; l <= localMaxZ - localMinZ; l++) { if (layer == null) { mat.Set(k + tileMinX, m, l + tileMinZ, BlockData.GetDefault()); } else { mat.Set(k + tileMinX, m, l + tileMinZ, layer.GetBlock(localMinX + k, blockY, localMinZ + l)); } } } } } } } }