core_rust/core/game/utils/
unit.rs

1/// The controllable unit moving on the grid.
2#[derive(Debug, Clone, Copy)]
3pub struct Unit {
4    x: usize,
5    y: usize,
6    movement_points: i8,
7    symbol: char,
8}
9
10impl Default for Unit {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl Unit {
17    /// Create a unit at (0,0) with one movement point and symbol '@'.
18    pub fn new() -> Self {
19        Unit {
20            x: 0,
21            y: 0,
22            movement_points: 1,
23            symbol: '@',
24        }
25    }
26
27    /// Symbol used when printing the world.
28    pub fn get_symbol(self) -> char {
29        self.symbol
30    }
31
32    /// Consume one movement point.
33    pub fn deduct_movement(&mut self) {
34        self.movement_points -= 1;
35    }
36
37    /// Remaining movement points.
38    pub fn get_movement(self) -> i8 {
39        self.movement_points
40    }
41
42    /// Reset movement to one point.
43    pub fn reset_movement(&mut self) {
44        self.movement_points = 1;
45    }
46
47    /// Current coordinates `(x, y)`.
48    pub fn get_position(self) -> (usize, usize) {
49        (self.x, self.y)
50    }
51
52    /// Set coordinates to `(x, y)`.
53    pub fn set_position(&mut self, x: usize, y: usize) {
54        self.x = x;
55        self.y = y;
56    }
57}