use bevy::prelude::Component;
use super::{CHUNK_SIZE, CHUNK_VOLUME};
use crate::voxel::VoxelType;
#[derive(Component, Clone, Debug)]
pub struct Chunk<V: VoxelType> {
voxels: Box<[V; CHUNK_VOLUME]>,
pub states: Box<[u8; CHUNK_VOLUME]>,
exposed: Option<Box<[u8; CHUNK_VOLUME]>>,
dirty: bool,
}
pub fn face_to_state(normal: bevy::math::IVec3) -> u8 {
match (normal.x, normal.y, normal.z) {
(1, 0, 0) => 0,
(-1, 0, 0) => 1,
(0, 1, 0) => 2,
(0, -1, 0) => 3,
(0, 0, 1) => 4,
(0, 0, -1) => 5,
_ => 6,
}
}
impl<V: VoxelType> Chunk<V> {
pub fn new() -> Self {
Self {
voxels: Box::new([V::air(); CHUNK_VOLUME]),
states: Box::new([0u8; CHUNK_VOLUME]),
exposed: None,
dirty: false,
}
}
#[inline]
pub fn is_dirty(&self) -> bool {
self.dirty
}
#[inline]
pub fn mark_clean(&mut self) {
self.dirty = false;
}
#[inline]
pub fn mark_dirty(&mut self) {
self.dirty = true;
}
#[inline]
pub fn get_index(x: usize, y: usize, z: usize) -> usize {
debug_assert!(x < CHUNK_SIZE, "x={x} out of range");
debug_assert!(y < CHUNK_SIZE, "y={y} out of range");
debug_assert!(z < CHUNK_SIZE, "z={z} out of range");
x * CHUNK_SIZE * CHUNK_SIZE + y * CHUNK_SIZE + z
}
#[inline]
pub fn get(&self, x: usize, y: usize, z: usize) -> V {
self.voxels[Self::get_index(x, y, z)]
}
#[inline]
pub fn set(&mut self, x: usize, y: usize, z: usize, voxel: V) {
let slot = &mut self.voxels[Self::get_index(x, y, z)];
if *slot != voxel {
*slot = voxel;
self.dirty = true;
}
}
#[inline]
pub fn get_state(&self, x: usize, y: usize, z: usize) -> u8 {
self.states[Self::get_index(x, y, z)]
}
#[inline]
pub fn set_state(&mut self, x: usize, y: usize, z: usize, state: u8) {
let slot = &mut self.states[Self::get_index(x, y, z)];
if *slot != state {
*slot = state;
self.dirty = true;
}
}
#[inline]
pub fn exposed_faces(&self, x: usize, y: usize, z: usize) -> u8 {
match &self.exposed {
Some(exposed) => exposed[Self::get_index(x, y, z)],
None => 0,
}
}
pub fn set_exposed_faces(&mut self, x: usize, y: usize, z: usize, faces: u8) {
let faces = faces & 0b11_1111;
if self.exposed_faces(x, y, z) == faces {
return;
}
let exposed = self
.exposed
.get_or_insert_with(|| Box::new([0; CHUNK_VOLUME]));
exposed[Self::get_index(x, y, z)] = faces;
self.dirty = true;
}
pub fn has_exposed_faces(&self) -> bool {
self.exposed
.as_ref()
.is_some_and(|exposed| exposed.iter().any(|faces| *faces != 0))
}
pub fn clear_exposed_faces(&mut self) {
if self.has_exposed_faces() {
self.dirty = true;
}
self.exposed = None;
}
#[inline]
pub fn is_solid_at(&self, x: usize, y: usize, z: usize) -> bool {
self.get(x, y, z).is_solid()
}
pub fn is_fully_solid(&self) -> bool {
self.voxels.iter().all(|v| v.is_solid())
}
pub fn is_empty(&self) -> bool {
self.voxels.iter().all(|v| !v.is_solid())
}
pub fn border_slice(&self, face: usize) -> [V; CHUNK_SIZE * CHUNK_SIZE] {
let mut slice = [V::air(); CHUNK_SIZE * CHUNK_SIZE];
let last = CHUNK_SIZE - 1;
match face {
0 => {
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
slice[x * CHUNK_SIZE + z] = self.get(x, last, z);
}
}
}
1 => {
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
slice[x * CHUNK_SIZE + z] = self.get(x, 0, z);
}
}
}
2 => {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
slice[y * CHUNK_SIZE + z] = self.get(last, y, z);
}
}
}
3 => {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
slice[y * CHUNK_SIZE + z] = self.get(0, y, z);
}
}
}
4 => {
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
slice[x * CHUNK_SIZE + y] = self.get(x, y, last);
}
}
}
5 => {
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
slice[x * CHUNK_SIZE + y] = self.get(x, y, 0);
}
}
}
_ => panic!("invalid face index: {face} (must be 0..6)"),
}
slice
}
}
impl<V: VoxelType> Default for Chunk<V> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::voxel::DefaultVoxel;
#[test]
fn new_chunk_is_all_air() {
let chunk = Chunk::<DefaultVoxel>::new();
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
assert_eq!(chunk.get(x, y, z), DefaultVoxel::Air);
}
}
}
}
#[test]
fn new_chunk_is_clean() {
assert!(!Chunk::<DefaultVoxel>::new().is_dirty());
}
#[test]
fn changing_a_voxel_or_state_marks_the_chunk_dirty() {
let mut chunk = Chunk::<DefaultVoxel>::new();
chunk.set(1, 2, 3, DefaultVoxel::Rock);
assert!(chunk.is_dirty());
let mut chunk = Chunk::<DefaultVoxel>::new();
chunk.set_state(1, 2, 3, 5);
assert!(chunk.is_dirty());
}
#[test]
fn writing_the_same_value_keeps_the_chunk_clean() {
let mut chunk = Chunk::<DefaultVoxel>::new();
chunk.set(1, 2, 3, DefaultVoxel::Air);
chunk.set_state(1, 2, 3, 0);
assert!(!chunk.is_dirty());
}
#[test]
fn mark_clean_and_mark_dirty_override_the_flag() {
let mut chunk = Chunk::<DefaultVoxel>::new();
chunk.set(0, 0, 0, DefaultVoxel::Rock);
chunk.mark_clean();
assert!(!chunk.is_dirty());
chunk.set(0, 0, 0, DefaultVoxel::Rock);
assert!(!chunk.is_dirty());
chunk.mark_dirty();
assert!(chunk.is_dirty());
}
#[test]
fn get_set_round_trip() {
let mut chunk = Chunk::<DefaultVoxel>::new();
chunk.set(3, 7, 11, DefaultVoxel::IronOre);
assert_eq!(chunk.get(3, 7, 11), DefaultVoxel::IronOre);
chunk.set(0, 0, 0, DefaultVoxel::RareMineral);
assert_eq!(chunk.get(0, 0, 0), DefaultVoxel::RareMineral);
chunk.set(15, 15, 15, DefaultVoxel::SiliconCrystal);
assert_eq!(chunk.get(15, 15, 15), DefaultVoxel::SiliconCrystal);
}
#[test]
fn is_fully_solid_on_filled_chunk() {
let mut chunk = Chunk::<DefaultVoxel>::new();
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
chunk.set(x, y, z, DefaultVoxel::Rock);
}
}
}
assert!(chunk.is_fully_solid());
assert!(!chunk.is_empty());
}
#[test]
fn is_empty_on_new_chunk() {
let chunk = Chunk::<DefaultVoxel>::new();
assert!(chunk.is_empty());
assert!(!chunk.is_fully_solid());
}
#[test]
fn border_slice_plus_y() {
let mut chunk = Chunk::<DefaultVoxel>::new();
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
chunk.set(x, 15, z, DefaultVoxel::Rock);
}
}
let slice = chunk.border_slice(0); for v in &slice {
assert_eq!(*v, DefaultVoxel::Rock);
}
let bottom = chunk.border_slice(1);
for v in &bottom {
assert_eq!(*v, DefaultVoxel::Air);
}
}
#[test]
fn border_slice_specific_voxel() {
let mut chunk = Chunk::<DefaultVoxel>::new();
chunk.set(15, 3, 7, DefaultVoxel::IronOre);
let slice = chunk.border_slice(2); assert_eq!(slice[3 * CHUNK_SIZE + 7], DefaultVoxel::IronOre);
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
if y == 3 && z == 7 {
continue;
}
assert_eq!(slice[y * CHUNK_SIZE + z], DefaultVoxel::Air);
}
}
}
}