diff --git a/src/types.rs b/src/types.rs index 3a0bb22..a742977 100644 --- a/src/types.rs +++ b/src/types.rs @@ -52,6 +52,40 @@ impl Debug for LayerBlockCoords { } } +/// Generic array for data stored per block of a chunk layer +/// +/// Includes various convenient iteration functions. +#[derive(Debug, Clone, Copy, Default)] +pub struct LayerBlockArray(pub [[T; BLOCKS_PER_CHUNK]; BLOCKS_PER_CHUNK]); + +impl LayerBlockArray { + pub fn keys() -> impl Iterator + Clone + Debug { + iproduct!(BlockZ::iter(), BlockX::iter()).map(|(z, x)| LayerBlockCoords { x, z }) + } + + pub fn values(&self) -> impl Iterator + Clone + Debug { + Self::keys().map(|k| &self[k]) + } + + pub fn iter(&self) -> impl Iterator + Clone + Debug { + Self::keys().map(|k| (k, &self[k])) + } +} + +impl Index for LayerBlockArray { + type Output = T; + + fn index(&self, index: LayerBlockCoords) -> &Self::Output { + &self.0[index.z.0 as usize][index.x.0 as usize] + } +} + +impl IndexMut for LayerBlockArray { + fn index_mut(&mut self, index: LayerBlockCoords) -> &mut Self::Output { + &mut self.0[index.z.0 as usize][index.x.0 as usize] + } +} + /// X, Y and Z coordinates of a block in a chunk section #[derive(Clone, Copy, PartialEq, Eq)] pub struct SectionBlockCoords {