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