Skip to main content

vec_rac/
grid.rs

1use crate::vector::Vector;
2
3#[derive(Clone, PartialEq, PartialOrd, Debug)]
4pub struct Grid {
5    width: usize,
6    height: usize,
7    v_off: Vector,
8    grid: Vec<bool>,
9}
10
11impl Grid {
12    pub fn new(width: usize, height: usize) -> Grid {
13        Grid {
14            width,
15            height,
16            v_off: Vector {
17                x: (width as i32) / 2,
18                y: (height as i32) / 2,
19            },
20            grid: vec![false; width * height],
21        }
22    }
23
24    pub fn get(&self, x: usize, y: usize) -> Option<bool> {
25        if x < self.width && y < self.height {
26            Some(self.grid[x + y * self.width])
27        } else {
28            None
29        }
30    }
31
32    pub fn v_get(&self, pos: Vector) -> Option<bool> {
33        let pos = pos + self.v_off;
34        self.get(pos.x as usize, pos.y as usize)
35    }
36
37    pub fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut bool> {
38        if x < self.width && y < self.height {
39            Some(&mut self.grid[x + y * self.width])
40        } else {
41            None
42        }
43    }
44
45    pub fn v_get_mut(&mut self, pos: Vector) -> Option<&mut bool> {
46        let pos = pos + self.v_off;
47        self.get_mut(pos.x as usize, pos.y as usize)
48    }
49
50    pub fn clear(&mut self) {
51        for cell in self.grid.iter_mut() {
52            *cell = false;
53        }
54    }
55}