From 31f7e4a9ca798f42de1d2fca1c5fd21adb21dd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Mon, 12 Jan 2026 23:52:27 +0100 Subject: [PATCH] feat(weapon.rs): add weapon struct and impl --- src/weapon.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/weapon.rs 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 + } +}