Reduce duplicate storage fetching since it has overhead

This commit is contained in:
Imbris
2021-07-03 00:58:11 -04:00
parent 830b0f8fc1
commit c16c83063f

View File

@ -109,23 +109,20 @@ pub fn handle_mount(server: &mut Server, mounter: EcsEntity, mountee: EcsEntity)
Some(comp::MountState::Unmounted) Some(comp::MountState::Unmounted)
); );
let within_range = within_mounting_range( let within_range = || {
state.ecs().read_storage::<comp::Pos>().get(mounter), let positions = state.ecs().read_storage::<comp::Pos>();
state.ecs().read_storage::<comp::Pos>().get(mountee), within_mounting_range(positions.get(mounter), positions.get(mountee))
);
let alive = |e| {
state
.ecs()
.read_storage::<comp::Health>()
.get(e)
.map_or(true, |h| !h.is_dead)
}; };
let healths = state.ecs().read_storage::<comp::Health>();
let alive = |e| healths.get(e).map_or(true, |h| !h.is_dead);
if not_mounting_yet && within_range && alive(mounter) && alive(mountee) { if not_mounting_yet && within_range() && alive(mounter) && alive(mountee) {
if let (Some(mounter_uid), Some(mountee_uid)) = ( let uids = state.ecs().read_storage::<Uid>();
state.ecs().uid_from_entity(mounter), if let (Some(mounter_uid), Some(mountee_uid)) =
state.ecs().uid_from_entity(mountee), (uids.get(mounter).copied(), uids.get(mountee).copied())
) { {
drop(uids);
drop(healths);
// We know the entities must exist to be able to look up their UIDs, so these // We know the entities must exist to be able to look up their UIDs, so these
// are guaranteed to work; hence we can ignore possible errors here. // are guaranteed to work; hence we can ignore possible errors here.
state.write_component_ignore_entity_dead( state.write_component_ignore_entity_dead(