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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use serde::{Deserialize, Serialize};

use super::{Entity, Item};
use crate::{
    dice_roll,
    types::{Action, CmdResult, EnemyStatus, Items},
};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Enemy {
    name: String,
    desc: String,
    inspect: String,
    hp: i32,
    ac: i32,
    xp: u32,
    damage: u32,
    status: EnemyStatus,
    #[serde(default)]
    loot: Items,
}

impl Enemy {
    pub fn new(name: &str, inspect: &str, status: EnemyStatus) -> Self {
        Self {
            name: name.to_owned(),
            desc: format!("There is a {} here.", name),
            inspect: inspect.to_owned(),
            hp: 1,
            ac: 0,
            xp: 0,
            damage: 1,
            status,
            loot: Items::new(),
        }
    }
    pub fn new_rats(status: EnemyStatus) -> Self {
        Self {
            name: String::from("swarm of rats"),
            desc: String::from("A swarm of rats raves along the floor."),
            inspect: String::from("The creatures chatter and scrape viciously."),
            hp: 24,
            ac: 0,
            xp: 25,
            damage: 2,
            status,
            loot: Items::new(),
        }
    }

    pub fn new_pirate(status: EnemyStatus) -> Self {
        Self {
            name: String::from("pirate"),
            desc: String::from("There is a pirate here."),
            inspect: String::from("The pirate is armed and smells vile."),
            hp: dice_roll(2, 7) as i32 + 2,
            ac: 10,
            xp: 50,
            damage: 6,
            status,
            loot: Items::new(),
        }
    }

    pub fn with_desc(mut self, desc: &str) -> Self {
        self.desc = String::from(desc);
        self
    }
    pub fn with_inspect(mut self, inspect: &str) -> Self {
        self.inspect = String::from(inspect);
        self
    }
    pub fn with_hp(mut self, hp: i32) -> Self {
        self.hp = hp;
        self
    }
    pub fn with_ac(mut self, ac: i32) -> Self {
        self.ac = ac;
        self
    }
    pub fn with_xp(mut self, xp: u32) -> Self {
        self.xp = xp;
        self
    }
    pub fn with_damage(mut self, damage: u32) -> Self {
        self.damage = damage;
        self
    }
    pub fn with_item(mut self, item: Item) -> Self {
        self.loot.push(Box::new(item));
        self
    }

    pub fn long_desc(&self) -> String {
        match self.status {
            EnemyStatus::Asleep => format!("{} It is asleep.", self.desc),
            _ => self.desc.to_owned(),
        }
    }

    pub const fn xp(&self) -> u32 {
        self.xp
    }

    pub fn damage(&self) -> u32 {
        dice_roll(1, self.damage)
    }

    pub fn is_angry(&self) -> bool {
        self.status == EnemyStatus::Angry
    }

    pub const fn status(&self) -> EnemyStatus {
        self.status
    }

    pub fn make_angry(&mut self) {
        self.status = EnemyStatus::Angry;
    }

    pub const fn loot(&self) -> &Items {
        &self.loot
    }

    pub fn take_damage(&mut self, damage: u32) -> Option<CmdResult> {
        self.make_angry();

        if dice_roll(1, 20) as i32 >= self.ac {
            self.hp -= damage as i32;
            None
        } else {
            match dice_roll(1, 3) {
                0 => Some(CmdResult::new(
                    Action::Active,
                    format!(
                        "\nYou swung at the {}, but it dodged out of the way.",
                        self.name
                    ),
                )),
                1 => Some(CmdResult::new(
                    Action::Active,
                    format!(
                        "\nYou hit the {}, but its armor absorbed the blow.",
                        self.name
                    ),
                )),
                _ => Some(CmdResult::new(
                    Action::Active,
                    format!("\nThe {} deftly blocked your attack.", self.name),
                )),
            }
        }
    }

    pub const fn is_alive(&self) -> bool {
        self.hp > 0
    }

    pub fn drop_loot(&mut self) -> Items {
        self.loot.drain(0..).collect()
    }
}

impl Entity for Enemy {
    fn name(&self) -> &str {
        &self.name
    }

    fn desc(&self) -> &str {
        &self.desc
    }

    fn inspect(&self) -> &str {
        &self.inspect
    }
}