diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-04 16:48:07 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-04 16:48:07 +0200 |
commit | 34967cd3b6530656ef0bf31810f9fd6dfb853765 (patch) | |
tree | 912bb0995db6997b601f246cfb0420b6b3ab2101 /src/game/map.rs | |
parent | ce0b808a01081322abc7ed51e09d0f452b606ad7 (diff) | |
download | gpn-tron-rust-34967cd3b6530656ef0bf31810f9fd6dfb853765.tar gpn-tron-rust-34967cd3b6530656ef0bf31810f9fd6dfb853765.tar.bz2 gpn-tron-rust-34967cd3b6530656ef0bf31810f9fd6dfb853765.tar.zst |
can vie games
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] + } +} |