waiting for previous chunk loading before doing next
This commit is contained in:
+13
-7
@@ -83,29 +83,35 @@ impl Game {
|
||||
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);
|
||||
|
||||
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 {
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
self.prev_chunk_pos = Some(chunk_pos);
|
||||
|
||||
self.prev_chunk_pos = Some(prev_chunk_pos+dir);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -22,6 +22,7 @@ pub struct World {
|
||||
data: WorldData,
|
||||
loader: ChunkLoader,
|
||||
chunks: HashMap<Vector3<i32>, WorldChunk>,
|
||||
busy: u32
|
||||
}
|
||||
|
||||
impl World {
|
||||
@@ -31,18 +32,24 @@ impl World {
|
||||
data: WorldData { name: name.clone(), ..Default::default() },
|
||||
loader: ChunkLoader::new(name.clone()),
|
||||
chunks: HashMap::new(),
|
||||
busy: 0,
|
||||
};
|
||||
instance.load_metadata();
|
||||
instance.save_all();
|
||||
instance
|
||||
}
|
||||
|
||||
pub fn is_busy(&self) -> bool {
|
||||
self.busy > 0
|
||||
}
|
||||
|
||||
/*
|
||||
* Receives loaded chunks from ChunkLoader
|
||||
*/
|
||||
pub fn update(&mut self) {
|
||||
for (pos, chunk) in self.loader.receive() {
|
||||
self.chunks.insert(pos, chunk);
|
||||
self.busy -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +58,7 @@ impl World {
|
||||
*/
|
||||
pub fn load_chunk(&mut self, pos: Vector3<i32>) {
|
||||
self.loader.load(pos);
|
||||
self.busy += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user