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-30 11:48:28 +00:00
|
|
|
Revive,
|
2019-07-01 20:07:30 +00:00
|
|
|
Command,
|
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) {
|
2019-06-30 11:48:28 +00:00
|
|
|
let amount = amount.min(self.maximum);
|
2019-05-27 19:41:24 +00:00
|
|
|
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-06-30 11:48:28 +00:00
|
|
|
self.current = ((self.current as i32 + amount).max(0) as u32).min(self.maximum);
|
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-06-30 11:48:28 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-05-13 13:58:01 +00:00
|
|
|
pub struct Stats {
|
2019-06-30 11:48:28 +00:00
|
|
|
pub name: String,
|
|
|
|
pub health: Health,
|
2019-05-13 13:58:01 +00:00
|
|
|
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-06-30 11:48:28 +00:00
|
|
|
self.health.current == 0
|
2019-05-25 21:13:38 +00:00
|
|
|
}
|
2019-06-30 20:25:37 +00:00
|
|
|
pub fn revive(&mut self) {
|
|
|
|
self.health
|
|
|
|
.set_to(self.health.get_maximum(), HealthSource::Revive);
|
|
|
|
self.is_dead = false;
|
|
|
|
}
|
2019-05-25 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 11:48:28 +00:00
|
|
|
impl Stats {
|
|
|
|
pub fn new(name: String) -> Self {
|
2019-05-13 13:58:01 +00:00
|
|
|
Self {
|
2019-06-30 11:48:28 +00:00
|
|
|
name,
|
|
|
|
health: Health {
|
2019-05-13 13:58:01 +00:00
|
|
|
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
|
|
|
}
|