2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-01-12 16:43:25 +00:00
|
|
|
use specs::{Component, FlaggedStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2020-01-12 16:43:25 +00:00
|
|
|
|
2020-07-06 05:56:02 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
2020-01-12 16:43:25 +00:00
|
|
|
pub struct Energy {
|
|
|
|
current: u32,
|
|
|
|
maximum: u32,
|
2019-11-22 00:53:28 +00:00
|
|
|
pub regen_rate: f32,
|
2020-01-12 16:43:25 +00:00
|
|
|
pub last_change: Option<(i32, f64, EnergySource)>,
|
|
|
|
}
|
|
|
|
|
2020-07-06 05:56:02 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
2020-01-12 16:43:25 +00:00
|
|
|
pub enum EnergySource {
|
2020-03-17 14:01:41 +00:00
|
|
|
Ability,
|
2020-02-13 07:30:32 +00:00
|
|
|
Climb,
|
2020-01-12 16:43:25 +00:00
|
|
|
LevelUp,
|
2020-03-10 17:54:59 +00:00
|
|
|
HitEnemy,
|
2019-11-20 18:31:36 +00:00
|
|
|
Regen,
|
2020-01-18 14:00:59 +00:00
|
|
|
Revive,
|
2020-01-12 16:43:25 +00:00
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum StatChangeError {
|
|
|
|
Underflow,
|
|
|
|
Overflow,
|
|
|
|
}
|
|
|
|
|
2020-01-12 16:43:25 +00:00
|
|
|
impl Energy {
|
|
|
|
pub fn new(amount: u32) -> Energy {
|
|
|
|
Energy {
|
|
|
|
current: amount,
|
|
|
|
maximum: amount,
|
2019-11-22 00:53:28 +00:00
|
|
|
regen_rate: 0.0,
|
2020-01-12 16:43:25 +00:00
|
|
|
last_change: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn current(&self) -> u32 { self.current }
|
2020-01-12 16:43:25 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn maximum(&self) -> u32 { self.maximum }
|
2020-01-12 16:43:25 +00:00
|
|
|
|
|
|
|
pub fn set_to(&mut self, amount: u32, cause: EnergySource) {
|
|
|
|
let amount = amount.min(self.maximum);
|
|
|
|
self.last_change = Some((amount as i32 - self.current as i32, 0.0, cause));
|
|
|
|
self.current = amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_by(&mut self, amount: i32, cause: EnergySource) {
|
|
|
|
self.current = ((self.current as i32 + amount).max(0) as u32).min(self.maximum);
|
|
|
|
self.last_change = Some((amount, 0.0, cause));
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:53:28 +00:00
|
|
|
pub fn try_change_by(
|
|
|
|
&mut self,
|
|
|
|
amount: i32,
|
|
|
|
cause: EnergySource,
|
|
|
|
) -> Result<(), StatChangeError> {
|
|
|
|
if self.current as i32 + amount < 0 {
|
|
|
|
Err(StatChangeError::Underflow)
|
|
|
|
} else if self.current as i32 + amount > self.maximum as i32 {
|
|
|
|
Err(StatChangeError::Overflow)
|
|
|
|
} else {
|
|
|
|
self.change_by(amount, cause);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 16:43:25 +00:00
|
|
|
pub fn set_maximum(&mut self, amount: u32) {
|
|
|
|
self.maximum = amount;
|
|
|
|
self.current = self.current.min(self.maximum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Energy {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
2020-01-12 16:43:25 +00:00
|
|
|
}
|