mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
c212de00c2
- replace serde_derive by feature of serde incl. source code modifications to compile - reduce futures-timer to "2.0" to be same as async_std - update notify - removed mio, bincode and lz4 compress in common as networking is now in own crate btw there is a better lz4 compress crate, which is newer than 2017 - update prometheus to 0.9 - can't update uvth yet due to usues - hashbrown to 7.2 to only need a single version - libsqlite3 update - image didn't change as there is a problem with `image 0.23` - switch old directories with newer directories-next - no num upgrade as we still depend on num 0.2 anyways - rodio and cpal upgrade - const-tewaker update - dispatch (untested) update - git2 update - iterations update
79 lines
1.9 KiB
Rust
79 lines
1.9 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use specs::{Component, FlaggedStorage};
|
|
use specs_idvs::IdvStorage;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
|
pub struct Energy {
|
|
current: u32,
|
|
maximum: u32,
|
|
pub regen_rate: f32,
|
|
pub last_change: Option<(i32, f64, EnergySource)>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
|
pub enum EnergySource {
|
|
Ability,
|
|
Climb,
|
|
LevelUp,
|
|
HitEnemy,
|
|
Regen,
|
|
Revive,
|
|
Unknown,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum StatChangeError {
|
|
Underflow,
|
|
Overflow,
|
|
}
|
|
|
|
impl Energy {
|
|
pub fn new(amount: u32) -> Energy {
|
|
Energy {
|
|
current: amount,
|
|
maximum: amount,
|
|
regen_rate: 0.0,
|
|
last_change: None,
|
|
}
|
|
}
|
|
|
|
pub fn current(&self) -> u32 { self.current }
|
|
|
|
pub fn maximum(&self) -> u32 { self.maximum }
|
|
|
|
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));
|
|
}
|
|
|
|
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(())
|
|
}
|
|
}
|
|
|
|
pub fn set_maximum(&mut self, amount: u32) {
|
|
self.maximum = amount;
|
|
self.current = self.current.min(self.maximum);
|
|
}
|
|
}
|
|
|
|
impl Component for Energy {
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
|
}
|