use bevy::prelude::*;
use crate::chunk::{CHUNK_SIZE, Chunk};
use crate::voxel::VoxelType;
use super::{ChunkManager, ChunkModified};
const FACE_OFFSETS: [IVec3; 6] = [
IVec3::Y,
IVec3::NEG_Y,
IVec3::X,
IVec3::NEG_X,
IVec3::Z,
IVec3::NEG_Z,
];
fn is_drawn<V: VoxelType>(voxel: V) -> bool {
voxel.color()[3] > 0.0
}
pub fn record_voxel_edit<V: VoxelType>(
chunk_coord: IVec3,
local: (usize, usize, usize),
old: V,
new: V,
manager: &ChunkManager,
chunks: &mut Query<&mut Chunk<V>>,
modified: &mut MessageWriter<ChunkModified>,
) {
if old == new {
return;
}
let size = CHUNK_SIZE as i32;
if let Some(mut chunk) = manager
.get_entity(chunk_coord)
.and_then(|entity| chunks.get_mut(entity).ok())
&& chunk.exposed_faces(local.0, local.1, local.2) != 0
{
chunk.set_exposed_faces(local.0, local.1, local.2, 0);
}
let removed = is_drawn(old) && !is_drawn(new);
let filled = is_drawn(new);
if !removed && !filled {
return;
}
let world_voxel =
chunk_coord * size + IVec3::new(local.0 as i32, local.1 as i32, local.2 as i32);
for (face, offset) in FACE_OFFSETS.iter().enumerate() {
let neighbor = world_voxel + *offset;
let neighbor_chunk = neighbor.div_euclid(IVec3::splat(size));
let (x, y, z) = (
neighbor.x.rem_euclid(size) as usize,
neighbor.y.rem_euclid(size) as usize,
neighbor.z.rem_euclid(size) as usize,
);
let Some(mut chunk) = manager
.get_entity(neighbor_chunk)
.and_then(|entity| chunks.get_mut(entity).ok())
else {
continue;
};
let bit = 1u8 << (face ^ 1);
let before = chunk.exposed_faces(x, y, z);
let after = if removed && is_drawn(chunk.get(x, y, z)) {
before | bit
} else {
before & !bit
};
if after != before {
chunk.set_exposed_faces(x, y, z, after);
if neighbor_chunk != chunk_coord {
modified.write(ChunkModified(neighbor_chunk));
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::world_with_chunks;
use crate::voxel::DefaultVoxel;
use crate::world::{get_voxel_at, set_voxel_at};
use bevy::ecs::message::Messages;
use bevy::ecs::system::SystemState;
const ROCK: DefaultVoxel = DefaultVoxel::Rock;
const AIR: DefaultVoxel = DefaultVoxel::Air;
const UP: u8 = 1 << 0;
const DOWN: u8 = 1 << 1;
const EAST: u8 = 1 << 2;
const WEST: u8 = 1 << 3;
fn floor_world() -> World {
let floor = || {
let mut chunk = Chunk::<DefaultVoxel>::new();
for x in 0..CHUNK_SIZE {
for y in 0..5 {
for z in 0..CHUNK_SIZE {
chunk.set(x, y, z, ROCK);
}
}
}
chunk.mark_clean();
chunk
};
let mut world =
world_with_chunks([(IVec3::new(-1, 0, 0), floor()), (IVec3::ZERO, floor())]);
world.init_resource::<Messages<ChunkModified>>();
world
}
fn set(world: &mut World, voxel_pos: IVec3, voxel: DefaultVoxel) {
let mut state: SystemState<(
Res<ChunkManager>,
Query<&mut Chunk<DefaultVoxel>>,
MessageWriter<ChunkModified>,
)> = SystemState::new(world);
let (manager, mut chunks, mut writer) = state.get_mut(world);
let center = voxel_pos.as_vec3() + Vec3::splat(0.5);
assert!(set_voxel_at(
center,
voxel,
&manager,
&mut chunks,
&mut writer
));
state.apply(world);
}
fn exposed(world: &mut World, voxel_pos: IVec3) -> u8 {
let size = CHUNK_SIZE as i32;
let coord = voxel_pos.div_euclid(IVec3::splat(size));
let local = voxel_pos.rem_euclid(IVec3::splat(size)).as_usizevec3();
let entity = world.resource::<ChunkManager>().get_entity(coord).unwrap();
world
.get::<Chunk<DefaultVoxel>>(entity)
.unwrap()
.exposed_faces(local.x, local.y, local.z)
}
fn chunk_at(world: &World, coord: IVec3) -> &Chunk<DefaultVoxel> {
let entity = world.resource::<ChunkManager>().get_entity(coord).unwrap();
world.get::<Chunk<DefaultVoxel>>(entity).unwrap()
}
fn modified(world: &World) -> Vec<IVec3> {
world
.resource::<Messages<ChunkModified>>()
.iter_current_update_messages()
.map(|message| message.0)
.collect()
}
#[test]
fn generated_terrain_has_no_exposed_faces() {
let world = floor_world();
assert!(!chunk_at(&world, IVec3::ZERO).has_exposed_faces());
assert!(!chunk_at(&world, IVec3::new(-1, 0, 0)).has_exposed_faces());
}
#[test]
fn removing_a_voxel_exposes_the_faces_that_looked_at_it() {
let mut world = floor_world();
let dug = IVec3::new(5, 4, 5);
set(&mut world, dug, AIR);
assert_eq!(exposed(&mut world, dug + IVec3::NEG_Y), UP);
assert_eq!(exposed(&mut world, dug + IVec3::X), WEST);
assert_eq!(exposed(&mut world, dug + IVec3::NEG_X), EAST);
assert_eq!(exposed(&mut world, dug + IVec3::Y), 0);
assert_eq!(exposed(&mut world, IVec3::new(9, 4, 9)), 0);
assert!(chunk_at(&world, IVec3::ZERO).is_dirty());
}
#[test]
fn digging_deeper_keeps_earlier_faces_and_adds_new_ones() {
let mut world = floor_world();
set(&mut world, IVec3::new(5, 4, 5), AIR);
set(&mut world, IVec3::new(5, 3, 5), AIR);
assert_eq!(exposed(&mut world, IVec3::new(6, 4, 5)), WEST);
assert_eq!(exposed(&mut world, IVec3::new(6, 3, 5)), WEST);
assert_eq!(exposed(&mut world, IVec3::new(5, 2, 5)), UP);
}
#[test]
fn removed_voxel_loses_its_own_record() {
let mut world = floor_world();
set(&mut world, IVec3::new(5, 4, 5), AIR);
assert_eq!(exposed(&mut world, IVec3::new(5, 3, 5)), UP);
set(&mut world, IVec3::new(5, 3, 5), AIR);
assert_eq!(exposed(&mut world, IVec3::new(5, 3, 5)), 0);
}
#[test]
fn filling_the_hole_covers_the_faces_again() {
let mut world = floor_world();
let dug = IVec3::new(5, 4, 5);
set(&mut world, dug, AIR);
set(&mut world, dug, DefaultVoxel::IronOre);
assert_eq!(exposed(&mut world, dug + IVec3::NEG_Y), 0);
assert_eq!(exposed(&mut world, dug + IVec3::X), 0);
assert_eq!(exposed(&mut world, dug), 0);
assert!(!chunk_at(&world, IVec3::ZERO).has_exposed_faces());
}
#[test]
fn placing_a_voxel_on_the_surface_records_nothing() {
let mut world = floor_world();
set(&mut world, IVec3::new(5, 5, 5), ROCK);
assert!(!chunk_at(&world, IVec3::ZERO).has_exposed_faces());
}
#[test]
fn replacing_a_voxel_with_another_records_nothing() {
let mut world = floor_world();
set(&mut world, IVec3::new(5, 4, 5), DefaultVoxel::IronOre);
assert!(!chunk_at(&world, IVec3::ZERO).has_exposed_faces());
}
#[test]
fn removal_at_a_chunk_border_marks_and_remeshes_the_neighbor_chunk() {
let mut world = floor_world();
let left = IVec3::new(-1, 0, 0);
set(&mut world, IVec3::new(0, 4, 5), AIR);
assert_eq!(exposed(&mut world, IVec3::new(-1, 4, 5)), EAST);
assert!(chunk_at(&world, left).is_dirty());
assert!(modified(&world).contains(&left));
}
#[test]
fn unloaded_neighbor_chunk_is_skipped() {
let mut world = floor_world();
set(&mut world, IVec3::new(15, 4, 5), AIR);
assert_eq!(exposed(&mut world, IVec3::new(14, 4, 5)), EAST);
assert_eq!(modified(&world), vec![IVec3::ZERO]);
}
#[test]
fn floor_under_a_removed_voxel_at_the_bottom_of_the_world_is_fine() {
let mut world = floor_world();
set(&mut world, IVec3::new(5, 0, 5), AIR);
assert_eq!(exposed(&mut world, IVec3::new(5, 1, 5)), DOWN);
let mut state: SystemState<(Res<ChunkManager>, Query<&Chunk<DefaultVoxel>>)> =
SystemState::new(&mut world);
let (manager, chunks) = state.get(&world);
assert_eq!(
get_voxel_at(Vec3::new(5.5, 0.5, 5.5), &manager, &chunks),
Some(AIR)
);
}
}