From 35ea1390973e3a40aaeb0bfc97b88851b103402d Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 13 Apr 2024 14:59:48 +0100 Subject: [PATCH] Initial chunk region get --- src/voxel/world/chunk.rs | 40 ++++++++++++++++++++++++++++++++++++++ src/voxel/world/manager.rs | 5 ++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/voxel/world/chunk.rs b/src/voxel/world/chunk.rs index 8c90700..b6a1017 100644 --- a/src/voxel/world/chunk.rs +++ b/src/voxel/world/chunk.rs @@ -52,6 +52,46 @@ impl Chunk { } } + pub fn get_region( + &mut self, + region_start: glam::UVec3, + region_dims: glam::UVec3, + ) -> Vec { + let mut voxels = vec![]; + + let start = region_start; + let end = region_start + region_dims; + let dims = self.settings.dimensions * self.settings.block_dimensions; + assert!(end.x <= dims.x && end.y <= dims.y && end.z <= dims.z); + + for z in (start.z)..(end.z) { + for y in (start.y)..(end.y) { + for x in (start.x)..(end.x) { + voxels.push(self.get_voxel(glam::uvec3(x, y, z))); + } + } + } + + voxels + } + + pub fn get_voxel(&mut self, pos: glam::UVec3) -> Voxel { + let dims = self.settings.dimensions * self.settings.block_dimensions; + debug_assert!(pos.x < dims.x && pos.y < dims.y && pos.z < dims.z); + + let block_pos = pos / self.settings.block_dimensions; + let block_idx = math::to_1d_index(block_pos, self.settings.dimensions); + let mut block = &self.blocks[block_idx]; + if block.is_empty() { + self.gen_block(block_pos, block_idx); + block = &self.blocks[block_idx] + } + + let local_pos = pos % self.settings.block_dimensions; + let local_idx = math::to_1d_index(local_pos, self.settings.block_dimensions); + block[local_idx] + } + pub fn get_block(&mut self, pos: glam::UVec3) -> Vec { let dims = self.settings.dimensions; assert!(pos.x < dims.x && pos.y < dims.y && pos.z < dims.z); diff --git a/src/voxel/world/manager.rs b/src/voxel/world/manager.rs index 3f2349d..f97cdf5 100644 --- a/src/voxel/world/manager.rs +++ b/src/voxel/world/manager.rs @@ -31,7 +31,10 @@ impl WorldManager { } let chunk = self.chunks.get_mut(&chunk_pos).unwrap(); - chunk.get_block(local_pos) + let block_dims = self.chunk_settings.block_dimensions; + chunk.get_region(local_pos * block_dims, block_dims) + + // chunk.get_block(local_pos) } fn gen_chunk(&mut self, pos: glam::IVec3) -> Chunk {