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.

Start on README.

+172 -6
+73
README.md
··· 1 + # Immediate Stats 2 + 3 + Game stats that reset every frame. Inspired by immediate mode rendering. 4 + 5 + == Todo Info about derive macro. 6 + 7 + ```rust no_run 8 + use immediate_stats::*; 9 + 10 + #[derive(StatContainer)] 11 + struct Speed(Stat); 12 + 13 + fn main() { 14 + loop { 15 + let mut speed = Speed(Stat::new(10)); // Set base speed to 10. 16 + 17 + speed.0 *= 2.0; // Applies a multiplier to the final result. 18 + speed.0 += 5; // Adds a bonus to the final result. 19 + // The order does not matter. Bonuses are always applied before multipliers. 20 + assert_eq!(speed.0.total(), 30); // (10 + 5) * 2 = 30 21 + 22 + speed.reset_modifiers(); // Reset bonus and multiplier, so speed is back to 10. 23 + } 24 + } 25 + ``` 26 + 27 + ## Bevy 28 + 29 + There is build-in integration with the [Bevy Engine](https://bevyengine.org) via the `bevy` feature flag. 30 + This adds systems for resetting `StatContainer` components and resources. 31 + 32 + ```rust ignore 33 + use bevy_app::prelude::*; 34 + use bevy_ecs::prelude::*; 35 + use immediate_stats::*; 36 + 37 + #[derive(StatContainer, Component, Resource)] 38 + struct Speed(Stat); 39 + 40 + fn main() { 41 + App::new() 42 + .add_plugins(ImmediateStatesPlugin) 43 + .add_systems(PreUpdate, ( 44 + reset_component_modifiers::<Speed>, 45 + reset_resource_modifiers::<Speed>, 46 + )) 47 + .run(); 48 + } 49 + ``` 50 + 51 + ### Bevy Butler 52 + 53 + If you use [Bevy Butler](https://github.com/TGRCdev/bevy-butler/), you can also use the `bevy_butler` feature flag. 54 + This automatically registers the required system(s) using the `add_component` attribute. 55 + 56 + ```rust ignore 57 + use bevy_app::prelude::*; 58 + use bevy_ecs::prelude::*; 59 + use immediate_stats::*; 60 + use bevy_butler::*; 61 + 62 + #[butler_plugin] 63 + struct MyPlugin; 64 + 65 + #[derive(StatContainer, Component)] 66 + #[add_component(plugin = MyPlugin)] // Added by `StatContainer` derive. 67 + struct Speed(Stat); 68 + ``` 69 + 70 + ### Version Compatibility 71 + | bevy | immediate_stats | 72 + |--------|-----------------| 73 + | `0.16` | `0.1` |
+7
immediate_stats/Cargo.toml
··· 23 23 ] } 24 24 bevy_reflect = { version = "0.16.0-rc", default-features = false, optional = true } 25 25 immediate_stats_macros = { path = "../immediate_stats_macros", default-features = false } 26 + 27 + [lints.rust] 28 + missing_docs = "warn" 29 + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(docsrs_dep)'] } 30 + unsafe_code = "deny" 31 + unsafe_op_in_unsafe_fn = "warn" 32 + unused_qualifications = "warn"
+73
immediate_stats/README.md
··· 1 + # Immediate Stats 2 + 3 + Game stats that reset every frame. Inspired by immediate mode rendering. 4 + 5 + == Todo Info about derive macro. 6 + 7 + ```rust no_run 8 + use immediate_stats::*; 9 + 10 + #[derive(StatContainer)] 11 + struct Speed(Stat); 12 + 13 + fn main() { 14 + loop { 15 + let mut speed = Speed(Stat::new(10)); // Set base speed to 10. 16 + 17 + speed.0 *= 2.0; // Applies a multiplier to the final result. 18 + speed.0 += 5; // Adds a bonus to the final result. 19 + // The order does not matter. Bonuses are always applied before multipliers. 20 + assert_eq!(speed.0.total(), 30); // (10 + 5) * 2 = 30 21 + 22 + speed.reset_modifiers(); // Reset bonus and multiplier, so speed is back to 10. 23 + } 24 + } 25 + ``` 26 + 27 + ## Bevy 28 + 29 + There is build-in integration with the [Bevy Engine](https://bevyengine.org) via the `bevy` feature flag. 30 + This adds systems for resetting `StatContainer` components and resources. 31 + 32 + ```rust ignore 33 + use bevy_app::prelude::*; 34 + use bevy_ecs::prelude::*; 35 + use immediate_stats::*; 36 + 37 + #[derive(StatContainer, Component, Resource)] 38 + struct Speed(Stat); 39 + 40 + fn main() { 41 + App::new() 42 + .add_plugins(ImmediateStatesPlugin) 43 + .add_systems(PreUpdate, ( 44 + reset_component_modifiers::<Speed>, 45 + reset_resource_modifiers::<Speed>, 46 + )) 47 + .run(); 48 + } 49 + ``` 50 + 51 + ### Bevy Butler 52 + 53 + If you use [Bevy Butler](https://github.com/TGRCdev/bevy-butler/), you can also use the `bevy_butler` feature flag. 54 + This automatically registers the required system(s) using the `add_component` attribute. 55 + 56 + ```rust ignore 57 + use bevy_app::prelude::*; 58 + use bevy_ecs::prelude::*; 59 + use immediate_stats::*; 60 + use bevy_butler::*; 61 + 62 + #[butler_plugin] 63 + struct MyPlugin; 64 + 65 + #[derive(StatContainer, Component)] 66 + #[add_component(plugin = MyPlugin)] // Added by `StatContainer` derive. 67 + struct Speed(Stat); 68 + ``` 69 + 70 + ### Version Compatibility 71 + | bevy | immediate_stats | 72 + |--------|-----------------| 73 + | `0.16` | `0.1` |
+1 -2
immediate_stats/src/bevy/butler.rs
··· 6 6 use bevy_app::App; 7 7 use bevy_butler::*; 8 8 use bevy_ecs::prelude::*; 9 - use bevy_reflect::prelude::*; 10 9 11 10 #[butler_plugin] 12 11 struct MyPlugin; 13 12 14 - #[derive(Reflect, Resource, Component, StatContainer, Default, PartialEq, Debug)] 13 + #[derive(Resource, Component, StatContainer, Default, PartialEq, Debug)] 15 14 #[add_component(plugin = MyPlugin)] 16 15 #[add_resource(plugin = MyPlugin)] 17 16 struct Health(Stat);
+8
immediate_stats/src/lib.rs
··· 1 + #![doc = include_str!("../README.md")] 2 + 1 3 #[cfg(feature = "bevy")] 2 4 pub mod bevy; 3 5 pub mod stat; 4 6 7 + // Todo Move into internal module called `__internal` like Bevy Butler. 8 + #[cfg(feature = "bevy")] 5 9 pub use bevy_app::prelude::PreUpdate; 6 10 pub use immediate_stats_macros::StatContainer; 11 + 12 + #[cfg(feature = "bevy")] 13 + pub use bevy::*; 14 + pub use stat::*; 7 15 8 16 /// Types that contain stats that need to be reset. 9 17 ///
+5 -2
immediate_stats/src/stat.rs
··· 17 17 /// [mul]:Stat::mul_assign 18 18 /// [div]:Stat::div_assign 19 19 #[derive(PartialEq, Debug, Copy, Clone)] 20 - #[cfg_attr(feature = "bevy", derive(bevy_reflect::Reflect))] 21 - #[reflect(PartialEq, Debug, Clone)] 20 + #[cfg_attr( 21 + feature = "bevy", 22 + derive(bevy_reflect::Reflect), 23 + reflect(PartialEq, Debug, Clone) 24 + )] 22 25 pub struct Stat { 23 26 /// The persistent value of the stat. 24 27 pub base: i32,
+5 -2
immediate_stats/src/stat/modifier.rs
··· 2 2 3 3 /// Modifier values that can be applied to a [`super::Stat`]. 4 4 #[derive(PartialEq, Debug, Copy, Clone)] 5 - #[cfg_attr(feature = "bevy", derive(bevy_reflect::Reflect))] 6 - #[reflect(PartialEq, Debug, Clone)] 5 + #[cfg_attr( 6 + feature = "bevy", 7 + derive(bevy_reflect::Reflect), 8 + reflect(PartialEq, Debug, Clone) 9 + )] 7 10 pub struct Modifier { 8 11 /// Added to `base` of a [`super::Stat`] during calculation. 9 12 pub bonus: i32,