#918 remove usages of .restrict_mut() from character_behavior.rs

This commit is contained in:
Thomas Kosel 2021-07-27 23:34:16 +02:00
parent 27f666622f
commit acb7f5dc4b
No known key found for this signature in database
GPG Key ID: AC43C6126CC42595
2 changed files with 62 additions and 79 deletions

View File

@ -8,12 +8,7 @@ use crate::{
terrain::TerrainGrid,
uid::Uid,
};
use specs::{
hibitset,
storage::{PairedStorage, SequentialRestriction},
DerefFlaggedStorage, Entity, LazyUpdate,
};
use specs_idvs::IdvStorage;
use specs::{storage::FlaggedAccessMut, Entity, LazyUpdate};
use vek::*;
pub trait CharacterBehavior {
@ -100,25 +95,16 @@ pub struct JoinData<'a> {
pub terrain: &'a TerrainGrid,
}
type RestrictedMut<'a, C> = PairedStorage<
'a,
'a,
C,
&'a mut DerefFlaggedStorage<C, IdvStorage<C>>,
&'a hibitset::BitSet,
SequentialRestriction,
>;
pub struct JoinStruct<'a> {
pub entity: Entity,
pub uid: &'a Uid,
pub char_state: RestrictedMut<'a, CharacterState>,
pub char_state: FlaggedAccessMut<'a, &'a mut CharacterState, CharacterState>,
pub pos: &'a mut Pos,
pub vel: &'a mut Vel,
pub ori: &'a mut Ori,
pub mass: &'a Mass,
pub density: &'a mut Density,
pub energy: RestrictedMut<'a, Energy>,
pub energy: FlaggedAccessMut<'a, &'a mut Energy, Energy>,
pub inventory: Option<&'a Inventory>,
pub controller: &'a mut Controller,
pub health: Option<&'a Health>,
@ -143,13 +129,13 @@ impl<'a> JoinData<'a> {
Self {
entity: j.entity,
uid: j.uid,
character: j.char_state.get_unchecked(),
character: &j.char_state,
pos: j.pos,
vel: j.vel,
ori: j.ori,
mass: j.mass,
density: j.density,
energy: j.energy.get_unchecked(),
energy: &j.energy,
inventory: j.inventory,
controller: j.controller,
inputs: &j.controller.inputs,

View File

@ -28,16 +28,16 @@ fn incorporate_update(
server_emitter: &mut Emitter<ServerEvent>,
) {
// TODO: if checking equality is expensive use optional field in StateUpdate
if join.char_state.get_unchecked() != &state_update.character {
*join.char_state.get_mut_unchecked() = state_update.character
if *join.char_state != state_update.character {
*join.char_state = state_update.character
};
*join.pos = state_update.pos;
*join.vel = state_update.vel;
*join.ori = state_update.ori;
*join.density = state_update.density;
// Note: might be changed every tick by timer anyway
if join.energy.get_unchecked() != &state_update.energy {
*join.energy.get_mut_unchecked() = state_update.energy
if *join.energy != state_update.energy {
*join.energy = state_update.energy
};
join.controller
.queued_inputs
@ -74,7 +74,6 @@ pub struct ReadData<'a> {
combos: ReadStorage<'a, Combo>,
alignments: ReadStorage<'a, comp::Alignment>,
terrain: ReadExpect<'a, TerrainGrid>,
inventories: ReadStorage<'a, Inventory>,
}
/// ## Character Behavior System
@ -93,6 +92,7 @@ impl<'a> System<'a> for Sys {
WriteStorage<'a, Ori>,
WriteStorage<'a, Density>,
WriteStorage<'a, Energy>,
WriteStorage<'a, Inventory>,
WriteStorage<'a, Controller>,
WriteStorage<'a, Poise>,
Write<'a, Vec<Outcome>>,
@ -112,6 +112,7 @@ impl<'a> System<'a> for Sys {
mut orientations,
mut densities,
mut energies,
mut inventories,
mut controllers,
mut poises,
mut outcomes,
@ -140,14 +141,14 @@ impl<'a> System<'a> for Sys {
) in (
&read_data.entities,
&read_data.uids,
&mut character_states.restrict_mut(),
&mut character_states,
&mut positions,
&mut velocities,
&mut orientations,
&read_data.masses,
&mut densities,
&mut energies.restrict_mut(),
read_data.inventories.maybe(),
&mut energies,
&mut inventories,
&mut controllers,
read_data.healths.maybe(),
&read_data.bodies,
@ -165,7 +166,7 @@ impl<'a> System<'a> for Sys {
// Enter stunned state if poise damage is enough
if let Some(mut poise) = poises.get_mut(entity) {
let was_wielded = char_state.get_unchecked().is_wield();
let was_wielded = char_state.is_wield();
let poise_state = poise.poise_state();
let pos = pos.0;
// Remove potion/saturation buff if knocked into poise state
@ -184,8 +185,7 @@ impl<'a> System<'a> for Sys {
PoiseState::Normal => {},
PoiseState::Interrupted => {
poise.reset();
*char_state.get_mut_unchecked() =
CharacterState::Stunned(common::states::stunned::Data {
*char_state = CharacterState::Stunned(common::states::stunned::Data {
static_data: common::states::stunned::StaticData {
buildup_duration: Duration::from_millis(125),
recover_duration: Duration::from_millis(125),
@ -203,8 +203,7 @@ impl<'a> System<'a> for Sys {
},
PoiseState::Stunned => {
poise.reset();
*char_state.get_mut_unchecked() =
CharacterState::Stunned(common::states::stunned::Data {
*char_state = CharacterState::Stunned(common::states::stunned::Data {
static_data: common::states::stunned::StaticData {
buildup_duration: Duration::from_millis(300),
recover_duration: Duration::from_millis(300),
@ -226,8 +225,7 @@ impl<'a> System<'a> for Sys {
},
PoiseState::Dazed => {
poise.reset();
*char_state.get_mut_unchecked() =
CharacterState::Stunned(common::states::stunned::Data {
*char_state = CharacterState::Stunned(common::states::stunned::Data {
static_data: common::states::stunned::StaticData {
buildup_duration: Duration::from_millis(600),
recover_duration: Duration::from_millis(250),
@ -249,8 +247,7 @@ impl<'a> System<'a> for Sys {
},
PoiseState::KnockedDown => {
poise.reset();
*char_state.get_mut_unchecked() =
CharacterState::Stunned(common::states::stunned::Data {
*char_state = CharacterState::Stunned(common::states::stunned::Data {
static_data: common::states::stunned::StaticData {
buildup_duration: Duration::from_millis(750),
recover_duration: Duration::from_millis(500),
@ -286,7 +283,7 @@ impl<'a> System<'a> for Sys {
mass,
density: &mut density,
energy,
inventory,
inventory: Some(&inventory),
controller: &mut controller,
health,
body,
@ -357,8 +354,8 @@ impl<'a> System<'a> for Sys {
// If mounted, character state is controlled by mount
if let Some(Mounting(_)) = read_data.mountings.get(entity) {
let idle_state = CharacterState::Idle {};
if join_struct.char_state.get_unchecked() != &idle_state {
*join_struct.char_state.get_mut_unchecked() = idle_state;
if *join_struct.char_state != idle_state {
*join_struct.char_state = idle_state;
}
continue;
}