Remove is_stay function

Clean code
This commit is contained in:
TelepathicWalrus 2023-04-30 19:26:52 +01:00 committed by Maxicarlos08
parent 6f0997705f
commit 9597810e3d
No known key found for this signature in database
12 changed files with 73 additions and 85 deletions

View File

@ -1477,7 +1477,7 @@ impl Client {
pub fn unmount(&mut self) { self.send_msg(ClientGeneral::ControlEvent(ControlEvent::Unmount)); }
pub fn toggle_stay(&mut self, entity: EcsEntity){
pub fn toggle_stay(&mut self, entity: EcsEntity) {
if let Some(uid) = self.state.read_component_copied(entity) {
self.send_msg(ClientGeneral::ControlEvent(ControlEvent::ToggleStay(uid)));
}

View File

@ -107,7 +107,6 @@ synced_components!(reexport_comps);
use crate::sync::{NetSync, SyncFrom};
// These are synced from any entity within range.
impl NetSync for PetStates {
const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;

View File

@ -1,8 +1,8 @@
use crate::comp::{body::Body, phys::Mass, quadruped_medium, quadruped_small};
use crossbeam_utils::atomic::AtomicCell;
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage};
use std::{num::NonZeroU64, sync::Arc};
use serde::{Deserialize, Serialize};
pub type PetId = AtomicCell<Option<NonZeroU64>>;
@ -109,24 +109,17 @@ impl Component for Pet {
type Storage = specs::VecStorage<Self>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum StayFollow{
pub enum StayFollow {
Stay,
Follow,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct PetState{
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct PetState {
pub stay: bool,
}
impl PetState {
pub fn get_state(&self) -> bool{
self.stay
}
}
impl Component for PetState {
type Storage = DerefFlaggedStorage<Self>;
}

View File

@ -65,11 +65,12 @@ impl<'a> System<'a> for Sys {
},
ControlEvent::ToggleStay(pet_uid) => {
if let Some(pet_entity) = read_data
.uid_allocator
.retrieve_entity_internal(pet_uid.id()){
server_emitter.emit(ServerEvent::ToggleStay(pet_entity));
}
}
.id_maps
.uid_entity(pet_uid)
{
server_emitter.emit(ServerEvent::ToggleStay(pet_entity));
}
},
ControlEvent::RemoveBuff(buff_id) => {
server_emitter.emit(ServerEvent::Buff {
entity,

View File

@ -12,9 +12,10 @@ use common::{
},
slot::EquipSlot,
},
pet::PetState,
ActiveAbilities, Alignment, Body, CharacterState, Combo, Energy, Health, Inventory,
LightEmitter, LootOwner, Ori, PhysicsState, Poise, Pos, Presence, Scale, SkillSet, Stance,
Stats, Vel, pet::PetState,
Stats, Vel,
},
consts::GRAVITY,
link::Is,

View File

@ -202,15 +202,23 @@ pub fn handle_unmount(server: &mut Server, rider: EcsEntity) {
state.ecs().write_storage::<Is<VolumeRider>>().remove(rider);
}
pub fn handle_toggle_stay(server: &mut Server, pet: EcsEntity){
pub fn handle_toggle_stay(server: &mut Server, pet: EcsEntity) {
let state = server.state_mut();
if state.ecs()
if state
.ecs()
.read_storage::<PetState>()
.get(pet)
.map_or(false, |s| s.stay){
let _ = state.ecs().write_storage::<PetState>().insert(pet, PetState { stay: false });
.map_or(false, |s| s.stay)
{
let _ = state
.ecs()
.write_storage::<PetState>()
.insert(pet, PetState { stay: false });
} else {
let _ = state.ecs().write_storage::<PetState>().insert(pet, PetState { stay: true });
let _ = state
.ecs()
.write_storage::<PetState>()
.insert(pet, PetState { stay: true });
}
}

View File

@ -23,7 +23,7 @@ use group_manip::handle_group;
use information::handle_site_info;
use interaction::{
handle_create_sprite, handle_lantern, handle_mine_block, handle_mount, handle_npc_interaction,
handle_sound, handle_unmount, handle_toggle_stay,
handle_sound, handle_toggle_stay, handle_unmount,
};
use inventory_manip::handle_inventory;
use invite::{handle_invite, handle_invite_response};

View File

@ -160,8 +160,7 @@ impl<'a> System<'a> for Sys {
.map_or(false, |item| {
matches!(&*item.kind(), comp::item::ItemKind::Glider)
});
let is_stay = pet_state
.map_or(false, |s| s.stay);
let is_stay = pet_state.map_or(false, |s| s.stay);
let is_gliding = matches!(
read_data.char_states.get(entity),

View File

@ -408,7 +408,7 @@ fn follow_if_far_away(bdata: &mut BehaviorData) -> bool {
if let Some(tgt_pos) = bdata.read_data.positions.get(target) {
let dist_sqrd = bdata.agent_data.pos.0.distance_squared(tgt_pos.0);
let stay = bdata.agent_data.is_stay;
if dist_sqrd > (MAX_PATROL_DIST * bdata.agent.psyche.idle_wander_factor).powi(2) && !stay{
if dist_sqrd > (MAX_PATROL_DIST * bdata.agent.psyche.idle_wander_factor).powi(2) && !stay {
bdata
.agent_data
.follow(bdata.agent, bdata.controller, bdata.read_data, tgt_pos);
@ -432,7 +432,7 @@ fn attack_if_owner_hurt(bdata: &mut BehaviorData) -> bool {
false
};
let stay = bdata.agent_data.is_stay;
if owner_recently_attacked && !stay{
if owner_recently_attacked && !stay {
bdata.agent_data.attack_target_attacker(
bdata.agent,
bdata.read_data,

View File

@ -1,5 +1,5 @@
use common::{
comp::{Alignment, Pet, PhysicsState, Pos, pet::PetState},
comp::{pet::PetState, Alignment, Pet, PhysicsState, Pos},
terrain::TerrainGrid,
uid::IdMaps,
};
@ -30,7 +30,7 @@ impl<'a> System<'a> for Sys {
(entities, terrain, mut positions, alignments, pets, pet_state, physics, id_maps): Self::SystemData,
) {
const LOST_PET_DISTANCE_THRESHOLD: f32 = 200.0;
// Find pets that are too far away from their owner
let lost_pets: Vec<(Entity, Pos)> = (&entities, &positions, &alignments, &pets)
.join()
@ -56,7 +56,7 @@ impl<'a> System<'a> for Sys {
})
.map(|(entity, owner_pos, _, _)| (entity, *owner_pos))
.collect();
for (pet_entity, owner_pos) in lost_pets.iter() {
let stay = pet_state.get(*pet_entity).map_or(false, |f| f.stay);
if let Some(mut pet_pos) = positions.get_mut(*pet_entity) && !stay{

View File

@ -2317,13 +2317,12 @@ impl Hud {
poise,
(alignment, is_mount, is_rider, stance),
)| {
// Use interpolated position if available
let pos = interpolated.map_or(pos.0, |i| i.pos);
let in_group = client.group_members().contains_key(uid);
let is_me = entity == me;
let dist_sqr = pos.distance_squared(player_pos);
// Determine whether to display nametag and healthbar based on whether the
// entity is mounted, has been damaged, is targeted/selected, or is in your
// group
@ -2433,32 +2432,22 @@ impl Hud {
i18n.get_msg("hud-mount").to_string(),
));
}
}
let p = entity;
let s = is_stay.get(p)
.map(|st| st.stay);
match s {
Some(false) => {
options.push((
GameInput::StayFollow,
i18n.get_msg("hud-stay").to_string(),
))
},
Some(true) => {
options.push((
GameInput::StayFollow,
i18n.get_msg("hud-follow").to_string(),
))
},
None => {
options.push((
GameInput::StayFollow,
i18n.get_msg("hud-stay").to_string(),
))
},
}
let pet_stay = is_stay.get(entity).map(|st| st.stay);
match pet_stay {
Some(false) => options.push((
GameInput::StayFollow,
i18n.get_msg("hud-stay").to_string(),
)),
Some(true) => options.push((
GameInput::StayFollow,
i18n.get_msg("hud-follow").to_string(),
)),
None => options.push((
GameInput::StayFollow,
i18n.get_msg("hud-stay").to_string(),
)),
}
options
},
_ => Vec::new(),

View File

@ -943,32 +943,30 @@ impl PlayState for SessionState {
let player_pos = client
.state()
.read_storage::<Pos>()
.get(client.entity())
.copied();
if let Some(player_pos) = player_pos {
// Find closest mountable entity
let closest_pet = (
&client.state().ecs().entities(),
&client.state().ecs().read_storage::<Pos>(),
// TODO: More cleverly filter by things that can actually be mounted
client.state().ecs().read_storage::<comp::Alignment>().maybe(),
)
.join()
.filter(|(entity, _, _)| *entity != client.entity())
.filter(|(_, _, alignment)| matches!(alignment, Some(comp::Alignment::Owned(owner)) if Some(*owner) == client.uid()))
.map(|(entity, pos, _)| {
(entity, player_pos.0.distance_squared(pos.0))
})
.filter(|(_, dist_sqr)| {
*dist_sqr < MAX_MOUNT_RANGE.powi(2)
})
.min_by_key(|(_, dist_sqr)| OrderedFloat(*dist_sqr));
if let Some((pet_entity, _)) = closest_pet
{
client.toggle_stay(pet_entity);
}
}
.get(client.entity())
.copied();
if let Some(player_pos) = player_pos {
// Find closest mountable entity
let closest_pet = (
&client.state().ecs().entities(),
&client.state().ecs().read_storage::<Pos>(),
// TODO: More cleverly filter by things that can actually be mounted
client.state().ecs().read_storage::<comp::Alignment>().maybe(),
)
.join()
.filter(|(entity, _, _)| *entity != client.entity())
.filter(|(_, _, alignment)| matches!(alignment, Some(comp::Alignment::Owned(owner)) if Some(*owner) == client.uid()))
.map(|(entity, pos, _)| {
(entity, player_pos.0.distance_squared(pos.0))
})
.filter(|(_, dist_sqr)| {
*dist_sqr < MAX_MOUNT_RANGE.powi(2)
})
.min_by_key(|(_, dist_sqr)| OrderedFloat(*dist_sqr));
if let Some((pet_entity, _)) = closest_pet {
client.toggle_stay(pet_entity);
}
}
},
GameInput::Interact => {
if state {