waiting for previous chunk loading before doing next

This commit is contained in:
Piotrek
2021-05-16 21:00:15 +02:00
parent eecedbf4f4
commit 0487f4acf3
2 changed files with 30 additions and 16 deletions
+22 -16
View File
@@ -82,30 +82,36 @@ 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));
let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
// 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);
// 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()};
println!("Load at {:?}", center);
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);
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.prev_chunk_pos = Some(chunk_pos);
}
// Receives chunks from workers
self.world.update();
// Send dirty chunks to renderer
// TODO: Unload chunks above certain radius
self.world.update();
for (pos, chunk) in self.world.all_chunks() {
if chunk.dirty {
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);