veloren_voxygen_anim/character/
sneak.rs

1use super::{
2    super::{Animation, vek::*},
3    CharacterSkeleton, SkeletonAttr,
4};
5use common::comp::item::ToolKind;
6use std::{f32::consts::PI, ops::Mul};
7
8pub struct SneakAnimation;
9
10impl Animation for SneakAnimation {
11    type Dependency<'a> = (Option<ToolKind>, Vec3<f32>, Vec3<f32>, Vec3<f32>, f32);
12    type Skeleton = CharacterSkeleton;
13
14    #[cfg(feature = "use-dyn-lib")]
15    const UPDATE_FN: &'static [u8] = b"character_sneak\0";
16
17    #[cfg_attr(feature = "be-dyn-lib", export_name = "character_sneak")]
18
19    fn update_skeleton_inner(
20        skeleton: &Self::Skeleton,
21        (_active_tool_kind, velocity, orientation, last_ori, global_time): Self::Dependency<'_>,
22        anim_time: f32,
23        rate: &mut f32,
24        s_a: &SkeletonAttr,
25    ) -> Self::Skeleton {
26        let mut next = (*skeleton).clone();
27        let speed = Vec2::<f32>::from(velocity).magnitude();
28        *rate = 1.0;
29        let slow = (anim_time * 3.0).sin();
30        let breathe = ((anim_time * 0.5).sin()).abs();
31        let walkintensity = if speed > 5.0 { 1.0 } else { 0.45 };
32        let lower = if speed > 5.0 { 0.0 } else { 1.0 };
33        let _snapfoot = if speed > 5.0 { 1.1 } else { 2.0 };
34        let lab: f32 = 1.0;
35        let foothoril = (anim_time * 7.0 * lab + PI * 1.45).sin();
36        let foothorir = (anim_time * 7.0 * lab + PI * (0.45)).sin();
37
38        let footvertl = (anim_time * 7.0 * lab).sin();
39        let footvertr = (anim_time * 7.0 * lab + PI).sin();
40
41        let footrotl = ((5.0 / (2.5 + (2.5) * ((anim_time * 7.0 * lab + PI * 1.4).sin()).powi(2)))
42            .sqrt())
43            * ((anim_time * 7.0 * lab + PI * 1.4).sin());
44
45        let footrotr = ((5.0 / (1.0 + (4.0) * ((anim_time * 7.0 * lab + PI * 0.4).sin()).powi(2)))
46            .sqrt())
47            * ((anim_time * 7.0 * lab + PI * 0.4).sin());
48
49        let short = (anim_time * lab * 7.0).sin();
50        let noisea = (anim_time * 11.0 + PI / 6.0).sin();
51        let noiseb = (anim_time * 19.0 + PI / 4.0).sin();
52
53        let shorte = ((5.0 / (4.0 + 1.0 * ((anim_time * lab * 7.0).sin()).powi(2))).sqrt())
54            * ((anim_time * lab * 7.0).sin());
55
56        let shortalt = (anim_time * lab * 7.0 + PI / 2.0).sin();
57
58        let head_look = Vec2::new(
59            (global_time + anim_time / 18.0).floor().mul(7331.0).sin() * 0.2,
60            (global_time + anim_time / 18.0).floor().mul(1337.0).sin() * 0.1,
61        );
62
63        let orientation: Vec2<f32> = Vec2::from(orientation);
64        let last_ori = Vec2::from(last_ori);
65        let tilt = if vek::Vec2::new(orientation, last_ori)
66            .map(|o| o.magnitude_squared())
67            .map(|m| m > 0.001 && m.is_finite())
68            .reduce_and()
69            && orientation.angle_between(last_ori).is_finite()
70        {
71            orientation.angle_between(last_ori).min(0.2)
72                * last_ori.determine_side(Vec2::zero(), orientation).signum()
73        } else {
74            0.0
75        } * 1.3;
76
77        next.hold.scale = Vec3::one() * 0.0;
78
79        if speed > 0.5 {
80            next.hand_l.position = Vec3::new(1.0 - s_a.hand.0, 4.0 + s_a.hand.1, 1.0 + s_a.hand.2);
81            next.hand_l.orientation = Quaternion::rotation_x(1.0);
82
83            next.hand_r.position = Vec3::new(-1.0 + s_a.hand.0, -1.0 + s_a.hand.1, s_a.hand.2);
84            next.hand_r.orientation = Quaternion::rotation_x(0.4);
85            next.head.position = Vec3::new(0.0, 1.0 + s_a.head.0, -1.0 + s_a.head.1 + short * 0.06);
86            next.head.orientation =
87                Quaternion::rotation_z(tilt * -2.5 + head_look.x * 0.2 - short * 0.06)
88                    * Quaternion::rotation_x(head_look.y + 0.45);
89
90            next.chest.position = Vec3::new(0.0, s_a.chest.0, -1.0 + s_a.chest.1 + shortalt * -0.5);
91            next.chest.orientation = Quaternion::rotation_z(0.3 + short * 0.08 + tilt * -0.2)
92                * Quaternion::rotation_y(tilt * 0.8)
93                * Quaternion::rotation_x(-0.5);
94
95            next.belt.position = Vec3::new(0.0, 0.5 + s_a.belt.0, 0.7 + s_a.belt.1);
96            next.belt.orientation = Quaternion::rotation_z(short * 0.1 + tilt * -1.1)
97                * Quaternion::rotation_y(tilt * 0.5)
98                * Quaternion::rotation_x(0.2);
99
100            next.back.orientation =
101                Quaternion::rotation_x(-0.25 + short * 0.1 + noisea * 0.1 + noiseb * 0.1);
102
103            next.shorts.position = Vec3::new(0.0, 1.0 + s_a.shorts.0, 1.0 + s_a.shorts.1);
104            next.shorts.orientation = Quaternion::rotation_z(short * 0.16 + tilt * -1.5)
105                * Quaternion::rotation_y(tilt * 0.7)
106                * Quaternion::rotation_x(0.3);
107
108            next.foot_l.position = Vec3::new(
109                -s_a.foot.0,
110                s_a.foot.1 + foothoril * -10.5 * walkintensity - lower * 1.0,
111                1.0 + s_a.foot.2 + ((footvertl * -1.7).max(-1.0)) * walkintensity,
112            );
113            next.foot_l.orientation =
114                Quaternion::rotation_x(-0.2 + footrotl * -0.8 * walkintensity)
115                    * Quaternion::rotation_y(tilt * 1.8);
116
117            next.foot_r.position = Vec3::new(
118                s_a.foot.0,
119                s_a.foot.1 + foothorir * -10.5 * walkintensity - lower * 1.0,
120                1.0 + s_a.foot.2 + ((footvertr * -1.7).max(-1.0)) * walkintensity,
121            );
122            next.foot_r.orientation =
123                Quaternion::rotation_x(-0.2 + footrotr * -0.8 * walkintensity)
124                    * Quaternion::rotation_y(tilt * 1.8);
125
126            next.shoulder_l.orientation = Quaternion::rotation_x(short * 0.15 * walkintensity);
127
128            next.shoulder_r.orientation = Quaternion::rotation_x(short * -0.15 * walkintensity);
129
130            next.lantern.orientation =
131                Quaternion::rotation_x(shorte * 0.2 + 0.4) * Quaternion::rotation_y(shorte * 0.1);
132        } else {
133            next.head.position = Vec3::new(
134                0.0,
135                1.0 + s_a.head.0,
136                -2.0 + s_a.head.1 + slow * 0.1 + breathe * -0.05,
137            );
138            next.head.orientation = Quaternion::rotation_z(head_look.x)
139                * Quaternion::rotation_x(0.6 + head_look.y.abs());
140
141            next.chest.position = Vec3::new(0.0, s_a.chest.0, -3.0 + s_a.chest.1 + slow * 0.1);
142            next.chest.orientation = Quaternion::rotation_x(-0.7);
143
144            next.belt.position = Vec3::new(0.0, s_a.belt.0, s_a.belt.1);
145            next.belt.orientation = Quaternion::rotation_z(0.3 + head_look.x * -0.1);
146
147            next.hand_l.position = Vec3::new(1.0 - s_a.hand.0, 5.0 + s_a.hand.1, 0.0 + s_a.hand.2);
148            next.hand_l.orientation = Quaternion::rotation_x(1.35);
149
150            next.hand_r.position = Vec3::new(-1.0 + s_a.hand.0, s_a.hand.1, s_a.hand.2);
151            next.hand_r.orientation = Quaternion::rotation_x(0.4);
152
153            next.shorts.position = Vec3::new(0.0, s_a.shorts.0, s_a.shorts.1);
154            next.shorts.orientation = Quaternion::rotation_z(0.6 + head_look.x * -0.2);
155
156            next.foot_l.position = Vec3::new(-s_a.foot.0, -6.0 + s_a.foot.1, 1.0 + s_a.foot.2);
157            next.foot_l.orientation = Quaternion::rotation_x(-0.5);
158
159            next.foot_r.position = Vec3::new(s_a.foot.0, 4.0 + s_a.foot.1, s_a.foot.2);
160        }
161
162        next.do_hold_lantern(s_a, anim_time, anim_time, speed / 10.0, 0.0, tilt);
163
164        next
165    }
166}