diff --git a/immediate_stats/src/lib.rs b/immediate_stats/src/lib.rs index 2e3e031..82d9127 100644 --- a/immediate_stats/src/lib.rs +++ b/immediate_stats/src/lib.rs @@ -2,6 +2,11 @@ pub mod bevy; pub mod stat; +/// Types that contain stats that need to be reset. +/// +/// It is recommended to use the [derive macro](macro) instead of implementing manually. +/// +/// [macro]: immediate_stats_macros::StatContainer pub trait StatContainer { /// Resets all stat bonuses to zero, and stat multipliers to one. fn reset_modifiers(&mut self); diff --git a/immediate_stats/src/stat.rs b/immediate_stats/src/stat.rs index 5bf1d2b..2fca161 100644 --- a/immediate_stats/src/stat.rs +++ b/immediate_stats/src/stat.rs @@ -1,10 +1,31 @@ use crate::StatContainer; use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; +/// A stat that [resets] to a base value every iteration. +/// +/// Temporary bonuses can be applied using [`+=`](add), [`-=`](sub), [`*=`](mul), and [`/=`](div). +/// During [calculation](Stat::total), +/// multiplication and division are always applied **after** addition and subtraction. +/// These bonuses are reset when [`reset_modifiers`](reset) is called. +/// +/// [reset]:StatContainer::reset_modifiers +/// [add]:Stat::add_assign +/// [sub]:Stat::sub_assign +/// [mul]:Stat::mul_assign +/// [div]:Stat::div_assign #[derive(PartialEq, Debug, Copy, Clone)] pub struct Stat { + /// The persistent value of the stat. pub base: i32, + /// Added to `base` during calculation and gets reset to zero every iteration. + /// This is added *before* `multiplier` is applied. + /// + /// Can be modified using [`Stat::add_assign`] and [`Stat::sub_assign`] pub bonus: i32, + /// Multiplies the `base` during calculation and gets reset to one every iteration. + /// This is applied *after* `bonus` is added. + /// + /// Can be modified using [`Stat::mul_assign`] and [`Stat::div_assign`] pub multiplier: f32, } @@ -16,6 +37,7 @@ impl Stat { } } + /// Calculates the total value of the stat. pub fn total(&self) -> i32 { ((self.base + self.bonus) as f32 * self.multiplier) as i32 } diff --git a/immediate_stats_macros/src/lib.rs b/immediate_stats_macros/src/lib.rs index 7f8a641..f5baa51 100644 --- a/immediate_stats_macros/src/lib.rs +++ b/immediate_stats_macros/src/lib.rs @@ -114,12 +114,14 @@ fn stat_container_enum(e: DataEnum) -> TokenStream { } } +/// The ways to identify fields. enum FieldAccess { Ident(Vec), Index(Vec), None, } +/// Returns a list of all fields that either have type `Stat` or are tagged with `#[stat]`. fn get_names_from_fields>(fields: T) -> FieldAccess { let mut names = Vec::new(); let mut nums = Vec::new();