Collar can make QuadMeds mountable, state + offsets

This commit is contained in:
Snowram 2021-04-29 02:42:38 +02:00
parent e66a2079ce
commit e4f3064b8a
12 changed files with 297 additions and 5 deletions

View File

@ -105,6 +105,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Merchants now use `/tell` instead of `/say` to communicate prices
- Entities catch on fire if they stand too close to campfires
- Water extinguishes entities on fire
- Some tamed NPCs can now be mounted
### Removed

View File

@ -661,6 +661,45 @@ impl Body {
pub fn mounting_offset(&self) -> Vec3<f32> {
match self {
Body::QuadrupedMedium(quadruped_medium) => {
match (quadruped_medium.species, quadruped_medium.body_type) {
(quadruped_medium::Species::Grolgar, _) => Vec3::from([0.5, 0.5, 1.8]),
(quadruped_medium::Species::Saber, _) => Vec3::from([0.3, 0.3, 1.3]),
(quadruped_medium::Species::Tiger, _) => Vec3::from([0.2, 0.2, 1.4]),
(quadruped_medium::Species::Tuskram, _) => Vec3::from([-0.5, -0.5, 1.5]),
(quadruped_medium::Species::Lion, _) => Vec3::from([0.3, 0.3, 1.5]),
(quadruped_medium::Species::Tarasque, _) => Vec3::from([0.6, 0.6, 2.0]),
(quadruped_medium::Species::Wolf, _) => Vec3::from([0.5, 0.5, 1.3]),
(quadruped_medium::Species::Frostfang, _) => Vec3::from([0.5, 0.5, 1.2]),
(quadruped_medium::Species::Mouflon, _) => Vec3::from([0.3, 0.3, 1.2]),
(quadruped_medium::Species::Catoblepas, _) => Vec3::from([0.0, 0.0, 2.0]),
(quadruped_medium::Species::Bonerattler, _) => Vec3::from([0.5, 0.5, 1.2]),
(quadruped_medium::Species::Deer, _) => Vec3::from([0.2, 0.2, 1.3]),
(quadruped_medium::Species::Hirdrasil, _) => Vec3::from([0.0, 0.0, 1.4]),
(quadruped_medium::Species::Roshwalr, _) => Vec3::from([0.5, 0.5, 1.8]),
(quadruped_medium::Species::Donkey, _) => Vec3::from([0.5, 0.5, 1.5]),
(quadruped_medium::Species::Camel, _) => Vec3::from([-0.1, -0.1, 2.8]),
(quadruped_medium::Species::Zebra, _) => Vec3::from([0.5, 0.5, 1.8]),
(quadruped_medium::Species::Antelope, _) => Vec3::from([0.3, 0.3, 1.4]),
(quadruped_medium::Species::Kelpie, _) => Vec3::from([0.5, 0.5, 1.9]),
(quadruped_medium::Species::Horse, _) => Vec3::from([0.1, 0.1, 2.0]),
(quadruped_medium::Species::Barghest, _) => Vec3::from([0.5, 0.5, 2.2]),
(quadruped_medium::Species::Cattle, quadruped_medium::BodyType::Male) => {
Vec3::from([0.5, 0.5, 2.6])
},
(quadruped_medium::Species::Cattle, quadruped_medium::BodyType::Female) => {
Vec3::from([0.7, 0.7, 2.2])
},
(quadruped_medium::Species::Darkhound, _) => Vec3::from([0.5, 0.5, 1.4]),
(quadruped_medium::Species::Highland, _) => Vec3::from([0.5, 0.5, 2.3]),
(quadruped_medium::Species::Yak, _) => Vec3::from([0.0, 0.0, 3.0]),
(quadruped_medium::Species::Panda, _) => Vec3::from([-0.2, -0.2, 1.4]),
(quadruped_medium::Species::Bear, _) => Vec3::from([-0.4, -0.4, 2.5]),
(quadruped_medium::Species::Dreadhorn, _) => Vec3::from([0.2, 0.2, 3.5]),
(quadruped_medium::Species::Moose, _) => Vec3::from([-0.6, -0.6, 2.1]),
(quadruped_medium::Species::Snowleopard, _) => Vec3::from([-0.5, -0.5, 1.4]),
}
},
Body::Ship(ship::Body::DefaultAirship) => Vec3::from([0.0, 0.0, 10.0]),
_ => Vec3::unit_z(),
}

View File

@ -47,6 +47,7 @@ pub enum CharacterState {
Idle,
Climb(climb::Data),
Sit,
Mount,
Dance,
Talk,
Sneak,

View File

@ -19,6 +19,7 @@ pub mod glide_wield;
pub mod healing_beam;
pub mod idle;
pub mod leap_melee;
pub mod mount;
pub mod repeater_ranged;
pub mod roll;
pub mod self_buff;

View File

@ -0,0 +1,50 @@
use super::utils::*;
use crate::{
comp::{CharacterState, InventoryAction, StateUpdate},
states::behavior::{CharacterBehavior, JoinData},
};
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate::from(data);
handle_wield(data, &mut update);
handle_jump(data, &mut update, 1.0);
// Try to Fall/Stand up/Move
if !data.physics.on_ground || data.inputs.move_dir.magnitude_squared() > 0.0 {
update.character = CharacterState::Idle;
}
update
}
fn wield(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate::from(data);
attempt_wield(data, &mut update);
update
}
fn dance(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate::from(data);
attempt_dance(data, &mut update);
update
}
fn stand(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate::from(data);
// Try to Fall/Stand up/Move
update.character = CharacterState::Idle;
update
}
fn manipulate_loadout(&self, data: &JoinData, inv_action: InventoryAction) -> StateUpdate {
let mut update = StateUpdate::from(data);
handle_manipulate_loadout(&data, &mut update, inv_action);
update
}
}

View File

@ -315,6 +315,9 @@ impl<'a> System<'a> for Sys {
CharacterState::Sit => {
states::sit::Data::handle_event(&states::sit::Data, &j, action)
},
CharacterState::Mount => {
states::mount::Data::handle_event(&states::mount::Data, &j, action)
},
CharacterState::Dance => {
states::dance::Data::handle_event(&states::dance::Data, &j, action)
},
@ -352,9 +355,9 @@ impl<'a> System<'a> for Sys {
// If mounted, character state is controlled by mount
// TODO: Make mounting a state
if let Some(Mounting(_)) = read_data.mountings.get(entity) {
let sit_state = CharacterState::Sit {};
if join_struct.char_state.get_unchecked() != &sit_state {
*join_struct.char_state.get_mut_unchecked() = sit_state;
let mount_state = CharacterState::Mount {};
if join_struct.char_state.get_unchecked() != &mount_state {
*join_struct.char_state.get_mut_unchecked() = mount_state;
}
continue;
}
@ -374,6 +377,7 @@ impl<'a> System<'a> for Sys {
CharacterState::GlideWield => states::glide_wield::Data.behavior(&j),
CharacterState::Stunned(data) => data.behavior(&j),
CharacterState::Sit => states::sit::Data::behavior(&states::sit::Data, &j),
CharacterState::Mount => states::mount::Data::behavior(&states::mount::Data, &j),
CharacterState::Dance => states::dance::Data::behavior(&states::dance::Data, &j),
CharacterState::Sneak => states::sneak::Data::behavior(&states::sneak::Data, &j),
CharacterState::BasicBlock(data) => data.behavior(&j),

View File

@ -68,7 +68,14 @@ impl<'a> System<'a> for Sys {
if let (Some(pos), Some(ori), Some(vel)) = (pos, ori, vel) {
let mounting_offset =
body.map_or(Vec3::unit_z(), Body::mounting_offset);
let _ = positions.insert(mounter, Pos(pos.0 + mounting_offset));
let _ = positions.insert(
mounter,
Pos(Vec3 {
x: pos.0.x + ori.look_dir().x * mounting_offset.x,
y: pos.0.y + ori.look_dir().y * mounting_offset.y,
z: pos.0.z + mounting_offset.z,
}),
);
let _ = orientations.insert(mounter, ori);
let _ = velocities.insert(mounter, vel);
}

View File

@ -193,6 +193,7 @@ impl<'a> System<'a> for Sys {
CharacterState::Idle { .. }
| CharacterState::Talk { .. }
| CharacterState::Sit { .. }
| CharacterState::Mount { .. }
| CharacterState::Dance { .. }
| CharacterState::Sneak { .. }
| CharacterState::Glide { .. }

View File

@ -349,6 +349,20 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
.write_storage()
.insert(tameable_entity, comp::Alignment::Owned(uid));
// Make entity mountable if its Body is QuadrupedMedium
// TODO: Make mounting better so that all entities can be
// mounted?
if let Some(comp::Body::QuadrupedMedium(_)) = state
.ecs()
.read_storage::<comp::Body>()
.get(tameable_entity)
{
let _ = state.ecs().write_storage().insert(
tameable_entity,
comp::MountState::Unmounted,
);
}
// Add to group system
let clients = state.ecs().read_storage::<Client>();
let uids = state.ecs().read_storage::<Uid>();

View File

@ -12,6 +12,7 @@ pub mod gliding;
pub mod idle;
pub mod jump;
pub mod leapmelee;
pub mod mount;
pub mod repeater;
pub mod roll;
pub mod run;
@ -35,7 +36,7 @@ pub use self::{
chargeswing::ChargeswingAnimation, climb::ClimbAnimation, dance::DanceAnimation,
dash::DashAnimation, equip::EquipAnimation, glidewield::GlideWieldAnimation,
gliding::GlidingAnimation, idle::IdleAnimation, jump::JumpAnimation, leapmelee::LeapAnimation,
repeater::RepeaterAnimation, roll::RollAnimation, run::RunAnimation,
mount::MountAnimation, repeater::RepeaterAnimation, roll::RollAnimation, run::RunAnimation,
shockwave::ShockwaveAnimation, shoot::ShootAnimation, sit::SitAnimation, sneak::SneakAnimation,
spin::SpinAnimation, spinmelee::SpinMeleeAnimation, staggered::StaggeredAnimation,
stand::StandAnimation, stunned::StunnedAnimation, swim::SwimAnimation,

View File

@ -0,0 +1,155 @@
use super::{
super::{vek::*, Animation},
CharacterSkeleton, SkeletonAttr,
};
use common::comp::item::ToolKind;
use std::{f32::consts::PI, ops::Mul};
pub struct MountAnimation;
impl Animation for MountAnimation {
#[allow(clippy::type_complexity)]
type Dependency = (
Option<ToolKind>,
Option<ToolKind>,
f32,
Vec3<f32>,
Vec3<f32>,
Vec3<f32>,
Vec3<f32>,
);
type Skeleton = CharacterSkeleton;
#[cfg(feature = "use-dyn-lib")]
const UPDATE_FN: &'static [u8] = b"character_mount\0";
#[cfg_attr(feature = "be-dyn-lib", export_name = "character_mount")]
fn update_skeleton_inner(
skeleton: &Self::Skeleton,
(
_active_tool_kind,
_second_tool_kind,
global_time,
velocity,
avg_vel,
orientation,
last_ori,
): Self::Dependency,
anim_time: f32,
_rate: &mut f32,
s_a: &SkeletonAttr,
) -> Self::Skeleton {
let mut next = (*skeleton).clone();
let slow = (anim_time * 1.0).sin();
let slowa = (anim_time * 1.0 + PI / 2.0).sin();
let fast = (anim_time * 12.0).sin();
let stop = (anim_time * 3.0).min(PI / 2.0).sin();
let head_look = Vec2::new(
(global_time * 0.05 + anim_time / 15.0)
.floor()
.mul(7331.0)
.sin()
* 0.25,
(global_time * 0.05 + anim_time / 15.0)
.floor()
.mul(1337.0)
.sin()
* 0.125,
);
let ori: Vec2<f32> = Vec2::from(orientation);
let last_ori = Vec2::from(last_ori);
let speed = (Vec2::<f32>::from(velocity).magnitude()).min(24.0);
let canceler = (speed / 24.0).powf(0.6);
let x_tilt = avg_vel.z.atan2(avg_vel.xy().magnitude()) * canceler;
let tilt = if ::vek::Vec2::new(ori, last_ori)
.map(|o| o.magnitude_squared())
.map(|m| m > 0.001 && m.is_finite())
.reduce_and()
&& ori.angle_between(last_ori).is_finite()
{
ori.angle_between(last_ori).min(0.2)
* last_ori.determine_side(Vec2::zero(), ori).signum()
} else {
0.0
} * 1.3;
next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1 + slow * 0.1 + stop * -0.8);
next.head.orientation = Quaternion::rotation_z(head_look.x + slow * 0.2 - slow * 0.1)
* Quaternion::rotation_x((0.4 + slowa * -0.1 + slow * 0.1 + head_look.y).abs());
next.chest.position = Vec3::new(
0.0,
s_a.chest.0 + stop * -0.4,
s_a.chest.1
+ slow * 0.1
+ stop * -0.8
+ fast * (velocity.xy().magnitude() / 10.0).min(1.7),
);
next.chest.orientation =
Quaternion::rotation_x(-0.6 + stop * 0.15 + x_tilt * (canceler * 6.0).min(2.0))
* Quaternion::rotation_y(tilt * 3.1);
next.belt.position = Vec3::new(0.0, s_a.belt.0 + stop * 1.2, s_a.belt.1);
next.belt.orientation = Quaternion::rotation_x(stop * 0.3);
next.back.position = Vec3::new(0.0, s_a.back.0, s_a.back.1);
next.shorts.position = Vec3::new(0.0, s_a.shorts.0 + stop * 2.5, s_a.shorts.1 + stop * 0.6);
next.shorts.orientation = Quaternion::rotation_x(stop * 0.6);
next.hand_l.position = Vec3::new(
-s_a.hand.0 + 4.0,
s_a.hand.1 + slowa * 0.15 + stop * 8.0,
s_a.hand.2 + slow * 0.7 + stop * 4.0,
);
next.hand_l.orientation =
Quaternion::rotation_x(PI / 2.0) * Quaternion::rotation_z(-PI / 2.0);
next.hand_r.position = Vec3::new(
s_a.hand.0 - 4.0,
s_a.hand.1 + slowa * 0.15 + stop * 8.0,
s_a.hand.2 + slow * 0.7 + stop * 4.0,
);
next.hand_r.orientation =
Quaternion::rotation_x(PI / 2.0) * Quaternion::rotation_z(PI / 2.0);
next.foot_l.position = Vec3::new(-s_a.foot.0 - 2.0, 4.0 + s_a.foot.1, s_a.foot.2);
next.foot_l.orientation = Quaternion::rotation_x(slow * 0.1 + stop * 0.4 + slow * 0.1);
next.foot_r.position = Vec3::new(s_a.foot.0 + 2.0, 4.0 + s_a.foot.1, s_a.foot.2);
next.foot_r.orientation = Quaternion::rotation_x(slowa * 0.1 + stop * 0.4 + slowa * 0.1);
next.shoulder_l.position = Vec3::new(-s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
next.shoulder_l.orientation = Quaternion::rotation_x(0.0);
next.shoulder_r.position = Vec3::new(s_a.shoulder.0, s_a.shoulder.1, s_a.shoulder.2);
next.shoulder_r.orientation = Quaternion::rotation_x(0.0);
next.torso.position = Vec3::new(0.0, -0.2, stop * -0.16) * s_a.scaler;
if skeleton.holding_lantern {
next.hand_r.position = Vec3::new(
s_a.hand.0 + 1.0 - head_look.x * 8.0,
s_a.hand.1 + 5.0 + head_look.x * 6.0,
s_a.hand.2 + 9.0 + head_look.y * 6.0,
);
next.hand_r.orientation = Quaternion::rotation_x(2.25)
* Quaternion::rotation_z(0.9)
* Quaternion::rotation_y(head_look.x * 3.0)
* Quaternion::rotation_x(head_look.y * 3.0);
let fast = (anim_time * 5.0).sin();
let fast2 = (anim_time * 4.5 + 8.0).sin();
next.lantern.position = Vec3::new(-0.5, -0.5, -2.5);
next.lantern.orientation = next.hand_r.orientation.inverse()
* Quaternion::rotation_x(fast * 0.1)
* Quaternion::rotation_y(fast2 * 0.1);
}
next
}
}

View File

@ -1505,6 +1505,24 @@ impl FigureMgr {
skeleton_attr,
)
},
CharacterState::Mount { .. } => {
anim::character::MountAnimation::update_skeleton(
&target_base,
(
active_tool_kind,
second_tool_kind,
time,
rel_vel,
rel_avg_vel,
// TODO: Update to use the quaternion.
ori * anim::vek::Vec3::<f32>::unit_y(),
state.last_ori * anim::vek::Vec3::<f32>::unit_y(),
),
state.state_time,
&mut state_animation_rate,
skeleton_attr,
)
},
CharacterState::GlideWield { .. } => {
anim::character::GlideWieldAnimation::update_skeleton(
&target_base,