Changed mod.rs to use CachedSpatialGrid

This commit is contained in:
TelepathicWalrus 2023-05-04 23:02:32 +01:00 committed by Maxicarlos08
parent d6496c999d
commit 5894913ede
No known key found for this signature in database

View File

@ -32,6 +32,7 @@ use common::{
trade::TradeResult, trade::TradeResult,
util::{Dir, Plane}, util::{Dir, Plane},
vol::ReadVol, vol::ReadVol,
CachedSpatialGrid,
}; };
use common_base::{prof_span, span}; use common_base::{prof_span, span};
use common_net::{msg::server::InviteAnswer, sync::WorldSyncExt}; use common_net::{msg::server::InviteAnswer, sync::WorldSyncExt};
@ -945,28 +946,34 @@ impl PlayState for SessionState {
.read_storage::<Pos>() .read_storage::<Pos>()
.get(client.entity()) .get(client.entity())
.copied(); .copied();
let mut close_pet = (None, None);
if let Some(player_pos) = player_pos { if let Some(player_pos) = player_pos {
// Find closest mountable entity let alignment =
let closest_pet = ( client.state().ecs().read_storage::<comp::Alignment>();
&client.state().ecs().entities(), let spatial_grid =
&client.state().ecs().read_storage::<Pos>(), client.state().ecs().read_resource::<CachedSpatialGrid>();
// TODO: More cleverly filter by things that can actually be mounted spatial_grid.0
client.state().ecs().read_storage::<comp::Alignment>().maybe(), .in_circle_aabr(player_pos.0.xy(), MAX_MOUNT_RANGE)
) .for_each(|e| {
.join() client
.filter(|(entity, _, _)| *entity != client.entity()) .state()
.filter(|(_, _, alignment)| matches!(alignment, Some(comp::Alignment::Owned(owner)) if Some(*owner) == client.uid())) .read_storage::<Pos>()
.map(|(entity, pos, _)| { .get(e)
(entity, player_pos.0.distance_squared(pos.0)) .map(|x|{
}) let distance = player_pos.0.distance_squared(x.0);
.filter(|(_, dist_sqr)| { if distance < close_pet.1.map_or(MAX_MOUNT_RANGE * MAX_MOUNT_RANGE, |x| x) && e != client.entity()
*dist_sqr < MAX_MOUNT_RANGE.powi(2) && matches!(alignment.get(e), Some(comp::Alignment::Owned(owner)) if Some(*owner) == client.uid()) {
}) close_pet.0 = Some(e);
.min_by_key(|(_, dist_sqr)| OrderedFloat(*dist_sqr)); close_pet.1 = Some(distance);
if let Some((pet_entity, _)) = closest_pet && client.state().read_storage::<Is<Mount>>().get(pet_entity).is_none() {
client.toggle_stay(pet_entity);
} }
} }
);
});
}
if let (Some(pet_entity), _) = close_pet && client.state().read_storage::<Is<Mount>>().get(pet_entity).is_none() {
client.toggle_stay(pet_entity);
}
}, },
GameInput::Interact => { GameInput::Interact => {
if state { if state {