Update neighbors

This commit is contained in:
griffi-gh 2023-02-05 01:18:30 +01:00
parent 319cffbb4e
commit 34fa47acbe

View file

@ -1,5 +1,5 @@
use glam::IVec3; use glam::{IVec3, ivec3};
use kubi_shared::blocks::Block; use kubi_shared::{blocks::Block, chunk::CHUNK_SIZE};
use shipyard::{UniqueViewMut, Unique}; use shipyard::{UniqueViewMut, Unique};
use super::ChunkStorage; use super::ChunkStorage;
@ -27,14 +27,33 @@ pub fn apply_queued_blocks(
mut queue: UniqueViewMut<BlockUpdateQueue>, mut queue: UniqueViewMut<BlockUpdateQueue>,
mut world: UniqueViewMut<ChunkStorage> mut world: UniqueViewMut<ChunkStorage>
) { ) {
//maybe i need to check for desired/current state here before marking as dirty?
queue.queue.retain(|&event| { queue.queue.retain(|&event| {
if let Some(block) = world.get_block_mut(event.position) { if let Some(block) = world.get_block_mut(event.position) {
*block = event.value; *block = event.value;
//mark chunk as dirty //mark chunk as dirty
//maybe i need to check for desired/current state here? let (chunk_pos, block_pos) = ChunkStorage::to_chunk_coords(event.position);
let (chunk_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."); 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; 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 return false
} }
true true