loading chunks in one axis at a time
This commit is contained in:
+29
-23
@@ -51,6 +51,34 @@ impl Game {
|
||||
instance
|
||||
}
|
||||
|
||||
fn load_chunks(&mut self, renderer: &mut impl RendererView, chunk_pos: Vector3<i32>) {
|
||||
// Wait for previous chunks to be loaded before loading new ones
|
||||
if !self.world.is_busy() {
|
||||
let prev_chunk_pos = self.prev_chunk_pos.unwrap_or(chunk_pos);
|
||||
let mut dir = chunk_pos - prev_chunk_pos;
|
||||
|
||||
// Shift max by 1, in one axis
|
||||
if dir.x != 0 { dir = Vector3{x: dir.x.clamp(-1, 1), y:0, z:0 } }
|
||||
else if dir.y != 0 { dir = Vector3{x:0, y:dir.y.clamp(-1, 1), z:0 } }
|
||||
else if dir.z != 0 { dir = Vector3{x:0, y:0, z:dir.z.clamp(-1, 1) } }
|
||||
else { }
|
||||
|
||||
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
|
||||
// Shift world
|
||||
renderer.shift(dir * world::chunk::SIZE as i32);
|
||||
// Load chunks at the edge
|
||||
let center = chunk_pos.clone() + RENDER_DIST*dir;
|
||||
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
|
||||
for a in -RENDER_DIST..RENDER_DIST+1 {
|
||||
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
|
||||
self.world.load_chunk(pos);
|
||||
}
|
||||
}
|
||||
|
||||
self.prev_chunk_pos = Some(prev_chunk_pos+dir);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, delta: f32, renderer: &mut impl RendererView) {
|
||||
// Update camera
|
||||
let camera = renderer.get_camera_transform();
|
||||
@@ -82,29 +110,7 @@ impl Game {
|
||||
// Chunk position
|
||||
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
|
||||
renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
|
||||
|
||||
// Wait for previous chunks to be loaded before loading new ones
|
||||
if !self.world.is_busy() {
|
||||
let prev_chunk_pos = self.prev_chunk_pos.unwrap_or(chunk_pos);
|
||||
let mut dir = chunk_pos - prev_chunk_pos;
|
||||
dir.x = dir.x.clamp(-1, 1);
|
||||
dir.y = dir.y.clamp(-1, 1);
|
||||
dir.z = dir.z.clamp(-1, 1);
|
||||
|
||||
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
|
||||
// Shift world
|
||||
renderer.shift(dir * world::chunk::SIZE as i32);
|
||||
// Load chunks at the edge
|
||||
let center = chunk_pos.clone() + RENDER_DIST*dir;
|
||||
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
|
||||
for a in -RENDER_DIST..RENDER_DIST+1 {
|
||||
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
|
||||
self.world.load_chunk(pos);
|
||||
}
|
||||
}
|
||||
|
||||
self.prev_chunk_pos = Some(prev_chunk_pos+dir);
|
||||
}
|
||||
self.load_chunks(renderer, chunk_pos);
|
||||
}
|
||||
|
||||
// Receives chunks from workers
|
||||
|
||||
Reference in New Issue
Block a user