diff --git a/src/weapon.rs b/src/weapon.rs new file mode 100644 index 0000000..4a0ee67 --- /dev/null +++ b/src/weapon.rs @@ -0,0 +1,36 @@ +pub struct Weapon { + name: &'static str, + damage: u8, + durability: u8, +} + +impl Weapon { + pub fn new(name: &'static str, damage: u8, durability: u8) -> Self { + Weapon { + name, + damage, + durability, + } + } + + pub fn consume(&mut self) -> &mut Self { + self.durability -= (self.damage / 2) as u8; + self + } + + fn is_broken(&self) -> bool { + self.durability <= 0 + } + + pub fn if_not_broken(&mut self, fun: impl FnOnce(u8) -> T) -> Option { + if !self.is_broken() { + Some(fun(self.damage)) + } else { + None + } + } + + pub fn get_name(&self) -> &'static str { + self.name + } +}