diff options
Diffstat (limited to 'src/game/map.rs')
-rw-r--r-- | src/game/map.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/game/map.rs b/src/game/map.rs new file mode 100644 index 0000000..420e256 --- /dev/null +++ b/src/game/map.rs @@ -0,0 +1,41 @@ +use glam::IVec2; +use std::ops::{Index, IndexMut}; + +pub struct Map { + pub cells: Vec<Option<u32>>, + pub size: IVec2, +} +impl Map { + pub fn new(width: usize, height: usize) -> Self { + Self { + cells: vec![None; width * height], + size: IVec2::new(width as i32, height as i32), + } + } + pub fn index(&self, v: IVec2) -> usize { + let i = v.x.rem_euclid(self.size.x as i32) + + v.y.rem_euclid(self.size.y as i32) * self.size.x as i32; + i as usize + } + pub fn clear_player(&mut self, p: u32) { + self.cells.iter_mut().for_each(|c| { + if let Some(cm) = c { + if *cm == p { + *c = None; + } + } + }); + } +} +impl Index<IVec2> for Map { + type Output = Option<u32>; + fn index(&self, index: IVec2) -> &Self::Output { + &self.cells[self.index(index)] + } +} +impl IndexMut<IVec2> for Map { + fn index_mut(&mut self, index: IVec2) -> &mut Self::Output { + let i = self.index(index); + &mut self.cells[i] + } +} |