Stat

Struct Stat 

Source
pub struct Stat<const M: usize> {
    pub base_value: f32,
    /* private fields */
}
Expand description

A value that can be modified through super::StatModifier

const M: usize decides how many modifiers a stat can maximally hold (modifier are internally an array on the stack)

Fields§

§base_value: f32

Implementations§

Source§

impl<const M: usize> Stat<M>

Source

pub fn new(base_value: f32) -> Self

// EXAMPLE: Creates a stat that can hold a maximum of 3 modifiers
let attack_stat: Stat<3> = Stat::new(0.0);
let attack_stat = Stat::<3>::new(0.0);
Examples found in repository?
examples/clean_code.rs (line 8)
7fn main() {
8    let mut _armor_stat: GameStat = Stat::new(10f32);
9    let mut _attack_damage_stat: GameStat = Stat::new(10f32);
10}
More examples
Hide additional examples
examples/daggers.rs (line 26)
22    pub fn new(base_attack_damage: f32) -> Self {
23        Self {
24            inventory: Vec::with_capacity(4),
25            hand: None,
26            attack_damage_stat: Stat::new(base_attack_damage),
27        }
28    }
examples/simple.rs (line 7)
5fn main() {
6    // our use case will never exceed 2 modifiers 'should be set according to your constraints'
7    let mut armor_stat: Stat<2> = Stat::new(10f32);
8    println!("armor_stat: {}", armor_stat.value());
9    {
10        // let's make our armor stat stronger
11        // this modifier is valid as long as the key generated from add_modifier() exists
12        let _modifier_key = armor_stat.add_modifier(StatModifier::Flat(5f32));
13        println!("armor_stat: {}", armor_stat.value());
14    } // _modifier_key is dropped here, that invalidates the modifiers and gets removed next .value() call
15
16    // our stat should be 10 again
17    println!("armor_stat: {}", armor_stat.value());
18}
Source

pub fn add_modifier(&mut self, modifier: StatModifier) -> StatModifierHandle

Add a modifier using the default order. super::StatModifier::default_order() panics if refcell is borrowed

Examples found in repository?
examples/simple.rs (line 12)
5fn main() {
6    // our use case will never exceed 2 modifiers 'should be set according to your constraints'
7    let mut armor_stat: Stat<2> = Stat::new(10f32);
8    println!("armor_stat: {}", armor_stat.value());
9    {
10        // let's make our armor stat stronger
11        // this modifier is valid as long as the key generated from add_modifier() exists
12        let _modifier_key = armor_stat.add_modifier(StatModifier::Flat(5f32));
13        println!("armor_stat: {}", armor_stat.value());
14    } // _modifier_key is dropped here, that invalidates the modifiers and gets removed next .value() call
15
16    // our stat should be 10 again
17    println!("armor_stat: {}", armor_stat.value());
18}
More examples
Hide additional examples
examples/daggers.rs (line 52)
42    pub fn equip_item_from_index(&mut self, i: usize) {
43        // return previous equipment to inventory
44        if let Some(previous_equip) = self.hand.take() {
45            self.inventory.push(previous_equip.0);
46        }
47        // move from inventory to hand
48        let new_dagger = self.inventory.remove(i);
49        // extract the modifier from the dagger stats
50        let modifier_key = self
51            .attack_damage_stat
52            .add_modifier(StatModifier::Flat(new_dagger.attack_damage));
53
54        // adding a modifier can fail if we exceed the max amount of modifiers
55        // T should be carefully selected for for Stat<T>
56        self.hand = Some((new_dagger, modifier_key));
57    }
Source

pub fn add_modifier_with_order( &mut self, modifier: StatModifier, order: i32, ) -> StatModifierHandle

panics if refcell is borrowed

Source

pub fn value_with_integrated_modifiers(&mut self, other_stat: &Self) -> f32

returns base value with modifiers applied from self AND other stats’s modifiers the other_stat’s modifiers are all applied after ‘self’ applies it’s modifiers the base value from other_stat is not taken into any account panics if refcell is borrowed

Source

pub fn highest_order(&self) -> i32

Returns the highest order of all modifiers panics if refcell is borrowed

Source

pub fn value(&self) -> f32

Returns the internal base_value with modifiers applied panics if refcell is borrowed

Examples found in repository?
examples/daggers.rs (line 60)
59    pub fn hurt_monster(&mut self, monster: &mut Monster) {
60        monster.health -= self.attack_damage_stat.value();
61
62        // just some flavoring
63        let attack_method = match &self.hand {
64            Some(dagger) => &dagger.0.name,
65            None => "just his hands",
66        };
67        println!(
68            "using: {}, monster took: {} damage! now it has {} health",
69            attack_method,
70            self.attack_damage_stat.value(),
71            monster.health
72        );
73    }
More examples
Hide additional examples
examples/simple.rs (line 8)
5fn main() {
6    // our use case will never exceed 2 modifiers 'should be set according to your constraints'
7    let mut armor_stat: Stat<2> = Stat::new(10f32);
8    println!("armor_stat: {}", armor_stat.value());
9    {
10        // let's make our armor stat stronger
11        // this modifier is valid as long as the key generated from add_modifier() exists
12        let _modifier_key = armor_stat.add_modifier(StatModifier::Flat(5f32));
13        println!("armor_stat: {}", armor_stat.value());
14    } // _modifier_key is dropped here, that invalidates the modifiers and gets removed next .value() call
15
16    // our stat should be 10 again
17    println!("armor_stat: {}", armor_stat.value());
18}
Source

pub fn value_with_base(&self, base_value: f32) -> f32

Returns the INPUT base_value (ignores self) with modifiers applied panics if refcell is borrowed

Trait Implementations§

Source§

impl<const M: usize> Clone for Stat<M>

Source§

fn clone(&self) -> Stat<M>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const M: usize> Debug for Stat<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const M: usize> Default for Stat<M>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const M: usize> From<f32> for Stat<M>

create a stat from i32 (Stat is always internally a f32)

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl<const M: usize> From<i32> for Stat<M>

create a stat from i32 (Stat is always internally a f32)

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<const M: usize> !Freeze for Stat<M>

§

impl<const M: usize> !RefUnwindSafe for Stat<M>

§

impl<const M: usize> !Send for Stat<M>

§

impl<const M: usize> !Sync for Stat<M>

§

impl<const M: usize> Unpin for Stat<M>

§

impl<const M: usize> !UnwindSafe for Stat<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.