aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/health.rs
blob: f9cc300ae7ccd67548b96f59e5c0016f1d451d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use crate::shared::*;

#[derive(Component, Message, Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
pub struct Health {
    pub health: f32,
    pub shield: f32,
}

impl Health {
    pub fn apply_damage(&mut self, damage: f32) {
        let shield_damage = damage.min(self.shield);
        let health_damage = damage - shield_damage;
        self.shield = self.shield - shield_damage;
        self.health = (self.health - health_damage).max(0.);
    }

    pub fn heal(&mut self, health: f32, max_health: f32) {
        self.health = (self.health + health).min(max_health);
    }
}