veloren/common/src/comp/stats.rs

242 lines
5.7 KiB
Rust
Raw Normal View History

use crate::comp::{body::humanoid::Race, Body};
2019-11-24 20:12:03 +00:00
use crate::{comp, sync::Uid};
use specs::{Component, FlaggedStorage};
use specs_idvs::IDVStorage;
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct HealthChange {
pub amount: i32,
pub cause: HealthSource,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthSource {
Attack { by: Uid }, // TODO: Implement weapon
Suicide,
World,
2019-06-30 11:48:28 +00:00
Revive,
2019-07-01 20:07:30 +00:00
Command,
LevelUp,
Item,
Unknown,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Health {
current: u32,
maximum: u32,
pub last_change: (f64, HealthChange),
}
2019-07-26 14:23:33 +00:00
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Exp {
current: u32,
maximum: u32,
2019-07-26 14:23:33 +00:00
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Level {
amount: u32,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct Equipment {
pub main: Option<comp::Item>,
pub alt: Option<comp::Item>,
// TODO: Armor
}
impl Health {
pub fn current(&self) -> u32 {
self.current
}
pub fn 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);
self.last_change = (
0.0,
HealthChange {
amount: amount as i32 - self.current as i32,
cause,
},
);
self.current = amount;
}
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);
}
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) {
self.maximum = amount;
self.current = self.current.min(self.maximum);
}
}
#[derive(Debug)]
pub enum StatChangeError {
Underflow,
Overflow,
}
use std::error::Error;
use std::fmt;
impl fmt::Display for StatChangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Underflow => "insufficient stat quantity",
Self::Overflow => "stat quantity would overflow",
}
)
}
}
impl Error for StatChangeError {}
2019-07-26 14:23:33 +00:00
impl Exp {
pub fn current(&self) -> u32 {
2019-07-26 14:23:33 +00:00
self.current
}
pub fn maximum(&self) -> u32 {
2019-07-26 14:23:33 +00:00
self.maximum
}
pub fn set_current(&mut self, current: u32) {
self.current = current;
2019-07-26 14:23:33 +00:00
}
// TODO: Uncomment when needed
// pub fn set_maximum(&mut self, maximum: u32) {
// self.maximum = maximum;
// }
2019-07-26 14:23:33 +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
}
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 {
pub fn set_level(&mut self, level: u32) {
self.amount = level;
}
2019-07-26 14:23:33 +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) {
self.amount += level;
2019-07-26 14:23:33 +00:00
}
}
2019-06-30 11:48:28 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
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,
pub equipment: Equipment,
pub endurance: u32,
pub fitness: u32,
pub willpower: u32,
pub is_dead: bool,
}
impl Stats {
pub fn should_die(&self) -> bool {
2019-06-30 11:48:28 +00:00
self.health.current == 0
}
2019-06-30 20:25:37 +00:00
pub fn revive(&mut self) {
self.health
.set_to(self.health.maximum(), HealthSource::Revive);
2019-06-30 20:25:37 +00:00
self.is_dead = false;
}
// TODO: Delete this once stat points will be a thing
2019-10-08 16:12:08 +00:00
pub fn update_max_hp(&mut self) {
2020-01-20 16:42:35 +00:00
self.health.set_maximum(27 + 15 * self.level.amount);
}
}
2019-06-30 11:48:28 +00:00
impl Stats {
pub fn new(name: String, body: Body, main: Option<comp::Item>) -> Self {
let race = if let comp::Body::Humanoid(hbody) = body {
Some(hbody.race)
} else {
None
};
let (endurance, fitness, willpower) = match race {
Some(Race::Danari) => (0, 1, 2),
Some(Race::Dwarf) => (1, 2, 0),
Some(Race::Elf) => (2, 1, 0),
Some(Race::Human) => (1, 0, 2),
Some(Race::Orc) => (0, 2, 1),
Some(Race::Undead) => (2, 0, 1),
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,
last_change: (
0.0,
HealthChange {
amount: 0,
cause: HealthSource::Revive,
},
),
},
2019-07-26 14:23:33 +00:00
level: Level { amount: 1 },
exp: Exp {
current: 0,
maximum: 50,
2019-07-26 14:23:33 +00:00
},
equipment: Equipment { main, alt: None },
endurance,
fitness,
willpower,
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-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
}
}
impl Component for Stats {
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Dying {
pub cause: HealthSource,
}
impl Component for Dying {
type Storage = IDVStorage<Self>;
}