waiting for previous chunk loading before doing next
This commit is contained in:
+22
-16
@@ -83,29 +83,35 @@ impl Game {
|
|||||||
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
|
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));
|
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 {
|
||||||
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
|
// Shift world
|
||||||
|
renderer.shift(dir * world::chunk::SIZE as i32);
|
||||||
// Shift world
|
// Load chunks at the edge
|
||||||
renderer.shift(dir * world::chunk::SIZE as i32);
|
let center = chunk_pos.clone() + RENDER_DIST*dir;
|
||||||
|
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
|
||||||
// Load chunks at the edge
|
for a in -RENDER_DIST..RENDER_DIST+1 {
|
||||||
let center = chunk_pos.clone() + RENDER_DIST*dir;
|
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
|
||||||
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
|
self.world.load_chunk(pos);
|
||||||
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(prev_chunk_pos+dir);
|
||||||
}
|
}
|
||||||
self.prev_chunk_pos = Some(chunk_pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Receives chunks from workers
|
||||||
|
self.world.update();
|
||||||
|
|
||||||
// Send dirty chunks to renderer
|
// Send dirty chunks to renderer
|
||||||
// TODO: Unload chunks above certain radius
|
// TODO: Unload chunks above certain radius
|
||||||
self.world.update();
|
|
||||||
for (pos, chunk) in self.world.all_chunks() {
|
for (pos, chunk) in self.world.all_chunks() {
|
||||||
if chunk.dirty {
|
if chunk.dirty {
|
||||||
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
|
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ pub struct World {
|
|||||||
data: WorldData,
|
data: WorldData,
|
||||||
loader: ChunkLoader,
|
loader: ChunkLoader,
|
||||||
chunks: HashMap<Vector3<i32>, WorldChunk>,
|
chunks: HashMap<Vector3<i32>, WorldChunk>,
|
||||||
|
busy: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
impl World {
|
impl World {
|
||||||
@@ -31,18 +32,24 @@ impl World {
|
|||||||
data: WorldData { name: name.clone(), ..Default::default() },
|
data: WorldData { name: name.clone(), ..Default::default() },
|
||||||
loader: ChunkLoader::new(name.clone()),
|
loader: ChunkLoader::new(name.clone()),
|
||||||
chunks: HashMap::new(),
|
chunks: HashMap::new(),
|
||||||
|
busy: 0,
|
||||||
};
|
};
|
||||||
instance.load_metadata();
|
instance.load_metadata();
|
||||||
instance.save_all();
|
instance.save_all();
|
||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_busy(&self) -> bool {
|
||||||
|
self.busy > 0
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Receives loaded chunks from ChunkLoader
|
* Receives loaded chunks from ChunkLoader
|
||||||
*/
|
*/
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
for (pos, chunk) in self.loader.receive() {
|
for (pos, chunk) in self.loader.receive() {
|
||||||
self.chunks.insert(pos, chunk);
|
self.chunks.insert(pos, chunk);
|
||||||
|
self.busy -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +58,7 @@ impl World {
|
|||||||
*/
|
*/
|
||||||
pub fn load_chunk(&mut self, pos: Vector3<i32>) {
|
pub fn load_chunk(&mut self, pos: Vector3<i32>) {
|
||||||
self.loader.load(pos);
|
self.loader.load(pos);
|
||||||
|
self.busy += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user