From 13dbf679c5f1e7fe7f6513f5c339c9c7e46ebe4a Mon Sep 17 00:00:00 2001 From: Maxicarlos08 Date: Mon, 14 Aug 2023 14:40:43 +0200 Subject: [PATCH] review --- client/src/lib.rs | 6 ++++-- common/src/comp/controller.rs | 2 +- common/src/event.rs | 2 +- common/systems/src/controller.rs | 4 ++-- server/src/events/interaction.rs | 22 ++++++++++------------ server/src/events/mod.rs | 6 +++--- voxygen/src/session/mod.rs | 18 ++++++++++-------- 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 22852894b8..cd07e42870 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1477,9 +1477,11 @@ impl Client { pub fn unmount(&mut self) { self.send_msg(ClientGeneral::ControlEvent(ControlEvent::Unmount)); } - pub fn toggle_stay(&mut self, entity: EcsEntity) { + pub fn set_pet_stay(&mut self, entity: EcsEntity, stay: bool) { if let Some(uid) = self.state.read_component_copied(entity) { - self.send_msg(ClientGeneral::ControlEvent(ControlEvent::ToggleStay(uid))); + self.send_msg(ClientGeneral::ControlEvent(ControlEvent::SetPetStay( + uid, stay, + ))); } } diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 4d738f992a..c0e1e82159 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -146,7 +146,7 @@ pub enum ControlEvent { Mount(Uid), MountVolume(VolumePos), Unmount, - ToggleStay(Uid), + SetPetStay(Uid, bool), InventoryEvent(InventoryEvent), GroupManip(GroupManip), RemoveBuff(BuffKind), diff --git a/common/src/event.rs b/common/src/event.rs index 81d09a7517..006607afd5 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -200,7 +200,7 @@ pub enum ServerEvent { Mount(EcsEntity, EcsEntity), MountVolume(EcsEntity, VolumePos), Unmount(EcsEntity), - ToggleStay(EcsEntity, EcsEntity), + SetPetStay(EcsEntity, EcsEntity, bool), Possess(Uid, Uid), /// Inserts default components for a character when loading into the game InitCharacterData { diff --git a/common/systems/src/controller.rs b/common/systems/src/controller.rs index 64c275f040..9da0652d13 100644 --- a/common/systems/src/controller.rs +++ b/common/systems/src/controller.rs @@ -63,9 +63,9 @@ impl<'a> System<'a> for Sys { } } }, - ControlEvent::ToggleStay(pet_uid) => { + ControlEvent::SetPetStay(pet_uid, stay) => { if let Some(pet_entity) = read_data.id_maps.uid_entity(pet_uid) { - server_emitter.emit(ServerEvent::ToggleStay(entity, pet_entity)); + server_emitter.emit(ServerEvent::SetPetStay(entity, pet_entity, stay)); } }, ControlEvent::RemoveBuff(buff_id) => { diff --git a/server/src/events/interaction.rs b/server/src/events/interaction.rs index 15d06a9c21..9741c958af 100755 --- a/server/src/events/interaction.rs +++ b/server/src/events/interaction.rs @@ -210,7 +210,12 @@ pub fn handle_unmount(server: &mut Server, rider: EcsEntity) { state.ecs().write_storage::>().remove(rider); } -pub fn handle_toggle_stay(server: &mut Server, command_giver: EcsEntity, pet: EcsEntity) { +pub fn handle_set_pet_stay( + server: &mut Server, + command_giver: EcsEntity, + pet: EcsEntity, + stay: bool, +) { let state = server.state_mut(); let positions = state.ecs().read_storage::(); let is_owner = state @@ -226,15 +231,8 @@ pub fn handle_toggle_stay(server: &mut Server, command_giver: EcsEntity, pet: Ec ) }); - let previous_pet_pos = state - .ecs() - .read_storage::() - .get(pet) - .and_then(|s| s.stay_pos); - let new_pet_pos = previous_pet_pos - .is_none() - .then_some(positions.get(pet).copied()) - .flatten(); + let current_pet_position = positions.get(pet).copied(); + let stay = stay && current_pet_position.is_some(); if is_owner && within_mounting_range(positions.get(command_giver), positions.get(pet)) && state.ecs().read_storage::>().get(pet).is_none() @@ -243,12 +241,12 @@ pub fn handle_toggle_stay(server: &mut Server, command_giver: EcsEntity, pet: Ec .ecs() .write_storage::() .get_mut(pet) - .map(|mut activity| activity.is_pet_staying = new_pet_pos.is_some()); + .map(|mut activity| activity.is_pet_staying = stay); state .ecs() .write_storage::() .get_mut(pet) - .map(|s| s.stay_pos = new_pet_pos); + .map(|s| s.stay_pos = current_pet_position); } } diff --git a/server/src/events/mod.rs b/server/src/events/mod.rs index b4a965761e..1173f9267b 100644 --- a/server/src/events/mod.rs +++ b/server/src/events/mod.rs @@ -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_toggle_stay, handle_unmount, + handle_set_pet_stay, handle_sound, handle_unmount, }; use inventory_manip::handle_inventory; use invite::{handle_invite, handle_invite_response}; @@ -144,8 +144,8 @@ impl Server { handle_mount_volume(self, mounter, volume) }, ServerEvent::Unmount(mounter) => handle_unmount(self, mounter), - ServerEvent::ToggleStay(command_giver, pet) => { - handle_toggle_stay(self, command_giver, pet) + ServerEvent::SetPetStay(command_giver, pet, stay) => { + handle_set_pet_stay(self, command_giver, pet, stay) }, ServerEvent::Possess(possessor_uid, possesse_uid) => { handle_possess(self, possessor_uid, possesse_uid) diff --git a/voxygen/src/session/mod.rs b/voxygen/src/session/mod.rs index 4d6e552c83..9a4ea9dc34 100644 --- a/voxygen/src/session/mod.rs +++ b/voxygen/src/session/mod.rs @@ -13,14 +13,14 @@ use vek::*; use client::{self, Client}; use common::{ - comp, comp::{ + self, dialogue::Subject, inventory::slot::{EquipSlot, Slot}, invite::InviteKind, item::{tool::ToolKind, ItemDesc}, - ChatType, Content, InputKind, InventoryUpdateEvent, Pos, PresenceKind, Stats, - UtteranceKind, Vel, + CharacterActivity, ChatType, Content, InputKind, InventoryUpdateEvent, Pos, PresenceKind, + Stats, UtteranceKind, Vel, }, consts::MAX_MOUNT_RANGE, event::UpdateCharacterMetadata, @@ -949,6 +949,7 @@ impl PlayState for SessionState { let mut close_pet = None; if let Some(player_pos) = player_pos { + let positions = client.state().read_storage::(); close_pet = client.state().ecs().read_resource::().0 .in_circle_aabr(player_pos.0.xy(), MAX_MOUNT_RANGE) .filter(|e| @@ -962,9 +963,7 @@ impl PlayState for SessionState { client.state().ecs().read_storage::>().get(*e).is_none() ) .min_by_key(|e| { - OrderedFloat(client - .state() - .read_storage::() + OrderedFloat(positions .get(*e) .map_or(MAX_MOUNT_RANGE * MAX_MOUNT_RANGE, |x| { player_pos.0.distance_squared(x.0) @@ -972,8 +971,11 @@ impl PlayState for SessionState { )) }); } - if let Some(pet_entity)= close_pet && client.state().read_storage::>().get(pet_entity).is_none() { - client.toggle_stay(pet_entity); + if let Some(pet_entity) = close_pet && client.state().read_storage::>().get(pet_entity).is_none() { + let is_staying = client.state() + .read_component_copied::(pet_entity) + .map_or(false, |activity| activity.is_pet_staying); + client.set_pet_stay(pet_entity, !is_staying); } }, GameInput::Interact => {