veloren_voxygen_anim/character/
crawl.rs

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