writing chunks instead of blocks

This commit is contained in:
Piotrek
2021-05-17 14:53:59 +02:00
parent 7359b6549c
commit 5266b32823
5 changed files with 55 additions and 38 deletions
+18 -17
View File
@@ -8,7 +8,7 @@ use winit::event::Event;
use player::Player;
use world::World;
const RENDER_DIST: i32 = 3;
pub const RENDER_DIST: i32 = 3;
pub enum GameState {
Paused,
@@ -61,18 +61,18 @@ impl Game {
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 { }
else { self.prev_chunk_pos = Some(prev_chunk_pos+dir); return; }
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);
}
// Shift world
println!("Shiftin by {:?}", dir);
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);
@@ -117,11 +117,12 @@ impl Game {
self.world.update();
// Send dirty chunks to renderer
// TODO: Unload chunks above certain radius
for (pos, chunk) in self.world.all_chunks() {
if chunk.dirty {
chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
chunk.dirty = false;
if chunk_pos == self.prev_chunk_pos.unwrap_or(chunk_pos) {
for (pos, chunk) in self.world.all_chunks() {
if chunk.dirty {
renderer.write_chunk(pos, chunk);
chunk.dirty = false;
}
}
}
}
+3 -8
View File
@@ -43,7 +43,7 @@ impl WorldChunk {
// Header
let (header_bytes, bytes) = bytes.split_at(128);
let file_ver = header_bytes[0];
let num_blocks = LittleEndian::read_u32(&header_bytes[1..5]) as usize;
let mut num_blocks = LittleEndian::read_u32(&header_bytes[1..5]) as usize;
assert_eq!(file_ver, 1);
// Nodes
@@ -57,7 +57,8 @@ impl WorldChunk {
// Blocks
let mut block_bytes = vec![0_u8; num_blocks*block::SIZE_QB];
if lz4::decompress(&block_bytes_compressed, &mut block_bytes[0..num_blocks*block::SIZE_QB]).is_err() {
println!("Decompression failed");
num_blocks = 0;
}
for i in 0..num_blocks {
@@ -124,10 +125,4 @@ impl WorldChunk {
// Return block reference
&mut self.blocks[(value-1) as usize]
}
pub fn write_to(&self, renderer: &mut dyn RendererView, offset: Vector3<i32>) {
for (blpos, block) in self.all_blocks() {
renderer.write(&(offset + blpos), block);
}
}
}