refactor: use restrict_mut

This commit is contained in:
timokoesters 2020-01-19 20:19:44 +01:00 committed by Pfauenauge90
parent 8afe5fd1dd
commit 65d0a1c4f4
2 changed files with 29 additions and 7 deletions

View File

@ -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()
{

View File

@ -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
}
}
}
}
}