rust_warrior/
unit.rs

1//! contains types that represent units that appear in the game
2
3use crate::Direction;
4
5/// The Warrior (our protagonist), enemy Sludges and Archers, and Captives.
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub enum UnitType {
8    Archer,
9    Captive,
10    Sludge,
11    ThickSludge,
12    Warrior,
13    Wizard,
14}
15
16impl UnitType {
17    /// A character (`&str` for convenience) representation of the unit type
18    pub fn draw(self) -> &'static str {
19        match self {
20            UnitType::Archer => "a",
21            UnitType::Captive => "C",
22            UnitType::Sludge => "s",
23            UnitType::ThickSludge => "S",
24            UnitType::Warrior => "@",
25            UnitType::Wizard => "w",
26        }
27    }
28}
29
30/// The state of a unit: its `position`, current/max `hp`, and `atk` power.
31#[derive(Clone, Debug)]
32pub struct Unit {
33    pub unit_type: UnitType,
34    pub position: (i32, i32),
35    pub hp: (i32, i32),
36    pub atk: i32,
37    pub facing: Option<Direction>,
38}
39
40impl Unit {
41    /// Create a unit of type `unit_type` at `position`.
42    pub fn new(unit_type: UnitType, position: (i32, i32)) -> Unit {
43        match unit_type {
44            UnitType::Archer => Unit::archer(position),
45            UnitType::Captive => Unit::captive(position),
46            UnitType::Sludge => Unit::sludge(position),
47            UnitType::ThickSludge => Unit::thick_sludge(position),
48            UnitType::Warrior => Unit::warrior(position),
49            UnitType::Wizard => Unit::wizard(position),
50        }
51    }
52
53    /// Create a unit of type Archer (7 HP, 3 ATK) at `position`.
54    pub fn archer(position: (i32, i32)) -> Unit {
55        Unit {
56            unit_type: UnitType::Archer,
57            position,
58            hp: (7, 7),
59            atk: 3,
60            facing: None,
61        }
62    }
63
64    /// Create a unit of type Captive (1 HP, 0 ATK) at `position`.
65    pub fn captive(position: (i32, i32)) -> Unit {
66        Unit {
67            unit_type: UnitType::Captive,
68            position,
69            hp: (1, 1),
70            atk: 0,
71            facing: None,
72        }
73    }
74
75    /// Create a unit of type Sludge (12 HP, 3 ATK) at `position`.
76    pub fn sludge(position: (i32, i32)) -> Unit {
77        Unit {
78            unit_type: UnitType::Sludge,
79            position,
80            hp: (12, 12),
81            atk: 3,
82            facing: None,
83        }
84    }
85
86    /// Create a unit of type ThickSludge (18 HP, 3 ATK) at `position`.
87    pub fn thick_sludge(position: (i32, i32)) -> Unit {
88        Unit {
89            unit_type: UnitType::ThickSludge,
90            position,
91            hp: (18, 18),
92            atk: 3,
93            facing: None,
94        }
95    }
96
97    /// Create a unit of type Warrior (20 HP, 5 ATK) at `position`.
98    pub fn warrior(position: (i32, i32)) -> Unit {
99        Unit {
100            unit_type: UnitType::Warrior,
101            position,
102            hp: (20, 20),
103            atk: 5,
104            facing: Some(Direction::Forward),
105        }
106    }
107
108    /// Create a unit of type Wizard (3 HP, 11 ATK) at `position`.
109    pub fn wizard(position: (i32, i32)) -> Unit {
110        Unit {
111            unit_type: UnitType::Wizard,
112            position,
113            hp: (3, 3),
114            atk: 11,
115            facing: None,
116        }
117    }
118}