aboutsummaryrefslogtreecommitdiff
path: root/src/game/map.rs
blob: 420e256a9032065ec68f3c65740329b48258a0c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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]
    }
}