veloren/common/src/comp/combo.rs

42 lines
990 B
Rust
Raw Normal View History

2021-02-27 19:55:06 +00:00
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage};
use specs_idvs::IdvStorage;
2021-03-20 20:44:18 +00:00
pub const COMBO_DECAY_START: f64 = 7.5; // seconds
2021-03-04 20:43:58 +00:00
2021-02-27 19:55:06 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Combo {
counter: u32,
last_increase: f64,
}
impl Default for Combo {
fn default() -> Self {
Self {
counter: 0,
last_increase: 0.0,
}
}
}
impl Combo {
pub fn counter(&self) -> u32 { self.counter }
pub fn last_increase(&self) -> f64 { self.last_increase }
pub fn reset(&mut self) { self.counter = 0; }
2021-03-04 20:43:58 +00:00
pub fn change_by(&mut self, amount: i32, time: f64) {
2021-08-12 10:34:04 +00:00
self.counter = if amount > 0 {
self.counter.saturating_add(amount as u32)
2021-03-04 20:43:58 +00:00
} else {
2021-08-12 10:34:04 +00:00
self.counter.saturating_sub(amount.abs() as u32)
};
2021-02-27 19:55:06 +00:00
self.last_increase = time;
}
}
impl Component for Combo {
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
}