From 1701aa5a616f3de55b0a426f8d9e679f2c98a0c6 Mon Sep 17 00:00:00 2001 From: tylermurphy534 Date: Mon, 3 Oct 2022 06:50:49 -0400 Subject: mouse picking --- src/chunk.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/chunk.cpp') diff --git a/src/chunk.cpp b/src/chunk.cpp index 55f8832..18e3c69 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -391,6 +391,29 @@ void Chunk::setBlock(int32_t x, int32_t y, int32_t z, uint8_t block) { cubes[index] = block; } +uint8_t Chunk::getGlobalBlock(int32_t x, int32_t y, int32_t z) { + if(y >= CHUNK_SIZE.y) return AIR; + if(y < 0) return INVALID; + int gridX = static_cast(floor(x / Chunk::CHUNK_SIZE.x)); + int gridZ = static_cast(floor(z / Chunk::CHUNK_SIZE.z)); + Chunk* chunk = getChunk(gridX, gridZ); + if(chunk == nullptr) return INVALID; + int localX = x - gridX * CHUNK_SIZE.x; + int localZ = z - gridZ * CHUNK_SIZE.z; + return chunk->getBlock(localX, y, localZ); +} + +void Chunk::setGlobalBlock(int32_t x, int32_t y, int32_t z, uint8_t block) { + if(y < 0 || y >= CHUNK_SIZE.y) return; + int gridX = static_cast(x % Chunk::CHUNK_SIZE.x); + int gridZ = static_cast(floor(z / Chunk::CHUNK_SIZE.z)); + Chunk* chunk = getChunk(gridX, gridZ); + if(chunk == nullptr) return; + int localX = x - gridX * CHUNK_SIZE.x; + int localZ = z - gridZ * CHUNK_SIZE.z; + chunk->setBlock(localX, y, localZ, block); +} + bool Chunk::isGenerated(int32_t gridX, int32_t gridZ) { Chunk* chunk = Chunk::getChunk(gridX, gridZ); if(chunk == nullptr) return false; -- cgit v1.2.3-freya