WIP: World rewrite #2

Draft
Jayrude wants to merge 5 commits from world-rewrite into main
2 changed files with 44 additions and 1 deletions
Showing only changes of commit 35ea139097 - Show all commits

View File

@ -52,6 +52,46 @@ impl Chunk {
} }
} }
pub fn get_region(
&mut self,
region_start: glam::UVec3,
region_dims: glam::UVec3,
) -> Vec<Voxel> {
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<Voxel> { pub fn get_block(&mut self, pos: glam::UVec3) -> Vec<Voxel> {
let dims = self.settings.dimensions; let dims = self.settings.dimensions;
assert!(pos.x < dims.x && pos.y < dims.y && pos.z < dims.z); assert!(pos.x < dims.x && pos.y < dims.y && pos.z < dims.z);

View File

@ -31,7 +31,10 @@ impl WorldManager {
} }
let chunk = self.chunks.get_mut(&chunk_pos).unwrap(); 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 { fn gen_chunk(&mut self, pos: glam::IVec3) -> Chunk {