···11+#![cfg(feature = "bevy")]
22+//! Contains systems and components for resetting [`StatContainer`]s in the Bevy game engine.
33+14use crate::StatContainer;
25use crate::modifier::Modifier;
36use crate::stat::Stat;
···811use bevy_reflect::Reflect;
912use bevy_reflect::prelude::ReflectDefault;
10131111-pub struct ImmediateStatesPlugin;
1414+/// Registers all types used by Immediate Stats with the Bevy type registry.
1515+pub struct ImmediateStatsPlugin;
12161313-impl Plugin for ImmediateStatesPlugin {
1717+impl Plugin for ImmediateStatsPlugin {
1418 fn build(&self, app: &mut App) {
1519 app.register_type::<PauseStatReset>()
1620 .register_type::<Stat>()
···2428#[reflect(Component, PartialEq, Debug, Default, Clone)]
2529pub struct PauseStatReset;
26303131+/// Calls [`StatContainer::reset_modifiers`] on all `T` components.
2732pub fn reset_component_modifiers<T: Component<Mutability = Mutable> + StatContainer>(
2833 mut query: Query<&mut T, Without<PauseStatReset>>,
2934) {
···3237 }
3338}
34394040+/// Calls [`StatContainer::reset_modifiers`] on all the `T` resource, if it exists.
3541pub fn reset_resource_modifiers<T: Resource + StatContainer>(res: Option<ResMut<T>>) {
3642 if let Some(mut res) = res {
3743 res.reset_modifiers();
+5-4
immediate_stats/src/lib.rs
···55pub mod modifier;
66pub mod stat;
7788+/// Todo
89pub use immediate_stats_macros::StatContainer;
910pub use modifier::*;
1011pub use stat::*;
11121213#[cfg(feature = "bevy")]
1314pub use bevy::*;
1414-#[cfg(feature = "bevy")]
1515+#[cfg(feature = "bevy")] // Used by derive macro.
1616+#[doc(hidden)]
1517pub use bevy_app::prelude::PreUpdate;
16181719/// Types that contain stats that need to be reset.
1820///
1919-/// It is recommended to use the [derive macro](macro) instead of implementing manually.
2020-///
2121-/// [macro]: immediate_stats_macros::StatContainer
2121+/// It is recommended to use the [derive macro](macro@StatContainer)
2222+/// instead of implementing manually.
2223#[cfg_attr(feature = "bevy", bevy_reflect::reflect_trait)]
2324pub trait StatContainer {
2425 /// Resets all stat bonuses to zero, and stat multipliers to one.
+5
immediate_stats/src/modifier.rs
···11+//! Contains a modifier that can be applied to [`Stat`](crate::Stat).
22+13use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
2435/// Modifier values that can be applied to a [`super::Stat`].
···1517}
16181719impl Modifier {
2020+ /// Creates a new modifier from a bonus and a multiplier.
1821 pub fn new(bonus: i32, multiplier: f32) -> Self {
1922 Self { bonus, multiplier }
2023 }
21242525+ /// Creates a new modifier from a bonus.
2226 pub fn from_bonus(bonus: i32) -> Self {
2327 Self {
2428 bonus,
···2630 }
2731 }
28323333+ /// Creates a new modifier from a multiplier.
2934 pub fn from_multiplier(multiplier: f32) -> Self {
3035 Self {
3136 multiplier,
+14-9
immediate_stats/src/stat.rs
···11+//! Contains the basic stat object.
22+13use crate::StatContainer;
24use crate::modifier::Modifier;
35use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
4655-/// A stat that [resets] to a base value every iteration.
77+/// A stat that [resets][reset] to a base value every iteration.
68///
77-/// Temporary bonuses can be applied using [`+=`](add), [`-=`](sub), [`*=`](mul), and [`/=`](div).
99+/// Temporary bonuses can be applied using [`+=`][add], [`-=`][sub], [`*=`][mul], and [`/=`][div].
810/// During [calculation](Stat::total),
911/// multiplication and division are always applied **after** addition and subtraction.
1010-/// These bonuses are reset when [`reset_modifiers`](reset) is called.
1212+/// These bonuses are reset when [`reset_modifiers`][reset] is called.
1113///
1212-/// [reset]:StatContainer::reset_modifiers
1313-/// [add]:Stat::add_assign
1414-/// [sub]:Stat::sub_assign
1515-/// [mul]:Stat::mul_assign
1616-/// [div]:Stat::div_assign
1414+/// [reset]: StatContainer::reset_modifiers
1515+/// [add]: Stat::add_assign
1616+/// [sub]: Stat::sub_assign
1717+/// [mul]: Stat::mul_assign
1818+/// [div]: Stat::div_assign
1719#[derive(PartialEq, Debug, Copy, Clone)]
1820#[cfg_attr(
1921 feature = "bevy",
···3638}
37393840impl Stat {
4141+ /// Creates a new modifier from a base value.
3942 pub fn new(base: i32) -> Self {
4043 Self {
4144 base,
···4851 ((self.base + self.bonus) as f32 * self.multiplier) as i32
4952 }
50535454+ /// A builder that overwrites the current bonus with a new value.
5155 pub fn with_bonus(mut self, bonus: i32) -> Self {
5256 self.bonus = bonus;
5357 self
5458 }
55596060+ /// A builder that overwrites the multiplier bonus with a new value.
5661 pub fn with_multiplier(mut self, multiplier: f32) -> Self {
5762 self.multiplier = multiplier;
5863 self
5964 }
60656161- /// Sets the bonus and multiplier to the [`Modifier`] values.
6666+ /// A builder that overwrites the current bonus and multiplier with a new value.
6267 pub fn with_modifier(mut self, modifier: Modifier) -> Self {
6368 self.bonus = modifier.bonus;
6469 self.multiplier = modifier.multiplier;
+8-1
immediate_stats/tests/bevy.rs
···11-#![cfg(feature = "bevy")]
21//! Tests the reset systems and `PauseStatReset`.
22+#![cfg(feature = "bevy")]
3344use bevy_ecs::prelude::*;
55use immediate_stats::*;
···6464 Some(Health(Stat::new(100))).as_ref()
6565 );
6666}
6767+6868+#[test]
6969+fn reset_resource_not_present() {
7070+ let mut world = World::new();
7171+ let system = world.register_system(reset_resource_modifiers::<Health>);
7272+ world.run_system(system).unwrap();
7373+}
+1-1
immediate_stats/tests/bevy_butler.rs
···11-#![cfg(feature = "bevy_butler")]
21//! Tests the `add_component` attribute for automatic system registration.
22+#![cfg(feature = "bevy_butler")]
3344use crate::StatContainer;
55use crate::stat::Stat;