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]>,
}
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]),
}
}
#[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) {
self.voxels[Self::get_index(x, y, z)] = voxel;
}
#[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) {
self.states[Self::get_index(x, y, z)] = state;
}
#[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 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);
}
}
}
}