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.

Merge pull request #23 from AlephCubed/dev

Added `Modifier::scaled` and `Display` implementations.

authored by

Josiah Nelson and committed by
GitHub
534d7cb2 e1cc364c

+63 -13
+6 -6
Cargo.lock
··· 452 452 453 453 [[package]] 454 454 name = "cc" 455 - version = "1.2.49" 455 + version = "1.2.50" 456 456 source = "registry+https://github.com/rust-lang/crates.io-index" 457 - checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" 457 + checksum = "9f50d563227a1c37cc0a263f64eca3334388c01c5e4c4861a9def205c614383c" 458 458 dependencies = [ 459 459 "find-msvc-tools", 460 460 "jobserver", ··· 788 788 789 789 [[package]] 790 790 name = "immediate_stats" 791 - version = "0.3.1" 791 + version = "0.3.2" 792 792 dependencies = [ 793 793 "bevy", 794 794 "bevy_app", ··· 800 800 801 801 [[package]] 802 802 name = "immediate_stats_macros" 803 - version = "0.3.1" 803 + version = "0.3.2" 804 804 dependencies = [ 805 805 "darling 0.23.0", 806 806 "proc-macro-error", ··· 1056 1056 1057 1057 [[package]] 1058 1058 name = "portable-atomic" 1059 - version = "1.11.1" 1059 + version = "1.12.0" 1060 1060 source = "registry+https://github.com/rust-lang/crates.io-index" 1061 - checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" 1061 + checksum = "f59e70c4aef1e55797c2e8fd94a4f2a973fc972cfde0e0b05f683667b0cd39dd" 1062 1062 1063 1063 [[package]] 1064 1064 name = "portable-atomic-util"
-3
Cargo.toml
··· 2 2 members = ["immediate_stats", "immediate_stats_macros"] 3 3 4 4 resolver = "3" 5 - 6 - [workspace.package.metadata.docs.rs] 7 - all-features = true
+2 -2
immediate_stats/Cargo.toml
··· 1 1 [package] 2 2 name = "immediate_stats" 3 - version = "0.3.1" 3 + version = "0.3.2" 4 4 edition = "2024" 5 5 description = "Game stats that reset every frame, inspired by immediate mode GUI." 6 6 categories = ["game-development", "data-structures"] ··· 35 35 "bevy_reflect", 36 36 ] } 37 37 bevy_reflect = { version = "0.17", default-features = false, optional = true } 38 - immediate_stats_macros = { path = "../immediate_stats_macros", version = "0.3.1", default-features = false } 38 + immediate_stats_macros = { path = "../immediate_stats_macros", version = "0.3.2", default-features = false } 39 39 40 40 [dev-dependencies] 41 41 bevy = { version = "0.17", default-features = false }
+21
immediate_stats/src/modifier.rs
··· 1 1 //! Contains a modifier that can be applied to [`Stat`](crate::Stat). 2 2 3 + use std::fmt::{Display, Formatter}; 3 4 use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; 4 5 5 6 /// Modifier values that can be [applied](super::Stat::apply) to a [`Stat`](super::Stat). ··· 39 40 Self { 40 41 multiplier, 41 42 ..Self::default() 43 + } 44 + } 45 + 46 + /// Returns a new modifier scaled by a fraction. 47 + /// 48 + /// When the scale is zero, the bonus will be zero while the multiplier will be one. 49 + pub fn scaled(&self, fraction: f32) -> Self { 50 + Self { 51 + bonus: (self.bonus as f32 * fraction) as i32, 52 + multiplier: (1.0 - fraction) * 1.0 + fraction * self.multiplier, 42 53 } 43 54 } 44 55 } ··· 79 90 self.multiplier /= rhs; 80 91 } 81 92 } 93 + 94 + impl Display for Modifier { 95 + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 96 + if let Some(precision) = f.precision() { 97 + write!(f, "(+{}) x {:.*}", self.bonus, precision, self.multiplier) 98 + } else { 99 + write!(f, "(+{}) x {}", self.bonus, self.multiplier) 100 + } 101 + } 102 + }
+18 -1
immediate_stats/src/stat.rs
··· 2 2 3 3 use crate::StatContainer; 4 4 use crate::modifier::Modifier; 5 + use std::fmt::{Display, Formatter}; 5 6 use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; 6 7 7 8 /// A stat that [resets][reset] to a base value every iteration. ··· 79 80 self.multiplier *= modifier.multiplier; 80 81 } 81 82 82 - /// Scales and applies the [`Modifier`] values to the bonus and multiplier. 83 + /// [Scales](Modifier::scaled) and applies the [`Modifier`] values to the bonus and multiplier. 84 + /// 85 + /// When the scale is zero, the bonus will be zero while the multiplier will be one. 83 86 /// 84 87 /// This adds the bonuses, and multiplies the multipliers. 85 88 pub fn apply_scaled(&mut self, modifier: Modifier, fraction: f32) { ··· 133 136 self.multiplier /= rhs; 134 137 } 135 138 } 139 + 140 + impl Display for Stat { 141 + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 142 + if let Some(precision) = f.precision() { 143 + write!( 144 + f, 145 + "({} + {}) x {:.*}", 146 + self.base, self.bonus, precision, self.multiplier 147 + ) 148 + } else { 149 + write!(f, "({} + {}) x {}", self.base, self.bonus, self.multiplier) 150 + } 151 + } 152 + }
+15
immediate_stats/tests/modifier.rs
··· 53 53 } 54 54 ); 55 55 } 56 + 57 + #[test] 58 + fn scaled() { 59 + let modifier = Modifier { 60 + bonus: 10, 61 + multiplier: 3.0, 62 + }; 63 + assert_eq!( 64 + modifier.scaled(0.5), 65 + Modifier { 66 + bonus: 5, 67 + multiplier: 2.0, 68 + } 69 + ); 70 + }
+1 -1
immediate_stats_macros/Cargo.toml
··· 1 1 [package] 2 2 name = "immediate_stats_macros" 3 - version = "0.3.1" 3 + version = "0.3.2" 4 4 edition = "2024" 5 5 description = "Game stats that reset every frame, inspired by immediate mode GUI." 6 6 categories = ["game-development", "data-structures"]