diff --git a/common/src/comp/energy.rs b/common/src/comp/energy.rs index 8629fa4bd5..2d7751cdb4 100644 --- a/common/src/comp/energy.rs +++ b/common/src/comp/energy.rs @@ -22,7 +22,8 @@ pub struct Energy { /// Maximum is the amount of energy the entity has after temporary modifiers /// are considered maximum: u32, - pub regen_rate: f32, + /// Rate of regeneration per tick. Starts at zero and accelerates. + regen_rate: f32, } impl Energy { @@ -94,6 +95,24 @@ impl Energy { } } + /// Returns `true` if the current value is less than the maximum + pub fn needs_regen(&self) -> bool { self.current < self.maximum } + + /// Regenerates energy based on the provided acceleration + pub fn regen(&mut self, accel: f32, dt: f32) { + if self.current < self.maximum { + self.change_by(self.regen_rate * dt); + self.regen_rate = (self.regen_rate + accel * dt).min(10.0); + } + } + + /// Checks whether the `regen_rate` is zero or not. Returns true if the + /// value is anything other than `0.0`. + pub fn needs_regen_rate_reset(&self) -> bool { self.regen_rate != 0.0 } + + /// Resets the energy regeneration rate to zero + pub fn reset_regen_rate(&mut self) { self.regen_rate = 0.0 } + pub fn change_by(&mut self, change: f32) { self.current = (((self.current() + change).clamp(0.0, f32::from(Self::MAX_ENERGY)) * Self::SCALING_FACTOR_FLOAT) as u32) diff --git a/common/src/comp/poise.rs b/common/src/comp/poise.rs index 5b016d9d4e..cfeb9fedb0 100644 --- a/common/src/comp/poise.rs +++ b/common/src/comp/poise.rs @@ -52,8 +52,8 @@ pub struct Poise { maximum: u32, /// Direction that the last poise change came from pub last_change: Dir, - /// Rate of poise regeneration per tick. Starts at zero and accelerates. - pub regen_rate: f32, + /// Rate of regeneration per tick. Starts at zero and accelerates. + regen_rate: f32, /// Time that entity was last in a poise state last_stun_time: Option