2019-08-27 19:56:46 +00:00
|
|
|
use crate::{comp, state::Uid};
|
2019-07-29 19:54:48 +00:00
|
|
|
use specs::{Component, FlaggedStorage};
|
|
|
|
use specs_idvs::IDVStorage;
|
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-08-07 02:26:24 +00:00
|
|
|
World,
|
2019-06-30 11:48:28 +00:00
|
|
|
Revive,
|
2019-07-01 20:07:30 +00:00
|
|
|
Command,
|
2019-08-01 12:48:09 +00:00
|
|
|
LevelUp,
|
2019-06-06 14:48:41 +00:00
|
|
|
Unknown,
|
2019-05-27 19:41:24 +00:00
|
|
|
}
|
2019-08-18 18:07:21 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum EnergySource {
|
|
|
|
CastSpell,
|
|
|
|
LevelUp,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-18 18:07:21 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Energy {
|
|
|
|
current: u32,
|
|
|
|
maximum: u32,
|
|
|
|
pub last_change: Option<(i32, f64, EnergySource)>,
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:23:33 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Exp {
|
2019-08-27 22:33:14 +00:00
|
|
|
current: u32,
|
|
|
|
maximum: u32,
|
2019-07-26 14:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Level {
|
|
|
|
amount: u32,
|
|
|
|
}
|
|
|
|
|
2019-08-27 19:56:46 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Equipment {
|
|
|
|
pub main: Option<comp::Item>,
|
|
|
|
pub alt: Option<comp::Item>,
|
|
|
|
// TODO: Armor
|
|
|
|
}
|
|
|
|
|
2019-05-13 13:58:01 +00:00
|
|
|
impl Health {
|
2019-08-01 12:48:09 +00:00
|
|
|
pub fn current(&self) -> u32 {
|
2019-05-27 19:41:24 +00:00
|
|
|
self.current
|
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2019-08-01 12:48:09 +00:00
|
|
|
pub fn maximum(&self) -> u32 {
|
2019-05-27 19:41:24 +00:00
|
|
|
self.maximum
|
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2019-05-27 19:41:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2019-05-27 19:41:24 +00:00
|
|
|
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-08-02 18:56:37 +00:00
|
|
|
|
2019-08-01 12:48:09 +00:00
|
|
|
pub fn set_maximum(&mut self, amount: u32) {
|
|
|
|
self.maximum = amount;
|
|
|
|
self.current = self.current.min(self.maximum);
|
|
|
|
}
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 18:07:21 +00:00
|
|
|
impl Energy {
|
|
|
|
pub fn current(&self) -> u32 {
|
|
|
|
self.current
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maximum(&self) -> u32 {
|
|
|
|
self.maximum
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_to(&mut self, amount: u32, cause: EnergySource) {
|
|
|
|
let amount = amount.min(self.maximum);
|
|
|
|
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: EnergySource) {
|
|
|
|
self.current = ((self.current as i32 + amount).max(0) as u32).min(self.maximum);
|
|
|
|
self.last_change = Some((amount, 0.0, cause));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_maximum(&mut self, amount: u32) {
|
|
|
|
self.maximum = amount;
|
|
|
|
self.current = self.current.min(self.maximum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:23:33 +00:00
|
|
|
impl Exp {
|
2019-08-27 22:33:14 +00:00
|
|
|
pub fn current(&self) -> u32 {
|
2019-07-26 14:23:33 +00:00
|
|
|
self.current
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:33:14 +00:00
|
|
|
pub fn maximum(&self) -> u32 {
|
2019-07-26 14:23:33 +00:00
|
|
|
self.maximum
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:33:14 +00:00
|
|
|
pub fn set_current(&mut self, current: u32) {
|
2019-07-26 18:40:29 +00:00
|
|
|
self.current = current;
|
2019-07-26 14:23:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 18:40:29 +00:00
|
|
|
// TODO: Uncomment when needed
|
2019-08-27 22:33:14 +00:00
|
|
|
// pub fn set_maximum(&mut self, maximum: u32) {
|
2019-07-26 18:40:29 +00:00
|
|
|
// self.maximum = maximum;
|
|
|
|
// }
|
2019-07-26 14:23:33 +00:00
|
|
|
|
2019-08-27 22:33:14 +00:00
|
|
|
pub fn change_by(&mut self, current: i64) {
|
|
|
|
self.current = ((self.current as i64) + current) as u32;
|
2019-07-26 14:23:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 22:33:14 +00:00
|
|
|
pub fn change_maximum_by(&mut self, maximum: i64) {
|
|
|
|
self.maximum = ((self.maximum as i64) + maximum) as u32;
|
2019-07-26 14:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
2019-07-26 18:40:29 +00:00
|
|
|
// TODO: Uncomment when needed
|
|
|
|
// pub fn set_level(&mut self, level: u32) {
|
|
|
|
// self.amount = level;
|
|
|
|
// }
|
2019-07-26 14:23:33 +00:00
|
|
|
|
2019-08-03 19:30:01 +00:00
|
|
|
pub fn level(&self) -> u32 {
|
2019-07-26 14:23:33 +00:00
|
|
|
self.amount
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_by(&mut self, level: u32) {
|
2019-07-26 18:40:29 +00:00
|
|
|
self.amount = self.amount + level;
|
2019-07-26 14:23:33 +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-08-18 18:07:21 +00:00
|
|
|
pub energy: Energy,
|
2019-07-26 14:23:33 +00:00
|
|
|
pub level: Level,
|
|
|
|
pub exp: Exp,
|
2019-08-27 19:56:46 +00:00
|
|
|
pub equipment: Equipment,
|
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 {
|
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
|
2019-08-01 12:48:09 +00:00
|
|
|
.set_to(self.health.maximum(), HealthSource::Revive);
|
2019-06-30 20:25:37 +00:00
|
|
|
self.is_dead = false;
|
|
|
|
}
|
2019-05-25 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 11:48:28 +00:00
|
|
|
impl Stats {
|
2019-08-27 19:56:46 +00:00
|
|
|
pub fn new(name: String, main: Option<comp::Item>) -> 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,
|
|
|
|
},
|
2019-07-26 14:23:33 +00:00
|
|
|
level: Level { amount: 1 },
|
|
|
|
exp: Exp {
|
2019-08-27 22:33:14 +00:00
|
|
|
current: 0,
|
|
|
|
maximum: 50,
|
2019-07-26 14:23:33 +00:00
|
|
|
},
|
2019-08-18 18:07:21 +00:00
|
|
|
energy: Energy {
|
|
|
|
current: 200,
|
|
|
|
maximum: 200,
|
|
|
|
last_change: None,
|
|
|
|
},
|
2019-08-27 19:56:46 +00:00
|
|
|
equipment: Equipment {
|
|
|
|
main: main,
|
|
|
|
alt: None,
|
|
|
|
},
|
2019-05-27 17:45:43 +00:00
|
|
|
is_dead: false,
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:49:48 +00:00
|
|
|
|
|
|
|
pub fn with_max_health(mut self, amount: u32) -> Self {
|
|
|
|
self.health.maximum = amount;
|
|
|
|
self.health.current = amount;
|
|
|
|
self
|
|
|
|
}
|
2019-08-18 18:07:21 +00:00
|
|
|
|
|
|
|
pub fn with_max_energy(mut self, amount: u32) -> Self {
|
|
|
|
self.energy.maximum = amount;
|
|
|
|
self.energy.current = amount;
|
|
|
|
self
|
|
|
|
}
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Stats {
|
2019-07-29 19:54:48 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
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-07-29 19:54:48 +00:00
|
|
|
type Storage = IDVStorage<Self>;
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|