2020-02-01 20:39:39 +00:00
|
|
|
use crate::{
|
|
|
|
comp,
|
2020-06-29 16:22:19 +00:00
|
|
|
comp::{body::humanoid::Species, skills::SkillSet, Body},
|
2020-02-01 20:39:39 +00:00
|
|
|
sync::Uid,
|
|
|
|
};
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-07-29 19:54:48 +00:00
|
|
|
use specs::{Component, FlaggedStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2020-06-29 16:22:19 +00:00
|
|
|
use std::{error::Error, fmt};
|
2019-05-13 13:58:01 +00:00
|
|
|
|
2019-10-17 20:59:36 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub struct HealthChange {
|
|
|
|
pub amount: i32,
|
|
|
|
pub cause: HealthSource,
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:52:58 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
2019-05-27 19:41:24 +00:00
|
|
|
pub enum HealthSource {
|
|
|
|
Attack { by: Uid }, // TODO: Implement weapon
|
2020-03-20 13:26:18 +00:00
|
|
|
Projectile { owner: Option<Uid> },
|
2019-05-27 19:41:24 +00:00
|
|
|
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-09-25 15:52:58 +00:00
|
|
|
Item,
|
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,
|
2019-10-17 20:59:36 +00:00
|
|
|
pub last_change: (f64, HealthChange),
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
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-05-13 13:58:01 +00:00
|
|
|
impl Health {
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn current(&self) -> u32 { self.current }
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn maximum(&self) -> u32 { 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);
|
2020-02-01 20:39:39 +00:00
|
|
|
self.last_change = (0.0, HealthChange {
|
|
|
|
amount: amount as i32 - self.current as i32,
|
|
|
|
cause,
|
|
|
|
});
|
2019-05-27 19:41:24 +00:00
|
|
|
self.current = amount;
|
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2019-10-17 20:59:36 +00:00
|
|
|
pub fn change_by(&mut self, change: HealthChange) {
|
|
|
|
self.current = ((self.current as i32 + change.amount).max(0) as u32).min(self.maximum);
|
|
|
|
self.last_change = (0.0, change);
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2020-01-20 16:42:35 +00:00
|
|
|
// This is private because max hp is based on the level
|
|
|
|
fn set_maximum(&mut self, amount: u32) {
|
2019-08-01 12:48:09 +00:00
|
|
|
self.maximum = amount;
|
|
|
|
self.current = self.current.min(self.maximum);
|
|
|
|
}
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
2019-11-22 00:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum StatChangeError {
|
|
|
|
Underflow,
|
|
|
|
Overflow,
|
|
|
|
}
|
2020-06-29 06:20:24 +00:00
|
|
|
|
2019-11-22 00:53:28 +00:00
|
|
|
impl fmt::Display for StatChangeError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-02-01 20:39:39 +00:00
|
|
|
write!(f, "{}", match self {
|
|
|
|
Self::Underflow => "insufficient stat quantity",
|
|
|
|
Self::Overflow => "stat quantity would overflow",
|
|
|
|
})
|
2019-11-22 00:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Error for StatChangeError {}
|
2019-05-13 13:58:01 +00:00
|
|
|
|
2019-07-26 14:23:33 +00:00
|
|
|
impl Exp {
|
2020-05-14 03:48:19 +00:00
|
|
|
/// Used to determine how much exp is required to reach the next level. When
|
|
|
|
/// a character levels up, the next level target is increased by this value
|
|
|
|
const EXP_INCREASE_FACTOR: u32 = 25;
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn current(&self) -> u32 { self.current }
|
2019-07-26 14:23:33 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn maximum(&self) -> u32 { self.maximum }
|
2019-07-26 14:23:33 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn set_current(&mut self, current: u32) { self.current = current; }
|
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
|
|
|
}
|
2020-05-14 03:48:19 +00:00
|
|
|
|
|
|
|
pub fn update_maximum(&mut self, level: u32) {
|
2020-06-18 19:45:37 +00:00
|
|
|
self.maximum = level
|
|
|
|
.saturating_mul(Self::EXP_INCREASE_FACTOR)
|
|
|
|
.saturating_add(Self::EXP_INCREASE_FACTOR);
|
2020-05-14 03:48:19 +00:00
|
|
|
}
|
2019-07-26 14:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn set_level(&mut self, level: u32) { self.amount = level; }
|
2019-07-26 14:23:33 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn level(&self) -> u32 { self.amount }
|
2019-07-26 14:23:33 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn change_by(&mut self, level: u32) { 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-07-26 14:23:33 +00:00
|
|
|
pub level: Level,
|
|
|
|
pub exp: Exp,
|
2020-06-29 06:20:24 +00:00
|
|
|
pub skill_set: SkillSet,
|
2020-01-13 22:49:46 +00:00
|
|
|
pub endurance: u32,
|
|
|
|
pub fitness: u32,
|
|
|
|
pub willpower: 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 {
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn should_die(&self) -> bool { self.health.current == 0 }
|
|
|
|
|
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-10-04 16:07:26 +00:00
|
|
|
|
2019-10-04 16:09:19 +00:00
|
|
|
// TODO: Delete this once stat points will be a thing
|
2020-03-26 13:46:08 +00:00
|
|
|
pub fn update_max_hp(&mut self) { self.health.set_maximum(52 + 3 * self.level.amount); }
|
2019-05-25 21:13:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 11:48:28 +00:00
|
|
|
impl Stats {
|
2020-03-15 14:27:06 +00:00
|
|
|
pub fn new(name: String, body: Body) -> Self {
|
2020-05-29 18:23:00 +00:00
|
|
|
let species = if let comp::Body::Humanoid(hbody) = body {
|
|
|
|
Some(hbody.species)
|
2020-01-13 22:49:46 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-05-29 18:23:00 +00:00
|
|
|
// TODO: define base stats somewhere else (maybe method on Body?)
|
|
|
|
let (endurance, fitness, willpower) = match species {
|
|
|
|
Some(Species::Danari) => (0, 2, 3), // Small, flexible, intelligent, physically weak
|
|
|
|
Some(Species::Dwarf) => (2, 2, 1), // phyiscally strong, intelligent, slow reflexes
|
|
|
|
Some(Species::Elf) => (1, 2, 2), // Intelligent, quick, physically weak
|
|
|
|
Some(Species::Human) => (2, 1, 2), // Perfectly balanced
|
|
|
|
Some(Species::Orc) => (3, 2, 0), /* Physically strong, non intelligent, medium */
|
|
|
|
// reflexes
|
|
|
|
Some(Species::Undead) => (1, 3, 1), /* Very good reflexes, equally intelligent and */
|
|
|
|
// strong
|
2020-01-13 22:49:46 +00:00
|
|
|
None => (0, 0, 0),
|
|
|
|
};
|
|
|
|
|
2019-10-08 16:12:08 +00:00
|
|
|
let mut stats = Self {
|
2019-06-30 11:48:28 +00:00
|
|
|
name,
|
|
|
|
health: Health {
|
2019-10-08 16:12:08 +00:00
|
|
|
current: 0,
|
|
|
|
maximum: 0,
|
2020-02-01 20:39:39 +00:00
|
|
|
last_change: (0.0, HealthChange {
|
|
|
|
amount: 0,
|
|
|
|
cause: HealthSource::Revive,
|
|
|
|
}),
|
2019-05-13 13:58:01 +00:00
|
|
|
},
|
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
|
|
|
},
|
2020-06-29 06:20:24 +00:00
|
|
|
skill_set: SkillSet::default(),
|
2020-01-13 22:49:46 +00:00
|
|
|
endurance,
|
|
|
|
fitness,
|
|
|
|
willpower,
|
2019-05-27 17:45:43 +00:00
|
|
|
is_dead: false,
|
2019-10-08 16:12:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
stats.update_max_hp();
|
|
|
|
stats
|
|
|
|
.health
|
|
|
|
.set_to(stats.health.maximum(), HealthSource::Revive);
|
|
|
|
|
|
|
|
stats
|
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-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Stats {
|
2020-07-06 05:56:02 +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 {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|