2019-06-06 14:48:41 +00:00
|
|
|
use crate::state::Uid;
|
|
|
|
use specs::{Component, FlaggedStorage, VecStorage};
|
2019-05-13 13:58:01 +00:00
|
|
|
|
2019-05-27 19:41:24 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum HealthSource {
|
|
|
|
Attack { by: Uid }, // TODO: Implement weapon
|
|
|
|
Suicide,
|
2019-06-06 14:48:41 +00:00
|
|
|
Unknown,
|
2019-05-27 19:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 22:00:38 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
2019-05-13 13:58:01 +00:00
|
|
|
pub struct Health {
|
2019-05-27 19:41:24 +00:00
|
|
|
current: u32,
|
|
|
|
maximum: u32,
|
|
|
|
pub last_change: Option<(i32, f64, HealthSource)>,
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Health {
|
2019-05-27 19:41:24 +00:00
|
|
|
pub fn get_current(&self) -> u32 {
|
|
|
|
self.current
|
|
|
|
}
|
|
|
|
pub fn get_maximum(&self) -> u32 {
|
|
|
|
self.maximum
|
|
|
|
}
|
|
|
|
pub fn set_to(&mut self, amount: u32, cause: HealthSource) {
|
|
|
|
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: HealthSource) {
|
2019-05-13 13:58:01 +00:00
|
|
|
self.current = (self.current as i32 + amount).max(0) as u32;
|
2019-05-27 19:41:24 +00:00
|
|
|
self.last_change = Some((amount, 0.0, cause));
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 22:00:38 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
2019-05-13 13:58:01 +00:00
|
|
|
pub struct Stats {
|
|
|
|
pub hp: Health,
|
|
|
|
pub xp: u32,
|
2019-05-27 17:45:43 +00:00
|
|
|
pub is_dead: bool,
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 21:13:38 +00:00
|
|
|
impl Stats {
|
2019-05-27 17:45:43 +00:00
|
|
|
pub fn should_die(&self) -> bool {
|
|
|
|
// TODO: Remove
|
2019-05-25 21:13:38 +00:00
|
|
|
self.hp.current == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 13:58:01 +00:00
|
|
|
impl Default for Stats {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
hp: Health {
|
|
|
|
current: 100,
|
|
|
|
maximum: 100,
|
|
|
|
last_change: None,
|
|
|
|
},
|
|
|
|
xp: 0,
|
2019-05-27 17:45:43 +00:00
|
|
|
is_dead: false,
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Stats {
|
|
|
|
type Storage = FlaggedStorage<Self, VecStorage<Self>>;
|
|
|
|
}
|
2019-05-17 20:47:58 +00:00
|
|
|
|
2019-05-27 19:41:24 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Dying {
|
|
|
|
pub cause: HealthSource,
|
|
|
|
}
|
2019-05-17 20:47:58 +00:00
|
|
|
|
|
|
|
impl Component for Dying {
|
2019-05-27 19:41:24 +00:00
|
|
|
type Storage = VecStorage<Self>;
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|