From 816d2f6077e68d7bf141204c87b50c94bd91a746 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 5 Feb 2023 01:18:30 +0100 Subject: [PATCH] Update neighbors --- kubi/src/world/queue.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/kubi/src/world/queue.rs b/kubi/src/world/queue.rs index 51adede..bfcaa63 100644 --- a/kubi/src/world/queue.rs +++ b/kubi/src/world/queue.rs @@ -1,5 +1,5 @@ -use glam::IVec3; -use kubi_shared::blocks::Block; +use glam::{IVec3, ivec3}; +use kubi_shared::{blocks::Block, chunk::CHUNK_SIZE}; use shipyard::{UniqueViewMut, Unique}; use super::ChunkStorage; @@ -27,14 +27,33 @@ pub fn apply_queued_blocks( mut queue: UniqueViewMut, mut world: UniqueViewMut ) { + //maybe i need to check for desired/current state here before marking as dirty? queue.queue.retain(|&event| { if let Some(block) = world.get_block_mut(event.position) { *block = event.value; //mark chunk as dirty - //maybe i need to check for desired/current state here? - let (chunk_pos, _) = ChunkStorage::to_chunk_coords(event.position); + let (chunk_pos, block_pos) = ChunkStorage::to_chunk_coords(event.position); let chunk = world.chunks.get_mut(&chunk_pos).expect("This error should never happen, if it does then something is super fucked up and the whole project needs to be burnt down."); chunk.mesh_dirty = true; + //If block pos is close to the border, some neighbors may be dirty! + const DIRECTIONS: [IVec3; 6] = [ + ivec3(1, 0, 0), + ivec3(-1, 0, 0), + ivec3(0, 1, 0), + ivec3(0, -1, 0), + ivec3(0, 0, 1), + ivec3(0, 0, -1), + ]; + for direction in DIRECTIONS { + let outside_chunk = |x| !(0..CHUNK_SIZE as i32).contains(x); + let chunk_dirty = (block_pos + direction).to_array().iter().any(outside_chunk); + if chunk_dirty { + let dir_chunk_pos = chunk_pos + direction; + if let Some(dir_chunk) = world.chunks.get_mut(&dir_chunk_pos) { + dir_chunk.mesh_dirty = true; + } + } + } return false } true