1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage, VecStorage};

pub const COMBO_DECAY_START: f64 = 7.5; // seconds

#[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; }

    pub fn change_by(&mut self, amount: i32, time: f64) {
        self.counter = if amount > 0 {
            self.counter.saturating_add(amount as u32)
        } else {
            self.counter.saturating_sub(amount.unsigned_abs())
        };
        self.last_increase = time;
    }
}

impl Component for Combo {
    type Storage = DerefFlaggedStorage<Self, VecStorage<Self>>;
}