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
|
|
|
|
// outside this module
|
2020-10-31 22:34:08 +00:00
|
|
|
current: u32,
|
2021-03-20 17:29:57 +00:00
|
|
|
base_max: u32,
|
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-09 04:07:17 +00:00
|
|
|
/// The maximum value allowed for current and maximum health
|
|
|
|
/// Maximum value is u16:MAX - 1 * 256, which only requires 24 bits. This
|
|
|
|
/// can fit into an f32 with no loss to precision
|
|
|
|
const MAX_HEALTH: u32 = 16776960;
|
|
|
|
/// The amount health is scaled by within this module
|
|
|
|
const SCALING_FACTOR_FLOAT: f32 = 256.;
|
|
|
|
const SCALING_FACTOR_INT: u32 = 256;
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
.min(Self::MAX_HEALTH as f32) as u32;
|
|
|
|
self.maximum = 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 {
|
|
|
|
let health = u32::from(body.base_health() + body.base_health_increase() * 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;
|
|
|
|
self.base_max = u32::from(body.base_health() + body.base_health_increase() * level)
|
|
|
|
* Self::SCALING_FACTOR_INT;
|
|
|
|
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-09 04:07:17 +00:00
|
|
|
self.current = (((self.current() + change.amount) as u32 * Self::SCALING_FACTOR_INT).max(0)
|
|
|
|
as u32)
|
|
|
|
.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;
|
|
|
|
}
|
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
|
|
|
}
|