clean up mounting

This commit is contained in:
Isse 2023-01-14 23:47:05 +01:00 committed by Joshua Barretto
parent a6b2f04518
commit a7588e274d
3 changed files with 18 additions and 21 deletions

View File

@ -94,6 +94,7 @@ pub fn is_mountable(mount: &Body, rider: Option<&Body>) -> bool {
| quadruped_low::Species::Elbst
| quadruped_low::Species::Tortoise
),
Body::Ship(_) => true,
_ => false,
}
}

View File

@ -1,6 +1,5 @@
use crate::{
comp,
comp::{pet::is_mountable, Body},
link::{Is, Link, LinkHandle, Role},
terrain::TerrainGrid,
uid::{Uid, UidAllocator},
@ -29,6 +28,7 @@ pub struct Mounting {
pub rider: Uid,
}
#[derive(Debug)]
pub enum MountingError {
NoSuchEntity,
NotMountable,
@ -39,7 +39,6 @@ impl Link for Mounting {
Read<'a, UidAllocator>,
WriteStorage<'a, Is<Mount>>,
WriteStorage<'a, Is<Rider>>,
WriteStorage<'a, Body>,
);
type DeleteData<'a> = (
Read<'a, UidAllocator>,
@ -60,7 +59,7 @@ impl Link for Mounting {
fn create(
this: &LinkHandle<Self>,
(uid_allocator, mut is_mounts, mut is_riders, body): Self::CreateData<'_>,
(uid_allocator, mut is_mounts, mut is_riders): Self::CreateData<'_>,
) -> Result<(), Self::Error> {
let entity = |uid: Uid| uid_allocator.retrieve_entity_internal(uid.into());
@ -68,23 +67,15 @@ impl Link for Mounting {
// Forbid self-mounting
Err(MountingError::NotMountable)
} else if let Some((mount, rider)) = entity(this.mount).zip(entity(this.rider)) {
if let Some(mount_body) = body.get(mount) {
if is_mountable(mount_body, body.get(rider)) {
let can_mount_with =
|entity| is_mounts.get(entity).is_none() && is_riders.get(entity).is_none();
let can_mount_with =
|entity| is_mounts.get(entity).is_none() && is_riders.get(entity).is_none();
// Ensure that neither mount or rider are already part of a mounting
// relationship
if can_mount_with(mount) && can_mount_with(rider) {
let _ = is_mounts.insert(mount, this.make_role());
let _ = is_riders.insert(rider, this.make_role());
Ok(())
} else {
Err(MountingError::NotMountable)
}
} else {
Err(MountingError::NotMountable)
}
// Ensure that neither mount or rider are already part of a mounting
// relationship
if can_mount_with(mount) && can_mount_with(rider) {
let _ = is_mounts.insert(mount, this.make_role());
let _ = is_riders.insert(rider, this.make_role());
Ok(())
} else {
Err(MountingError::NotMountable)
}

View File

@ -10,7 +10,7 @@ use common::{
inventory::slot::EquipSlot,
loot_owner::LootOwnerKind,
tool::ToolKind,
Inventory, LootOwner, Pos, SkillGroupKind,
Inventory, LootOwner, Pos, SkillGroupKind, pet::is_mountable,
},
consts::{MAX_MOUNT_RANGE, SOUND_TRAVEL_DIST_PER_VOLUME},
event::EventBus,
@ -119,7 +119,12 @@ pub fn handle_mount(server: &mut Server, rider: EcsEntity, mount: EcsEntity) {
Some(comp::Alignment::Owned(owner)) if *owner == rider_uid,
);
if is_pet {
let can_ride = state.ecs()
.read_storage()
.get(mount)
.map_or(false, |mount_body| is_mountable(mount_body, state.ecs().read_storage().get(rider)));
if is_pet && can_ride {
drop(uids);
drop(healths);
let _ = state.link(Mounting {