mirror of
https://github.com/guezoloic/BakersAdventure.git
synced 2026-01-25 03:34:06 +00:00
feat(character.rs): replace inner weapon var to weapon "class"
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use crate::weapon::Weapon;
|
||||
|
||||
/// # Character
|
||||
///
|
||||
/// Originally, there were two python classes: `Heroes`
|
||||
@@ -7,54 +9,48 @@
|
||||
///
|
||||
/// It consists of:
|
||||
/// - `life` as unsigned 8-bit integer
|
||||
/// - `damage` as unsigned 8-bit integer
|
||||
/// - `mana` as unsigned 8-bit integer
|
||||
/// - `weapon` as Option heap weapon struct
|
||||
/// - `name` as rodata str (string stored in
|
||||
/// read-only memory)
|
||||
///
|
||||
/// It also provides the following methods:
|
||||
/// - `new(u8, u8, u8, &'static str)` function is used
|
||||
/// to be a static constructor of `Character` that return
|
||||
/// the struct initialized.
|
||||
/// - `new(u8, &'static str, Option<Box<Weapon>>)`
|
||||
/// function is used to be a static constructor of
|
||||
/// `Character` that return the struct initialized.
|
||||
|
||||
|
||||
/// - `attack(&mut self, &mut self)` function removes
|
||||
/// other's life based on self damage and return self
|
||||
/// for method chaining.
|
||||
/// - `comsume_mana(&mut self, u8)` function reduces mana
|
||||
/// with the `u8` second parameter.
|
||||
/// - `get_name` is only an accessor of `name`
|
||||
pub struct Character {
|
||||
life: u8,
|
||||
damage: u8,
|
||||
mana: u8,
|
||||
name: &'static str,
|
||||
weapon: Option<Box<Weapon>>,
|
||||
}
|
||||
|
||||
impl Character {
|
||||
/// Creates a new `Character` with the given attributes.
|
||||
/// Acts as a "static constructor" returning the
|
||||
/// initialized struct.
|
||||
pub fn new(life: u8, damage: u8, mana: u8, name: &'static str) -> Self {
|
||||
Character {
|
||||
life,
|
||||
damage,
|
||||
mana,
|
||||
name,
|
||||
}
|
||||
pub fn new(life: u8, name: &'static str, weapon: Option<Box<Weapon>>) -> Self {
|
||||
Character { life, name, weapon }
|
||||
}
|
||||
|
||||
pub fn new_no_weapon(life: u8, name: &'static str) -> Self {
|
||||
Character { life, name, weapon: None }
|
||||
}
|
||||
|
||||
/// Attacks another character, reducing their `life` by
|
||||
/// self.damage. Returns `self` to allow method chaining.
|
||||
pub fn attack(&mut self, other: &mut Self) -> &mut Self {
|
||||
other.life -= self.damage;
|
||||
if let Some(w) = &mut self.weapon {
|
||||
w.if_not_broken(|damage: u8| other.life -= damage);
|
||||
w.consume();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Consumes `consumption` amount of mana.
|
||||
/// Returns `self` to allow method chaining.
|
||||
pub fn consume_mana(&mut self, consumption: u8) -> &mut Self {
|
||||
self.mana -= consumption;
|
||||
self
|
||||
}
|
||||
/// Returns the character's name.
|
||||
pub fn get_name(&self) -> &str {
|
||||
self.name
|
||||
@@ -68,42 +64,21 @@ mod tests {
|
||||
#[test]
|
||||
/// Tests that a character is correctly initialized.
|
||||
fn character_build() {
|
||||
let c = Character::new(10, 5, 3, "test1");
|
||||
assert_eq!(c.life, 10);
|
||||
assert_eq!(c.damage, 5);
|
||||
assert_eq!(c.mana, 3);
|
||||
let c = Character::new_no_weapon(100, "test");
|
||||
assert_eq!(c.life, 100);
|
||||
assert_eq!(c.name, c.get_name());
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Tests that attacking reduces the target's life correctly.
|
||||
fn character_attack() {
|
||||
let mut c1 = Character::new(10, 3, 5, "test1");
|
||||
let mut c2 = Character::new(10, 2, 5, "test2");
|
||||
let w = Box::new(Weapon::new("test", 10, 100));
|
||||
let mut c1 = Character::new(100, "test1", Option::Some(w));
|
||||
let mut c2 = Character::new_no_weapon(200, "test2");
|
||||
|
||||
c1.attack(&mut c2);
|
||||
assert_eq!(c2.life, 7);
|
||||
assert_eq!(c2.life, 190);
|
||||
Character::attack(&mut c2, &mut c1);
|
||||
assert_eq!(c1.life, 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Tests that consuming mana reduces it correctly.
|
||||
fn character_mana() {
|
||||
let mut c = Character::new(10, 3, 15, "test1");
|
||||
c.consume_mana(10);
|
||||
assert_eq!(c.mana, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Tests method chaining: attack -> consume mana
|
||||
/// -> attack.
|
||||
fn character_chaining() {
|
||||
let mut c1 = Character::new(10, 3, 15, "test1");
|
||||
let mut c2 = Character::new(10, 2, 15, "test2");
|
||||
|
||||
c1.attack(&mut c2).consume_mana(10).attack(&mut c2);
|
||||
assert_eq!(c2.life, 4);
|
||||
assert_eq!(c1.mana, 5);
|
||||
assert_eq!(c1.life, 100);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user