2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-09-09 04:07:17 +00:00
|
|
|
use crate::comp;
|
2021-02-17 13:03:20 +00:00
|
|
|
use crate::{uid::Uid, DamageSource};
|
2020-10-31 22:34:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-02-17 13:03:20 +00:00
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-07 20:25:12 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage};
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-31 22:34:08 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
|
|
|
|
|
|
|
/// Specifies what and how much changed current health
|
2021-09-09 04:07:17 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
2020-10-31 22:34:08 +00:00
|
|
|
pub struct HealthChange {
|
2021-09-09 04:07:17 +00:00
|
|
|
pub amount: f32,
|
|
|
|
pub by: Option<Uid>,
|
|
|
|
pub cause: Option<DamageSource>,
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
impl HealthChange {
|
|
|
|
pub fn damage_by(&self) -> Option<Uid> { self.cause.is_some().then_some(self.by).flatten() }
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
/// Health is represented by u32s within the module, but treated as a float by
|
|
|
|
/// the rest of the game.
|
|
|
|
// As a general rule, all input and output values to public functions should be
|
|
|
|
// floats rather than integers.
|
2020-10-31 22:34:08 +00:00
|
|
|
pub struct Health {
|
2021-09-09 04:07:17 +00:00
|
|
|
// Current and base_max are scaled by 256 within this module compared to what is visible to
|
2021-09-10 19:20:14 +00:00
|
|
|
// outside this module. The scaling is done to allow health to function as a fixed point while
|
|
|
|
// still having the advantages of being an integer. The scaling of 256 was chosen so that max
|
|
|
|
// health could be u16::MAX - 1, and then the scaled health could fit inside an f32 with no
|
|
|
|
// precision loss
|
2021-09-23 11:36:03 +00:00
|
|
|
/// Current health is how much health the entity currently has. Current
|
|
|
|
/// health *must* be lower than or equal to maximum health.
|
2020-10-31 22:34:08 +00:00
|
|
|
current: u32,
|
2021-09-10 19:20:14 +00:00
|
|
|
/// Base max is the amount of health the entity has without considering
|
|
|
|
/// temporary modifiers such as buffs
|
2021-03-20 17:29:57 +00:00
|
|
|
base_max: u32,
|
2021-09-10 19:20:14 +00:00
|
|
|
/// Maximum is the amount of health the entity has after temporary modifiers
|
|
|
|
/// are considered
|
2020-10-31 22:34:08 +00:00
|
|
|
maximum: u32,
|
2021-09-09 04:07:17 +00:00
|
|
|
// Time since last change and what the last change was
|
|
|
|
// TODO: Remove the time since last change, either convert to time of last change or just emit
|
|
|
|
// an outcome where appropriate. Is currently just used for frontend.
|
2020-10-31 22:34:08 +00:00
|
|
|
pub last_change: (f64, HealthChange),
|
|
|
|
pub is_dead: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Health {
|
2021-09-10 19:20:14 +00:00
|
|
|
/// Used when comparisons to health are needed outside this module.
|
|
|
|
// This value is chosen as anything smaller than this is more precise than our
|
|
|
|
// units of health.
|
|
|
|
pub const HEALTH_EPSILON: f32 = 0.5 / Self::MAX_SCALED_HEALTH as f32;
|
|
|
|
/// Maximum value allowed for health before scaling
|
|
|
|
const MAX_HEALTH: u16 = u16::MAX - 1;
|
2021-09-09 04:07:17 +00:00
|
|
|
/// The maximum value allowed for current and maximum health
|
2021-09-10 19:20:14 +00:00
|
|
|
/// Maximum value is (u16:MAX - 1) * 256, which only requires 24 bits. This
|
2021-09-09 04:07:17 +00:00
|
|
|
/// can fit into an f32 with no loss to precision
|
2021-09-10 19:20:14 +00:00
|
|
|
// Cast to u32 done as u32::from cannot be called inside constant
|
|
|
|
const MAX_SCALED_HEALTH: u32 = Self::MAX_HEALTH as u32 * Self::SCALING_FACTOR_INT;
|
2021-09-09 04:07:17 +00:00
|
|
|
/// The amount health is scaled by within this module
|
|
|
|
const SCALING_FACTOR_FLOAT: f32 = 256.;
|
2021-09-10 19:20:14 +00:00
|
|
|
const SCALING_FACTOR_INT: u32 = Self::SCALING_FACTOR_FLOAT as u32;
|
2021-09-09 04:07:17 +00:00
|
|
|
|
|
|
|
/// Returns the current value of health casted to a float
|
|
|
|
pub fn current(&self) -> f32 { self.current as f32 / Self::SCALING_FACTOR_FLOAT }
|
2020-10-31 22:34:08 +00:00
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
/// Returns the base maximum value of health casted to a float
|
|
|
|
pub fn base_max(&self) -> f32 { self.base_max as f32 / Self::SCALING_FACTOR_FLOAT }
|
2020-10-31 22:34:08 +00:00
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
/// Returns the maximum value of health casted to a float
|
|
|
|
pub fn maximum(&self) -> f32 { self.maximum as f32 / Self::SCALING_FACTOR_FLOAT }
|
|
|
|
|
|
|
|
/// Returns the fraction of health an entity has remaining
|
|
|
|
pub fn fraction(&self) -> f32 { self.current() / self.maximum().max(1.0) }
|
|
|
|
|
|
|
|
/// Updates the maximum value for health
|
|
|
|
pub fn update_maximum(&mut self, modifiers: comp::stats::StatsModifier) {
|
|
|
|
let maximum = modifiers
|
|
|
|
.compute_maximum(self.base_max as f32)
|
2021-09-10 19:20:14 +00:00
|
|
|
// NaN does not need to be handled here as rust will automatically change to 0 when casting to u32
|
|
|
|
.clamp(0.0, Self::MAX_SCALED_HEALTH as f32) as u32;
|
2021-09-09 04:07:17 +00:00
|
|
|
self.maximum = maximum;
|
2021-09-23 11:36:03 +00:00
|
|
|
// Clamp the current health to enforce the current <= maximum invariant.
|
|
|
|
self.current = self.current.min(maximum);
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub fn new(body: comp::Body, level: u16) -> Self {
|
2021-09-10 19:20:14 +00:00
|
|
|
let health = u32::from(
|
|
|
|
body.base_health()
|
|
|
|
.saturating_add(body.base_health_increase().saturating_mul(level)),
|
|
|
|
) * Self::SCALING_FACTOR_INT;
|
2020-10-31 22:34:08 +00:00
|
|
|
Health {
|
2021-09-09 04:07:17 +00:00
|
|
|
current: health,
|
|
|
|
base_max: health,
|
|
|
|
maximum: health,
|
2020-10-31 22:34:08 +00:00
|
|
|
last_change: (0.0, HealthChange {
|
2021-09-09 04:07:17 +00:00
|
|
|
amount: 0.0,
|
|
|
|
by: None,
|
|
|
|
cause: None,
|
2020-10-31 22:34:08 +00:00
|
|
|
}),
|
|
|
|
is_dead: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
// TODO: Delete this once stat points will be a thing
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-09-09 04:07:17 +00:00
|
|
|
pub fn update_max_hp(&mut self, body: comp::Body, level: u16) {
|
|
|
|
let old_max = self.base_max;
|
2021-09-10 19:20:14 +00:00
|
|
|
self.base_max = u32::from(
|
|
|
|
body.base_health()
|
|
|
|
.saturating_add(body.base_health_increase().saturating_mul(level)),
|
|
|
|
) * Self::SCALING_FACTOR_INT;
|
2021-09-09 04:07:17 +00:00
|
|
|
self.current = (self.current + self.base_max - old_max).min(self.maximum);
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-31 22:34:08 +00:00
|
|
|
pub fn change_by(&mut self, change: HealthChange) {
|
2021-09-10 19:20:14 +00:00
|
|
|
self.current = (((self.current() + change.amount).clamp(0.0, f32::from(Self::MAX_HEALTH))
|
|
|
|
as u32
|
|
|
|
* Self::SCALING_FACTOR_INT) as u32)
|
2021-09-09 04:07:17 +00:00
|
|
|
.min(self.maximum);
|
2020-10-31 22:34:08 +00:00
|
|
|
self.last_change = (0.0, change);
|
|
|
|
}
|
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
pub fn should_die(&self) -> bool { self.current == 0 }
|
2021-03-20 17:29:57 +00:00
|
|
|
|
2021-09-09 04:07:17 +00:00
|
|
|
pub fn kill(&mut self) {
|
|
|
|
self.current = 0;
|
|
|
|
self.is_dead = true;
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-31 22:34:08 +00:00
|
|
|
pub fn revive(&mut self) {
|
2021-09-09 04:07:17 +00:00
|
|
|
self.current = self.maximum;
|
2020-10-31 22:34:08 +00:00
|
|
|
self.is_dead = false;
|
|
|
|
}
|
2021-09-12 16:23:53 +00:00
|
|
|
|
|
|
|
// Function only exists for plugin tests, do not use anywhere else
|
|
|
|
// TODO: Remove somehow later
|
|
|
|
#[deprecated]
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Health {
|
|
|
|
current: 0,
|
|
|
|
base_max: 0,
|
|
|
|
maximum: 0,
|
|
|
|
last_change: (0.0, HealthChange {
|
|
|
|
amount: 0.0,
|
|
|
|
by: None,
|
|
|
|
cause: None,
|
|
|
|
}),
|
|
|
|
is_dead: false,
|
|
|
|
}
|
|
|
|
}
|
StaminaPlus buff, modifying stamina via buffs
trying to fix this, coming back to this later
please remember to change potion back future self!
this ALMOST works. maybe MR ready, kinda jank tho
so close, and yet so far...
IT WORKS IT WORKS IT WORKS IT WORKS IT WORKS IT WO
did the same with health, ill fix this garbage l8r
think we're basically done here
whoops forgot to change the food back
fixing and cleaning up part 1
fixed everything part 2 now with buff images
ran clippy + fmt, fixed items that i modified
bracket bulldozing, boldly
hopefully this should be good?
need to rebase real quick
please let me be done
StaminaPlus buff, modifying stamina via buffs
trying to fix this, coming back to this later
please remember to change potion back future self!
this ALMOST works. maybe MR ready, kinda jank tho
so close, and yet so far...
IT WORKS IT WORKS IT WORKS IT WORKS IT WORKS IT WO
did the same with health, ill fix this garbage l8r
think we're basically done here
whoops forgot to change the food back
fixing and cleaning up part 1
fixed everything part 2 now with buff images
ran clippy + fmt, fixed items that i modified
hopefully this should be good?
cargo clippy fmt stuff
deleted an extraneous file?? how did that even...?
2021-01-26 22:47:55 +00:00
|
|
|
}
|
2021-02-17 13:03:20 +00:00
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-31 22:34:08 +00:00
|
|
|
impl Component for Health {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2020-10-31 22:34:08 +00:00
|
|
|
}
|