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: f32Implementations§
Source§impl<const M: usize> Stat<M>
impl<const M: usize> Stat<M>
Sourcepub fn new(base_value: f32) -> Self
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?
More examples
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}Sourcepub fn add_modifier(&mut self, modifier: StatModifier) -> StatModifierHandle
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?
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
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 }Sourcepub fn add_modifier_with_order(
&mut self,
modifier: StatModifier,
order: i32,
) -> StatModifierHandle
pub fn add_modifier_with_order( &mut self, modifier: StatModifier, order: i32, ) -> StatModifierHandle
panics if refcell is borrowed
Sourcepub fn value_with_integrated_modifiers(&mut self, other_stat: &Self) -> f32
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
Sourcepub fn highest_order(&self) -> i32
pub fn highest_order(&self) -> i32
Returns the highest order of all modifiers panics if refcell is borrowed
Sourcepub fn value(&self) -> f32
pub fn value(&self) -> f32
Returns the internal base_value with modifiers applied panics if refcell is borrowed
Examples found in repository?
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
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}Sourcepub fn value_with_base(&self, base_value: f32) -> f32
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