From c3b834ec15bc2ff55b9610ff2439ac06d3de8275 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 20 May 2021 19:52:29 -0500 Subject: [PATCH] Max energy stat functional. --- common/src/comp/energy.rs | 54 ++++++++++++++++--------- common/src/comp/inventory/item/armor.rs | 10 ++--- common/src/comp/stats.rs | 4 ++ common/systems/src/buff.rs | 25 ++++-------- common/systems/src/lib.rs | 2 +- common/systems/src/stats.rs | 11 ++++- 6 files changed, 63 insertions(+), 43 deletions(-) diff --git a/common/src/comp/energy.rs b/common/src/comp/energy.rs index a10546e038..d3e4bff462 100644 --- a/common/src/comp/energy.rs +++ b/common/src/comp/energy.rs @@ -6,9 +6,8 @@ use specs_idvs::IdvStorage; #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub struct Energy { current: u32, - maximum: u32, base_max: u32, - last_max: u32, + maximum: u32, pub regen_rate: f32, pub last_change: Option<(i32, f64, EnergySource)>, } @@ -45,7 +44,6 @@ impl Energy { current: 0, maximum: 0, base_max: 0, - last_max: 0, regen_rate: 0.0, last_change: None, } @@ -53,6 +51,8 @@ impl Energy { pub fn current(&self) -> u32 { self.current } + pub fn base_max(&self) -> u32 { self.base_max } + pub fn maximum(&self) -> u32 { self.maximum } pub fn set_to(&mut self, amount: u32, cause: EnergySource) { @@ -74,6 +74,12 @@ impl Energy { self.current = self.current.min(self.maximum); } + // Scales the temporary max health by a modifier. + pub fn scale_maximum(&mut self, scaled: f32) { + let scaled_max = (self.base_max as f32 * scaled) as u32; + self.set_maximum(scaled_max); + } + pub fn try_change_by( &mut self, amount: i32, @@ -92,29 +98,18 @@ impl Energy { } } - //sets last_max to base HP, then if the current is more than your base_max - // it'll set it to base max - pub fn last_set(&mut self) { self.last_max = self.maximum } - pub fn update_max_energy(&mut self, body: Option, level: u16) { + const ENERGY_PER_LEVEL: u32 = 50; if let Some(body) = body { - self.set_base_max(body.base_energy() + 50 * level as u32); - self.set_maximum(body.base_energy() + 50 * level as u32); + self.set_base_max(body.base_energy() + ENERGY_PER_LEVEL * level as u32); + self.set_maximum(body.base_energy() + ENERGY_PER_LEVEL * level as u32); self.change_by(EnergyChange { - amount: 50, + amount: ENERGY_PER_LEVEL as i32, source: EnergySource::LevelUp, }); } } - pub fn reset_max(&mut self) { - self.maximum = self.base_max; - if self.current > self.last_max { - self.current = self.last_max; - self.last_max = self.base_max; - } - } - // This is private because max energy is based on the level fn set_base_max(&mut self, amount: u32) { self.base_max = amount; @@ -138,6 +133,29 @@ impl Energy { .fold(1.0, |a, b| a + b) }) } + + /// Computes the modifier that should be applied to max energy from the + /// currently equipped items + pub fn compute_max_energy_mod_from_inv(&self, inventory: Option<&Inventory>) -> f32 { + use comp::item::ItemKind; + // Defaults to a value of 0 if no inventory is equipped + let energy_increase = inventory.map_or(0, |inv| { + inv.equipped_items() + .filter_map(|item| { + if let ItemKind::Armor(armor) = &item.kind() { + Some(armor.get_energy_max()) + } else { + None + } + }) + .sum() + }); + // Returns the energy increase divided by base max of energy. + // This value is then added to the max_energy_modifier field on stats component. + // Adding is important here, as it ensures that a flat modifier is applied + // correctly. + energy_increase as f32 / self.base_max as f32 + } } pub struct EnergyChange { diff --git a/common/src/comp/inventory/item/armor.rs b/common/src/comp/inventory/item/armor.rs index 2d6ffe18b7..204101826b 100644 --- a/common/src/comp/inventory/item/armor.rs +++ b/common/src/comp/inventory/item/armor.rs @@ -33,7 +33,7 @@ pub struct Stats { #[serde(default)] poise_resilience: Protection, #[serde(default)] - energy_max: u32, + energy_max: i32, #[serde(default)] energy_recovery: f32, #[serde(default)] @@ -48,7 +48,7 @@ impl Stats { pub fn new( protection: Protection, poise_resilience: Protection, - energy_max: u32, + energy_max: i32, energy_recovery: f32, crit_chance: f32, stealth: f32, @@ -67,7 +67,7 @@ impl Stats { pub fn get_poise_resilience(&self) -> Protection { self.poise_resilience } - pub fn get_energy_max(&self) -> u32 { self.energy_max } + pub fn get_energy_max(&self) -> i32 { self.energy_max } pub fn get_energy_recovery(&self) -> f32 { self.energy_recovery } @@ -83,7 +83,7 @@ impl Sub for Stats { Self { protection: self.protection - other.protection, poise_resilience: self.poise_resilience - other.poise_resilience, - energy_max: self.energy_max.saturating_sub(other.energy_max), + energy_max: self.energy_max - other.energy_max, energy_recovery: self.energy_recovery - other.energy_recovery, crit_chance: self.crit_chance - other.crit_chance, stealth: self.stealth - other.stealth, @@ -139,7 +139,7 @@ impl Armor { pub fn get_poise_resilience(&self) -> Protection { self.stats.poise_resilience } - pub fn get_energy_max(&self) -> u32 { self.stats.energy_max } + pub fn get_energy_max(&self) -> i32 { self.stats.energy_max } pub fn get_energy_recovery(&self) -> f32 { self.stats.energy_recovery } diff --git a/common/src/comp/stats.rs b/common/src/comp/stats.rs index fd962d702b..d3f2b6d77e 100644 --- a/common/src/comp/stats.rs +++ b/common/src/comp/stats.rs @@ -28,6 +28,7 @@ pub struct Stats { pub move_speed_modifier: f32, pub attack_speed_modifier: f32, pub friction_modifier: f32, + pub max_energy_modifier: f32, } impl Stats { @@ -39,6 +40,7 @@ impl Stats { move_speed_modifier: 1.0, attack_speed_modifier: 1.0, friction_modifier: 1.0, + max_energy_modifier: 1.0, } } @@ -52,6 +54,7 @@ impl Stats { move_speed_modifier: 1.0, attack_speed_modifier: 1.0, friction_modifier: 1.0, + max_energy_modifier: 1.0, } } @@ -62,6 +65,7 @@ impl Stats { self.move_speed_modifier = 1.0; self.attack_speed_modifier = 1.0; self.friction_modifier = 1.0; + self.max_energy_modifier = 1.0; } } diff --git a/common/systems/src/buff.rs b/common/systems/src/buff.rs index a04d006053..b504dbc4a2 100644 --- a/common/systems/src/buff.rs +++ b/common/systems/src/buff.rs @@ -23,6 +23,7 @@ pub struct ReadData<'a> { inventories: ReadStorage<'a, Inventory>, healths: ReadStorage<'a, Health>, physics_states: ReadStorage<'a, PhysicsState>, + energies: ReadStorage<'a, Energy>, } #[derive(Default)] @@ -30,7 +31,6 @@ pub struct Sys; impl<'a> System<'a> for Sys { type SystemData = ( ReadData<'a>, - WriteStorage<'a, Energy>, WriteStorage<'a, Buffs>, WriteStorage<'a, Stats>, ); @@ -39,20 +39,16 @@ impl<'a> System<'a> for Sys { const ORIGIN: Origin = Origin::Common; const PHASE: Phase = Phase::Create; - fn run( - _job: &mut Job, - (read_data, mut energies, mut buffs, mut stats): Self::SystemData, - ) { + fn run(_job: &mut Job, (read_data, mut buffs, mut stats): Self::SystemData) { let mut server_emitter = read_data.server_bus.emitter(); let dt = read_data.dt.0; // Set to false to avoid spamming server buffs.set_event_emission(false); - energies.set_event_emission(false); stats.set_event_emission(false); - for (entity, mut buff_comp, mut energy, mut stat, health) in ( + for (entity, mut buff_comp, energy, mut stat, health) in ( &read_data.entities, &mut buffs, - &mut energies, + &read_data.energies, &mut stats, &read_data.healths, ) @@ -106,9 +102,7 @@ impl<'a> System<'a> for Sys { } } - // Call to reset energy and stats to base values - energy.last_set(); - energy.reset_max(); + // Call to reset stats to base values stat.reset_temp_modifiers(); // Iterator over the lists of buffs by kind @@ -160,7 +154,7 @@ impl<'a> System<'a> for Sys { }, BuffEffect::MaxHealthModifier { value, kind } => match kind { ModifierKind::Additive => { - stat.max_health_modifier += *value / (health.maximum() as f32); + stat.max_health_modifier += *value / (health.base_max() as f32); }, ModifierKind::Fractional => { stat.max_health_modifier *= *value; @@ -168,12 +162,10 @@ impl<'a> System<'a> for Sys { }, BuffEffect::MaxEnergyModifier { value, kind } => match kind { ModifierKind::Additive => { - let new_max = (energy.maximum() as f32 + *value) as u32; - energy.set_maximum(new_max); + stat.max_energy_modifier += *value / (energy.base_max() as f32); }, ModifierKind::Fractional => { - let new_max = (energy.maximum() as f32 + *value) as u32; - energy.set_maximum(new_max); + stat.max_energy_modifier *= *value; }, }, BuffEffect::DamageReduction(dr) => { @@ -244,7 +236,6 @@ impl<'a> System<'a> for Sys { } // Turned back to true buffs.set_event_emission(true); - energies.set_event_emission(true); stats.set_event_emission(true); } } diff --git a/common/systems/src/lib.rs b/common/systems/src/lib.rs index 8e6f63292a..bd0b018e3d 100644 --- a/common/systems/src/lib.rs +++ b/common/systems/src/lib.rs @@ -24,8 +24,8 @@ pub fn add_local_systems(dispatch_builder: &mut DispatcherBuilder) { dispatch::(dispatch_builder, &[]); dispatch::(dispatch_builder, &[&mount::Sys::sys_name()]); dispatch::(dispatch_builder, &[&controller::Sys::sys_name()]); - dispatch::(dispatch_builder, &[]); dispatch::(dispatch_builder, &[]); + dispatch::(dispatch_builder, &[&buff::Sys::sys_name()]); dispatch::(dispatch_builder, &[ &interpolation::Sys::sys_name(), &controller::Sys::sys_name(), diff --git a/common/systems/src/stats.rs b/common/systems/src/stats.rs index 4539978d62..662c90ac10 100644 --- a/common/systems/src/stats.rs +++ b/common/systems/src/stats.rs @@ -2,7 +2,7 @@ use common::{ comp::{ self, skills::{GeneralSkill, Skill}, - Body, CharacterState, Combo, Energy, EnergyChange, EnergySource, Health, Poise, + Body, CharacterState, Combo, Energy, EnergyChange, EnergySource, Health, Inventory, Poise, PoiseChange, PoiseSource, Pos, SkillSet, Stats, }, event::{EventBus, ServerEvent}, @@ -30,6 +30,7 @@ pub struct ReadData<'a> { uids: ReadStorage<'a, Uid>, bodies: ReadStorage<'a, Body>, char_states: ReadStorage<'a, CharacterState>, + inventories: ReadStorage<'a, Inventory>, } /// This system kills players, levels them up, and regenerates energy. @@ -84,13 +85,15 @@ impl<'a> System<'a> for Sys { poises.set_event_emission(true); // Update stats - for (entity, uid, stats, mut skill_set, mut health, pos) in ( + for (entity, uid, stats, mut skill_set, mut health, pos, mut energy, inventory) in ( &read_data.entities, &read_data.uids, &stats, &mut skill_sets.restrict_mut(), &mut healths.restrict_mut(), &read_data.positions, + &mut energies, + read_data.inventories.maybe(), ) .join() { @@ -123,6 +126,10 @@ impl<'a> System<'a> for Sys { health.scale_maximum(stat.max_health_modifier); } + // Happens every tick unlike health in order to apply inventory effects + let max_energy_inventory_mod = energy.compute_max_energy_mod_from_inv(inventory); + energy.scale_maximum(stat.max_energy_modifier + max_energy_inventory_mod); + let skillset = skill_set.get_unchecked(); let skills_to_level = skillset .skill_groups