diff --git a/immediate_stats/src/modifier.rs b/immediate_stats/src/modifier.rs index 3037230..fe10a50 100644 --- a/immediate_stats/src/modifier.rs +++ b/immediate_stats/src/modifier.rs @@ -42,6 +42,16 @@ impl Modifier { ..Self::default() } } + + /// Returns a new modifier scaled by a fraction. + /// + /// When the scale is zero, the bonus will be zero while the multiplier will be one. + pub fn scaled(&self, fraction: f32) -> Self { + Self { + bonus: (self.bonus as f32 * fraction) as i32, + multiplier: (1.0 - fraction) * 1.0 + fraction * self.multiplier, + } + } } impl Default for Modifier { diff --git a/immediate_stats/src/stat.rs b/immediate_stats/src/stat.rs index d522d30..840dfab 100644 --- a/immediate_stats/src/stat.rs +++ b/immediate_stats/src/stat.rs @@ -80,7 +80,9 @@ impl Stat { self.multiplier *= modifier.multiplier; } - /// Scales and applies the [`Modifier`] values to the bonus and multiplier. + /// [Scales](Modifier::scaled) and applies the [`Modifier`] values to the bonus and multiplier. + /// + /// When the scale is zero, the bonus will be zero while the multiplier will be one. /// /// This adds the bonuses, and multiplies the multipliers. pub fn apply_scaled(&mut self, modifier: Modifier, fraction: f32) { diff --git a/immediate_stats/tests/modifier.rs b/immediate_stats/tests/modifier.rs index 0e2a435..7b6328d 100644 --- a/immediate_stats/tests/modifier.rs +++ b/immediate_stats/tests/modifier.rs @@ -53,3 +53,18 @@ fn divide() { } ); } + +#[test] +fn scaled() { + let modifier = Modifier { + bonus: 10, + multiplier: 3.0, + }; + assert_eq!( + modifier.scaled(0.5), + Modifier { + bonus: 5, + multiplier: 2.0, + } + ); +}