maybe fixed the out of bounds chunk bug?

This commit is contained in:
Piotrek
2021-05-17 21:47:04 +02:00
parent 65bacd745e
commit 688405a579
2 changed files with 16 additions and 13 deletions
+10 -8
View File
@@ -53,15 +53,16 @@ impl Game {
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);
//if !self.world.is_busy() {
if self.prev_chunk_pos == None { self.prev_chunk_pos = Some(chunk_pos); }
let prev_chunk_pos = self.prev_chunk_pos.unwrap();
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 { self.prev_chunk_pos = Some(prev_chunk_pos+dir); return; }
else { return; }
// Shift world
println!("Shiftin by {:?}", dir);
@@ -76,7 +77,7 @@ impl Game {
}
self.prev_chunk_pos = Some(prev_chunk_pos+dir);
}
//}
}
pub fn update(&mut self, delta: f32, renderer: &mut impl RendererView) {
@@ -87,10 +88,11 @@ impl Game {
// Get positions
let cam_pos = camera.get_position();
let cam_pos = Vector3{x: cam_pos.x as i32, y: cam_pos.y as i32, z: cam_pos.z as i32};
let mut chunk_pos = cam_pos / world::chunk::SIZE as i32;
if cam_pos.x < 0 { chunk_pos.x -= 1; }
if cam_pos.y < 0 { chunk_pos.y -= 1; }
if cam_pos.z < 0 { chunk_pos.z -= 1; }
let chunk_pos = cam_pos / world::chunk::SIZE as i32;
// why was this there? lol
//if cam_pos.x < 0 { chunk_pos.x -= 1; }
//if cam_pos.y < 0 { chunk_pos.y -= 1; }
//if cam_pos.z < 0 { chunk_pos.z -= 1; }
// Update pointing direction
let yaw = self.player.camera_controller.get_yaw();
+6 -5
View File
@@ -5,7 +5,7 @@ use crate::{game, game::world::{chunk, chunk::WorldChunk, block::WorldBlock}};
use cgmath::Vector3;
pub trait RendererView {
fn write_chunk(&mut self, chunk_pos: &Vector3<i32>, chunk: &WorldChunk);
fn write_chunk(&mut self, chunk_pos: &Vector3<i32>, chunk: &WorldChunk) -> bool;
fn shift(&mut self, offset: Vector3<i32>);
fn get_camera_transform(&mut self) -> &mut dyn CameraTransform;
fn get_ui(&mut self) -> &mut UserInterface;
@@ -15,20 +15,21 @@ pub trait RendererView {
impl RendererView for Renderer {
fn write_chunk(&mut self, chunk_pos: &Vector3<i32>, chunk: &WorldChunk) {
fn write_chunk(&mut self, chunk_pos: &Vector3<i32>, chunk: &WorldChunk) -> bool {
// Calculate chunk position in renderer space
let chunk_off = self.world_offset / chunk::SIZE as i32;
let pos = chunk_pos + chunk_off;
// Make sure we are withing current world bounds
if pos.x.abs() > game::RENDER_DIST { println!("out of bounds! x: {}", pos.x); return; }
if pos.y.abs() > game::RENDER_DIST { println!("out of bounds! y: {}", pos.y); return; }
if pos.z.abs() > game::RENDER_DIST { println!("out of bounds! z: {}", pos.z); return; }
if pos.x.abs() > game::RENDER_DIST { println!("out of bounds! x: {} off: {:?} pos: {:?}", pos.x, chunk_off, chunk_pos); return false; }
if pos.y.abs() > game::RENDER_DIST { println!("out of bounds! y: {} off: {:?} pos: {:?}", pos.y, chunk_off, chunk_pos); return false; }
if pos.z.abs() > game::RENDER_DIST { println!("out of bounds! z: {} off: {:?} pos: {:?}", pos.z, chunk_off, chunk_pos); return false; }
// Write chunk to renderer
println!("write chunk {:?}", pos);
let pos = pos * chunk::SIZE as i32;
for (block_pos, block) in chunk.all_blocks() {
self.buffers.content.write(&self.queue, &(block_pos + pos), block);
}
true
}
fn free_chunk(&mut self, chunk_pos: Vector3<i32>) {