use glam::IVec2; use std::ops::{Index, IndexMut}; pub struct Map { pub cells: Vec>, 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 for Map { type Output = Option; fn index(&self, index: IVec2) -> &Self::Output { &self.cells[self.index(index)] } } impl IndexMut for Map { fn index_mut(&mut self, index: IVec2) -> &mut Self::Output { let i = self.index(index); &mut self.cells[i] } }