Game stats that reset every frame, inspired by immediate mode GUI.
gamedev bevy stats
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Basic documentation for everything.

Also fixed broken links.

+41 -19
-1
README.md
··· 39 39 40 40 fn main() { 41 41 App::new() 42 - .add_plugins(ImmediateStatesPlugin) 43 42 .add_systems(PreUpdate, ( 44 43 reset_component_modifiers::<Speed>, 45 44 reset_resource_modifiers::<Speed>,
-1
immediate_stats/README.md
··· 39 39 40 40 fn main() { 41 41 App::new() 42 - .add_plugins(ImmediateStatesPlugin) 43 42 .add_systems(PreUpdate, ( 44 43 reset_component_modifiers::<Speed>, 45 44 reset_resource_modifiers::<Speed>,
+8 -2
immediate_stats/src/bevy.rs
··· 1 + #![cfg(feature = "bevy")] 2 + //! Contains systems and components for resetting [`StatContainer`]s in the Bevy game engine. 3 + 1 4 use crate::StatContainer; 2 5 use crate::modifier::Modifier; 3 6 use crate::stat::Stat; ··· 8 11 use bevy_reflect::Reflect; 9 12 use bevy_reflect::prelude::ReflectDefault; 10 13 11 - pub struct ImmediateStatesPlugin; 14 + /// Registers all types used by Immediate Stats with the Bevy type registry. 15 + pub struct ImmediateStatsPlugin; 12 16 13 - impl Plugin for ImmediateStatesPlugin { 17 + impl Plugin for ImmediateStatsPlugin { 14 18 fn build(&self, app: &mut App) { 15 19 app.register_type::<PauseStatReset>() 16 20 .register_type::<Stat>() ··· 24 28 #[reflect(Component, PartialEq, Debug, Default, Clone)] 25 29 pub struct PauseStatReset; 26 30 31 + /// Calls [`StatContainer::reset_modifiers`] on all `T` components. 27 32 pub fn reset_component_modifiers<T: Component<Mutability = Mutable> + StatContainer>( 28 33 mut query: Query<&mut T, Without<PauseStatReset>>, 29 34 ) { ··· 32 37 } 33 38 } 34 39 40 + /// Calls [`StatContainer::reset_modifiers`] on all the `T` resource, if it exists. 35 41 pub fn reset_resource_modifiers<T: Resource + StatContainer>(res: Option<ResMut<T>>) { 36 42 if let Some(mut res) = res { 37 43 res.reset_modifiers();
+5 -4
immediate_stats/src/lib.rs
··· 5 5 pub mod modifier; 6 6 pub mod stat; 7 7 8 + /// Todo 8 9 pub use immediate_stats_macros::StatContainer; 9 10 pub use modifier::*; 10 11 pub use stat::*; 11 12 12 13 #[cfg(feature = "bevy")] 13 14 pub use bevy::*; 14 - #[cfg(feature = "bevy")] 15 + #[cfg(feature = "bevy")] // Used by derive macro. 16 + #[doc(hidden)] 15 17 pub use bevy_app::prelude::PreUpdate; 16 18 17 19 /// Types that contain stats that need to be reset. 18 20 /// 19 - /// It is recommended to use the [derive macro](macro) instead of implementing manually. 20 - /// 21 - /// [macro]: immediate_stats_macros::StatContainer 21 + /// It is recommended to use the [derive macro](macro@StatContainer) 22 + /// instead of implementing manually. 22 23 #[cfg_attr(feature = "bevy", bevy_reflect::reflect_trait)] 23 24 pub trait StatContainer { 24 25 /// Resets all stat bonuses to zero, and stat multipliers to one.
+5
immediate_stats/src/modifier.rs
··· 1 + //! Contains a modifier that can be applied to [`Stat`](crate::Stat). 2 + 1 3 use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; 2 4 3 5 /// Modifier values that can be applied to a [`super::Stat`]. ··· 15 17 } 16 18 17 19 impl Modifier { 20 + /// Creates a new modifier from a bonus and a multiplier. 18 21 pub fn new(bonus: i32, multiplier: f32) -> Self { 19 22 Self { bonus, multiplier } 20 23 } 21 24 25 + /// Creates a new modifier from a bonus. 22 26 pub fn from_bonus(bonus: i32) -> Self { 23 27 Self { 24 28 bonus, ··· 26 30 } 27 31 } 28 32 33 + /// Creates a new modifier from a multiplier. 29 34 pub fn from_multiplier(multiplier: f32) -> Self { 30 35 Self { 31 36 multiplier,
+14 -9
immediate_stats/src/stat.rs
··· 1 + //! Contains the basic stat object. 2 + 1 3 use crate::StatContainer; 2 4 use crate::modifier::Modifier; 3 5 use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; 4 6 5 - /// A stat that [resets] to a base value every iteration. 7 + /// A stat that [resets][reset] to a base value every iteration. 6 8 /// 7 - /// Temporary bonuses can be applied using [`+=`](add), [`-=`](sub), [`*=`](mul), and [`/=`](div). 9 + /// Temporary bonuses can be applied using [`+=`][add], [`-=`][sub], [`*=`][mul], and [`/=`][div]. 8 10 /// During [calculation](Stat::total), 9 11 /// multiplication and division are always applied **after** addition and subtraction. 10 - /// These bonuses are reset when [`reset_modifiers`](reset) is called. 12 + /// These bonuses are reset when [`reset_modifiers`][reset] is called. 11 13 /// 12 - /// [reset]:StatContainer::reset_modifiers 13 - /// [add]:Stat::add_assign 14 - /// [sub]:Stat::sub_assign 15 - /// [mul]:Stat::mul_assign 16 - /// [div]:Stat::div_assign 14 + /// [reset]: StatContainer::reset_modifiers 15 + /// [add]: Stat::add_assign 16 + /// [sub]: Stat::sub_assign 17 + /// [mul]: Stat::mul_assign 18 + /// [div]: Stat::div_assign 17 19 #[derive(PartialEq, Debug, Copy, Clone)] 18 20 #[cfg_attr( 19 21 feature = "bevy", ··· 36 38 } 37 39 38 40 impl Stat { 41 + /// Creates a new modifier from a base value. 39 42 pub fn new(base: i32) -> Self { 40 43 Self { 41 44 base, ··· 48 51 ((self.base + self.bonus) as f32 * self.multiplier) as i32 49 52 } 50 53 54 + /// A builder that overwrites the current bonus with a new value. 51 55 pub fn with_bonus(mut self, bonus: i32) -> Self { 52 56 self.bonus = bonus; 53 57 self 54 58 } 55 59 60 + /// A builder that overwrites the multiplier bonus with a new value. 56 61 pub fn with_multiplier(mut self, multiplier: f32) -> Self { 57 62 self.multiplier = multiplier; 58 63 self 59 64 } 60 65 61 - /// Sets the bonus and multiplier to the [`Modifier`] values. 66 + /// A builder that overwrites the current bonus and multiplier with a new value. 62 67 pub fn with_modifier(mut self, modifier: Modifier) -> Self { 63 68 self.bonus = modifier.bonus; 64 69 self.multiplier = modifier.multiplier;
+8 -1
immediate_stats/tests/bevy.rs
··· 1 - #![cfg(feature = "bevy")] 2 1 //! Tests the reset systems and `PauseStatReset`. 2 + #![cfg(feature = "bevy")] 3 3 4 4 use bevy_ecs::prelude::*; 5 5 use immediate_stats::*; ··· 64 64 Some(Health(Stat::new(100))).as_ref() 65 65 ); 66 66 } 67 + 68 + #[test] 69 + fn reset_resource_not_present() { 70 + let mut world = World::new(); 71 + let system = world.register_system(reset_resource_modifiers::<Health>); 72 + world.run_system(system).unwrap(); 73 + }
+1 -1
immediate_stats/tests/bevy_butler.rs
··· 1 - #![cfg(feature = "bevy_butler")] 2 1 //! Tests the `add_component` attribute for automatic system registration. 2 + #![cfg(feature = "bevy_butler")] 3 3 4 4 use crate::StatContainer; 5 5 use crate::stat::Stat;