use bevy::math::IVec3; use super::{palette_storage::PaletteStorage, BloxelStore, BloxelStoreMut}; use crate::bloxel::math::USize3; pub struct PaletteBloxelStorage { size: USize3, data: PaletteStorage, } impl PaletteBloxelStorage { pub fn new(size: USize3, fill: T) -> Self { let len = size.width.get() * size.height.get() * size.depth.get(); let data = PaletteStorage::new(len as usize, fill); Self { size, data } } pub fn new_with_default(size: USize3) -> Self where T: Default, { Self::new(size, Default::default()) } pub fn get_used_count(&self, value: T) -> usize { self.data.get_used_count(value) } } impl BloxelStore for PaletteBloxelStorage { fn size(&self) -> USize3 { self.size } fn get(&self, pos: IVec3) -> Option { self.size .ivec3_to_index(pos) .map(|index| self.data.get(index)) } } impl BloxelStoreMut for PaletteBloxelStorage { fn set(&mut self, pos: IVec3, value: T) -> Result { self.size .ivec3_to_index(pos) .map(|index| self.data.set(index, value)) .ok_or(()) } }