feat(weapon.rs): add weapon struct and impl

This commit is contained in:
2026-01-12 23:52:27 +01:00
parent 6d89db211b
commit 31f7e4a9ca

36
src/weapon.rs Normal file
View File

@@ -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<T>(&mut self, fun: impl FnOnce(u8) -> T) -> Option<T> {
if !self.is_broken() {
Some(fun(self.damage))
} else {
None
}
}
pub fn get_name(&self) -> &'static str {
self.name
}
}