2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-01-07 20:25:12 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage};
|
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-11-22 00:53:28 +00:00
|
|
|
#[derive(Debug)]
|
2021-03-12 11:10:42 +00:00
|
|
|
#[allow(dead_code)] // TODO: remove once trade sim hits master
|
2019-11-22 00:53:28 +00:00
|
|
|
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-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,
|
2021-02-28 20:02:03 +00:00
|
|
|
pub damage_reduction: f32,
|
2021-03-20 17:29:57 +00:00
|
|
|
pub max_health_modifier: f32,
|
2021-04-16 17:44:11 +00:00
|
|
|
pub move_speed_modifier: f32,
|
2021-05-30 16:40:11 +00:00
|
|
|
pub attack_speed_modifier: f32,
|
2020-07-09 00:04:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 11:48:28 +00:00
|
|
|
impl Stats {
|
2021-01-22 21:12:16 +00:00
|
|
|
pub fn new(name: String) -> Self {
|
2020-10-31 22:34:08 +00:00
|
|
|
Self {
|
2019-06-30 11:48:28 +00:00
|
|
|
name,
|
2021-02-28 20:02:03 +00:00
|
|
|
damage_reduction: 0.0,
|
2021-03-20 17:29:57 +00:00
|
|
|
max_health_modifier: 1.0,
|
2021-04-16 17:44:11 +00:00
|
|
|
move_speed_modifier: 1.0,
|
2021-05-30 16:40:11 +00:00
|
|
|
attack_speed_modifier: 1.0,
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
2019-08-02 19:49:48 +00:00
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
/// Creates an empty `Stats` instance - used during character loading from
|
|
|
|
/// the database
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "".to_owned(),
|
2021-02-28 20:02:03 +00:00
|
|
|
damage_reduction: 0.0,
|
2021-03-20 17:29:57 +00:00
|
|
|
max_health_modifier: 1.0,
|
2021-04-16 17:44:11 +00:00
|
|
|
move_speed_modifier: 1.0,
|
2021-05-30 16:40:11 +00:00
|
|
|
attack_speed_modifier: 1.0,
|
2020-09-17 23:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 17:29:57 +00:00
|
|
|
|
|
|
|
/// Resets temporary modifiers to default values
|
|
|
|
pub fn reset_temp_modifiers(&mut self) {
|
|
|
|
self.damage_reduction = 0.0;
|
|
|
|
self.max_health_modifier = 1.0;
|
2021-04-16 17:44:11 +00:00
|
|
|
self.move_speed_modifier = 1.0;
|
2021-05-30 16:40:11 +00:00
|
|
|
self.attack_speed_modifier = 1.0;
|
2021-03-20 17:29:57 +00:00
|
|
|
}
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Stats {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|