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