veloren_voxygen_anim/bird_medium/
dash.rs

1use super::{
2    super::{Animation, vek::*},
3    BirdMediumSkeleton, SkeletonAttr,
4};
5use common::states::utils::StageSection;
6use std::f32::consts::PI;
7
8pub struct DashAnimation;
9type DashAnimationDependency<'a> = (
10    Vec3<f32>,
11    Vec3<f32>,
12    Vec3<f32>,
13    f32,
14    Option<StageSection>,
15    f32,
16    f32,
17);
18
19impl Animation for DashAnimation {
20    type Dependency<'a> = DashAnimationDependency<'a>;
21    type Skeleton = BirdMediumSkeleton;
22
23    #[cfg(feature = "use-dyn-lib")]
24    const UPDATE_FN: &'static [u8] = b"bird_medium_dash\0";
25
26    #[cfg_attr(feature = "be-dyn-lib", export_name = "bird_medium_dash")]
27    fn update_skeleton_inner<'a>(
28        skeleton: &Self::Skeleton,
29        (velocity, orientation, last_ori, acc_vel, stage_section, global_time, timer): Self::Dependency<'_>,
30        anim_time: f32,
31        rate: &mut f32,
32        s_a: &SkeletonAttr,
33    ) -> Self::Skeleton {
34        let mut next = (*skeleton).clone();
35        let speed = (Vec2::<f32>::from(velocity).magnitude()).min(22.0);
36        *rate = 1.0;
37
38        let (movement1base, chargemovementbase, movement2base, movement3, legtell) =
39            match stage_section {
40                Some(StageSection::Buildup) => (anim_time.sqrt(), 0.0, 0.0, 0.0, anim_time),
41                Some(StageSection::Charge) => (1.0, 1.0, 0.0, 0.0, 0.0),
42                Some(StageSection::Action) => (1.0, 0.0, anim_time.powi(4), 0.0, 1.0),
43                Some(StageSection::Recover) => (1.0, 0.0, 1.0, anim_time, 1.0),
44                _ => (0.0, 0.0, 0.0, 0.0, 0.0),
45            };
46        let pullback = 1.0 - movement3;
47        let subtract = global_time - timer;
48        let check = subtract - subtract.trunc();
49        let mirror = (check - 0.5).signum();
50        let movement1abs = movement1base * pullback;
51        let movement2abs = movement2base * pullback;
52        let legtwitch = (legtell * 6.0).sin() * pullback;
53        let legswing = legtell * pullback;
54        let chargeanim = (chargemovementbase * anim_time * 15.0).sin();
55
56        //let speednorm = speed / 13.0;
57        let speednorm = (speed / 13.0).powf(0.25);
58
59        let speedmult = 0.8;
60        let lab: f32 = 0.6; //6
61
62        // acc_vel and anim_time mix to make sure phase length isn't starting at
63        // +infinite
64        let mixed_vel = acc_vel + anim_time * 5.0; //sets run frequency using speed, with anim_time setting a floor
65
66        let short = ((1.0
67            / (0.72
68                + 0.28 * ((mixed_vel * 1.0 * lab * speedmult + PI * -0.15 - 0.5).sin()).powi(2)))
69        .sqrt())
70            * ((mixed_vel * 1.0 * lab * speedmult + PI * -0.15 - 0.5).sin())
71            * speednorm;
72
73        //
74        let shortalt = (mixed_vel * 1.0 * lab * speedmult + PI * 3.0 / 8.0 - 0.5).sin() * speednorm;
75
76        let ori: Vec2<f32> = Vec2::from(orientation);
77        let last_ori = Vec2::from(last_ori);
78        let tilt = if vek::Vec2::new(ori, last_ori)
79            .map(|o| o.magnitude_squared())
80            .map(|m| m > 0.001 && m.is_finite())
81            .reduce_and()
82            && ori.angle_between(last_ori).is_finite()
83        {
84            ori.angle_between(last_ori).min(0.2)
85                * last_ori.determine_side(Vec2::zero(), ori).signum()
86        } else {
87            0.0
88        } * 1.3;
89
90        next.head.scale = Vec3::one() * 0.98;
91        next.leg_l.scale = Vec3::one() * 0.98;
92        next.leg_r.scale = Vec3::one() * 0.98;
93
94        next.head.position = Vec3::new(0.0, s_a.head.0, s_a.head.1);
95        next.head.orientation = Quaternion::rotation_x(
96            -0.1 * speednorm + short * -0.05 + movement1abs * -0.8 + movement2abs * 0.2,
97        ) * Quaternion::rotation_y(tilt * 0.2)
98            * Quaternion::rotation_z(shortalt * -0.05 - tilt * 1.5);
99
100        next.chest.position = Vec3::new(
101            0.0,
102            s_a.chest.0,
103            s_a.chest.1 + short * 0.5 + 0.5 * speednorm,
104        );
105        next.chest.orientation =
106            Quaternion::rotation_x(short * 0.07 + movement1abs * 0.8 + movement2abs * -1.2)
107                * Quaternion::rotation_y(tilt * 0.8)
108                * Quaternion::rotation_z(shortalt * 0.10);
109
110        next.tail.position = Vec3::new(0.0, s_a.tail.0, s_a.tail.1);
111        next.tail.orientation =
112            Quaternion::rotation_x(0.6 + short * -0.02 + movement1abs * -0.8 + movement2abs * 0.8);
113
114        next.wing_in_l.position = Vec3::new(-s_a.wing_in.0, s_a.wing_in.1, s_a.wing_in.2);
115        next.wing_in_r.position = Vec3::new(s_a.wing_in.0, s_a.wing_in.1, s_a.wing_in.2);
116
117        next.wing_in_l.orientation =
118            Quaternion::rotation_y(
119                -0.8 + movement1abs * 1.0 + chargeanim * 0.2 - movement2abs * 0.6,
120            ) * Quaternion::rotation_z(0.2 - movement1abs * 0.6 - movement2abs * 0.6);
121        next.wing_in_r.orientation =
122            Quaternion::rotation_y(
123                0.8 - movement1abs * 1.0 - chargeanim * 0.2 + movement2abs * 0.6,
124            ) * Quaternion::rotation_z(-0.2 + movement1abs * 0.6 + movement2abs * 0.6);
125
126        next.wing_out_l.position = Vec3::new(-s_a.wing_out.0, s_a.wing_out.1, s_a.wing_out.2);
127        next.wing_out_r.position = Vec3::new(s_a.wing_out.0, s_a.wing_out.1, s_a.wing_out.2);
128        next.wing_out_l.orientation =
129            Quaternion::rotation_y(-0.2 + short * 0.05) * Quaternion::rotation_z(0.2);
130        next.wing_out_r.orientation =
131            Quaternion::rotation_y(0.2 + short * -0.05) * Quaternion::rotation_z(-0.2);
132
133        if legtell > 0.0 {
134            if mirror.is_sign_positive() {
135                next.leg_l.orientation = Quaternion::rotation_x(legswing * 1.1 + legtwitch * 0.5);
136
137                next.leg_r.orientation = Quaternion::rotation_x(0.0);
138            } else {
139                next.leg_l.orientation = Quaternion::rotation_x(0.0);
140
141                next.leg_r.orientation = Quaternion::rotation_x(legswing * 1.1 + legtwitch * 0.5);
142            }
143        }
144
145        next
146    }
147}