From 65d0a1c4f476580fb9f2b74cf30dfe0619f67dc9 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sun, 19 Jan 2020 20:19:44 +0100 Subject: [PATCH] refactor: use restrict_mut --- common/src/sys/controller.rs | 16 ++++++++++++++-- common/src/sys/stats.rs | 20 +++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/common/src/sys/controller.rs b/common/src/sys/controller.rs index 86e468b45c..c63f726055 100644 --- a/common/src/sys/controller.rs +++ b/common/src/sys/controller.rs @@ -277,13 +277,24 @@ impl<'a> System<'a> for Sys { ) { let mut server_emitter = server_bus.emitter(); let mut local_emitter = local_bus.emitter(); - for (entity, uid, controller, mut character, stats, energy, body, vel, physics, mount) in ( + for ( + entity, + uid, + controller, + mut character, + stats, + mut energy, + body, + vel, + physics, + mount, + ) in ( &entities, &uids, &mut controllers, &mut character_states, &stats, - &mut energies, + &mut energies.restrict_mut(), &bodies, &velocities, &physics_states, @@ -585,6 +596,7 @@ impl<'a> System<'a> for Sys { // Try to charge if inputs.charge.is_pressed() && !inputs.charge.is_held_down() { if energy + .get_mut_unchecked() .try_change_by(-CHARGE_COST, EnergySource::CastSpell) .is_ok() { diff --git a/common/src/sys/stats.rs b/common/src/sys/stats.rs index b013a12171..ad5e16e984 100644 --- a/common/src/sys/stats.rs +++ b/common/src/sys/stats.rs @@ -33,11 +33,11 @@ impl<'a> System<'a> for Sys { stats.set_event_emission(true); // Mutates all stats every tick causing the server to resend this component for every entity every tick - for (entity, character_state, mut stats, energy) in ( + for (entity, character_state, mut stats, mut energy) in ( &entities, &character_states, &mut stats.restrict_mut(), - &mut energies, + &mut energies.restrict_mut(), ) .join() { @@ -75,11 +75,21 @@ impl<'a> System<'a> for Sys { // Accelerate recharging energy if not wielding. match character_state.action { ActionState::Idle => { - energy.regen_rate += ENERGY_REGEN_ACCEL * dt.0; - energy.change_by(energy.regen_rate as i32, EnergySource::Regen); + if { + let energy = energy.get_unchecked(); + energy.current() < energy.maximum() + } { + let mut energy = energy.get_mut_unchecked(); + energy.regen_rate += ENERGY_REGEN_ACCEL * dt.0; + energy.change_by(energy.regen_rate as i32, EnergySource::Regen); + } } // All other states do not regen and set the rate back to zero. - _ => energy.regen_rate = 0.0, + _ => { + if energy.get_unchecked().regen_rate != 0.0 { + energy.get_mut_unchecked().regen_rate = 0.0 + } + } } } }