This commit is contained in:
Maxicarlos08 2023-08-14 14:40:43 +02:00
parent b7c56c9057
commit 13dbf679c5
No known key found for this signature in database
7 changed files with 31 additions and 29 deletions

View File

@ -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,
)));
}
}

View File

@ -146,7 +146,7 @@ pub enum ControlEvent {
Mount(Uid),
MountVolume(VolumePos),
Unmount,
ToggleStay(Uid),
SetPetStay(Uid, bool),
InventoryEvent(InventoryEvent),
GroupManip(GroupManip),
RemoveBuff(BuffKind),

View File

@ -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 {

View File

@ -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) => {

View File

@ -210,7 +210,12 @@ 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, 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::<Pos>();
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::<comp::Agent>()
.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::<Is<Mount>>().get(pet).is_none()
@ -243,12 +241,12 @@ pub fn handle_toggle_stay(server: &mut Server, command_giver: EcsEntity, pet: Ec
.ecs()
.write_storage::<comp::CharacterActivity>()
.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::<comp::Agent>()
.get_mut(pet)
.map(|s| s.stay_pos = new_pet_pos);
.map(|s| s.stay_pos = current_pet_position);
}
}

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_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)

View File

@ -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::<Pos>();
close_pet = client.state().ecs().read_resource::<CachedSpatialGrid>().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::<Is<Mount>>().get(*e).is_none()
)
.min_by_key(|e| {
OrderedFloat(client
.state()
.read_storage::<Pos>()
OrderedFloat(positions
.get(*e)
.map_or(MAX_MOUNT_RANGE * MAX_MOUNT_RANGE, |x| {
player_pos.0.distance_squared(x.0)
@ -973,7 +972,10 @@ impl PlayState for SessionState {
});
}
if let Some(pet_entity) = close_pet && client.state().read_storage::<Is<Mount>>().get(pet_entity).is_none() {
client.toggle_stay(pet_entity);
let is_staying = client.state()
.read_component_copied::<CharacterActivity>(pet_entity)
.map_or(false, |activity| activity.is_pet_staying);
client.set_pet_stay(pet_entity, !is_staying);
}
},
GameInput::Interact => {