veloren_voxygen_anim/character/
roll.rs

1use super::{
2    super::{Animation, vek::*},
3    CharacterSkeleton, SkeletonAttr,
4};
5use common::{
6    comp::item::{Hands, ToolKind},
7    states::utils::StageSection,
8    util::Dir,
9};
10use std::f32::consts::PI;
11
12pub struct RollAnimation;
13
14type RollAnimationDependency = (
15    Option<ToolKind>,
16    Option<ToolKind>,
17    (Option<Hands>, Option<Hands>),
18    bool,
19    Vec3<f32>,
20    Vec3<f32>,
21    f32,
22    Option<StageSection>,
23    Option<Dir>,
24);
25
26impl Animation for RollAnimation {
27    type Dependency<'a> = RollAnimationDependency;
28    type Skeleton = CharacterSkeleton;
29
30    #[cfg(feature = "use-dyn-lib")]
31    const UPDATE_FN: &'static [u8] = b"character_roll\0";
32
33    #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "character_roll"))]
34    fn update_skeleton_inner(
35        skeleton: &Self::Skeleton,
36        (
37            active_tool_kind,
38            second_tool_kind,
39            hands,
40            wield_status,
41            orientation,
42            last_ori,
43            _global_time,
44            stage_section,
45            prev_aimed_dir,
46        ): Self::Dependency<'_>,
47        anim_time: f32,
48        rate: &mut f32,
49        s_a: &SkeletonAttr,
50    ) -> Self::Skeleton {
51        *rate = 1.0;
52        let mut next = (*skeleton).clone();
53
54        let ori: Vec2<f32> = Vec2::from(orientation);
55        let last_ori = Vec2::from(last_ori);
56        let tilt = if vek::Vec2::new(ori, last_ori)
57            .map(|o| o.magnitude_squared())
58            .map(|m| m > 0.0001 && m.is_finite())
59            .reduce_and()
60            && ori.angle_between(last_ori).is_finite()
61        {
62            ori.angle_between(last_ori).min(0.05)
63                * last_ori.determine_side(Vec2::zero(), ori).signum()
64        } else {
65            0.0
66        };
67
68        let (movement1base, movement2, movement3) = match stage_section {
69            Some(StageSection::Buildup) => (anim_time, 0.0, 0.0),
70            Some(StageSection::Movement) => (1.0, anim_time, 0.0),
71            Some(StageSection::Recover) => (1.0, 1.0, anim_time),
72            _ => (0.0, 0.0, 0.0),
73        };
74        let pullback = 1.0 - movement3;
75        let movement1 = movement1base * pullback;
76
77        if wield_status {
78            next.main.position = Vec3::new(0.0, 0.0, 0.0);
79            next.main.orientation = Quaternion::rotation_x(0.0);
80            next.second.position = Vec3::new(0.0, 0.0, 0.0);
81            next.second.orientation = Quaternion::rotation_z(0.0);
82            match hands {
83                (Some(Hands::Two), _) | (None, Some(Hands::Two)) => match active_tool_kind {
84                    Some(ToolKind::Sword) => {
85                        next.hand_l.position = Vec3::new(s_a.shl.0, s_a.shl.1, s_a.shl.2);
86                        next.hand_l.orientation =
87                            Quaternion::rotation_x(s_a.shl.3) * Quaternion::rotation_y(s_a.shl.4);
88                        next.hand_r.position = Vec3::new(s_a.shr.0, s_a.shr.1, s_a.shr.2);
89                        next.hand_r.orientation =
90                            Quaternion::rotation_x(s_a.shr.3) * Quaternion::rotation_y(s_a.shr.4);
91
92                        next.control.position = Vec3::new(s_a.sc.0, s_a.sc.1, s_a.sc.2);
93                        next.control.orientation = Quaternion::rotation_x(s_a.sc.3);
94                    },
95                    Some(ToolKind::Axe) => {
96                        next.hand_l.position = Vec3::new(s_a.ahl.0, s_a.ahl.1, s_a.ahl.2);
97                        next.hand_l.orientation =
98                            Quaternion::rotation_x(s_a.ahl.3) * Quaternion::rotation_y(s_a.ahl.4);
99                        next.hand_r.position = Vec3::new(s_a.ahr.0, s_a.ahr.1, s_a.ahr.2);
100                        next.hand_r.orientation =
101                            Quaternion::rotation_x(s_a.ahr.3) * Quaternion::rotation_z(s_a.ahr.5);
102
103                        next.control.position = Vec3::new(s_a.ac.0, s_a.ac.1, s_a.ac.2);
104                        next.control.orientation = Quaternion::rotation_x(s_a.ac.3)
105                            * Quaternion::rotation_y(s_a.ac.4)
106                            * Quaternion::rotation_z(s_a.ac.5);
107                    },
108                    Some(
109                        ToolKind::Hammer | ToolKind::Pick | ToolKind::Shovel | ToolKind::Instrument,
110                    ) => {
111                        next.hand_l.position = Vec3::new(s_a.hhl.0, s_a.hhl.1, s_a.hhl.2);
112                        next.hand_l.orientation = Quaternion::rotation_x(s_a.hhl.3)
113                            * Quaternion::rotation_y(s_a.hhl.4)
114                            * Quaternion::rotation_z(s_a.hhl.5);
115                        next.hand_r.position = Vec3::new(s_a.hhr.0, s_a.hhr.1, s_a.hhr.2);
116                        next.hand_r.orientation = Quaternion::rotation_x(s_a.hhr.3)
117                            * Quaternion::rotation_y(s_a.hhr.4)
118                            * Quaternion::rotation_z(s_a.hhr.5);
119
120                        next.control.position = Vec3::new(s_a.hc.0, s_a.hc.1, s_a.hc.2);
121                        next.control.orientation = Quaternion::rotation_x(s_a.hc.3)
122                            * Quaternion::rotation_y(s_a.hc.4)
123                            * Quaternion::rotation_z(s_a.hc.5);
124                    },
125                    Some(ToolKind::Staff) | Some(ToolKind::Sceptre) => {
126                        next.hand_r.position = Vec3::new(s_a.sthr.0, s_a.sthr.1, s_a.sthr.2);
127                        next.hand_r.orientation =
128                            Quaternion::rotation_x(s_a.sthr.3) * Quaternion::rotation_y(s_a.sthr.4);
129
130                        next.control.position = Vec3::new(s_a.stc.0, s_a.stc.1, s_a.stc.2);
131
132                        next.hand_l.position = Vec3::new(s_a.sthl.0, s_a.sthl.1, s_a.sthl.2);
133                        next.hand_l.orientation = Quaternion::rotation_x(s_a.sthl.3);
134
135                        next.control.orientation = Quaternion::rotation_x(s_a.stc.3)
136                            * Quaternion::rotation_y(s_a.stc.4)
137                            * Quaternion::rotation_z(s_a.stc.5);
138                    },
139                    Some(ToolKind::Bow) => {
140                        next.hand_l.position = Vec3::new(s_a.bhl.0, s_a.bhl.1, s_a.bhl.2);
141                        next.hand_l.orientation = Quaternion::rotation_x(s_a.bhl.3);
142                        next.hand_r.position = Vec3::new(s_a.bhr.0, s_a.bhr.1, s_a.bhr.2);
143                        next.hand_r.orientation = Quaternion::rotation_x(s_a.bhr.3);
144
145                        next.hold.position = Vec3::new(0.0, -1.0, -5.2);
146                        next.hold.orientation = Quaternion::rotation_x(-PI / 2.0);
147                        next.hold.scale = Vec3::one() * 1.0;
148
149                        next.control.position = Vec3::new(s_a.bc.0, s_a.bc.1, s_a.bc.2);
150                        next.control.orientation =
151                            Quaternion::rotation_y(s_a.bc.4) * Quaternion::rotation_z(s_a.bc.5);
152                    },
153                    Some(ToolKind::Debug) => {
154                        next.hand_l.position = Vec3::new(-7.0, 4.0, 3.0);
155                        next.hand_l.orientation = Quaternion::rotation_x(1.27);
156                        next.main.position = Vec3::new(-5.0, 5.0, 23.0);
157                        next.main.orientation = Quaternion::rotation_x(PI);
158                    },
159                    Some(ToolKind::Farming) => {
160                        next.hand_l.position = Vec3::new(9.0, 1.0, 1.0);
161                        next.hand_l.orientation = Quaternion::rotation_x(PI / 2.0);
162                        next.hand_r.position = Vec3::new(9.0, 1.0, 11.0);
163                        next.hand_r.orientation = Quaternion::rotation_x(PI / 2.0);
164                        next.main.position = Vec3::new(7.5, 7.5, 13.2);
165                        next.main.orientation = Quaternion::rotation_y(PI);
166
167                        next.control.position = Vec3::new(-11.0, 1.8, 4.0);
168                    },
169                    Some(ToolKind::Shield) => {
170                        next.hand_l.position = Vec3::new(0.0, -1.5, 0.0);
171                        next.hand_l.orientation = Quaternion::rotation_x(PI / 2.0);
172
173                        next.hand_r.position = Vec3::new(0.0, 0.0, 0.0);
174                        next.hand_r.orientation =
175                            Quaternion::rotation_x(PI / 2.0) * Quaternion::rotation_y(2.0);
176
177                        next.control.position = Vec3::new(0.0, 7.0, 4.0);
178                        next.control.orientation =
179                            Quaternion::rotation_y(-0.5) * Quaternion::rotation_z(-1.25);
180                    },
181                    _ => {},
182                },
183                (_, _) => {},
184            };
185            match hands {
186                (Some(Hands::One), _) => {
187                    next.control_l.position = Vec3::new(-7.0, 8.0, 2.0);
188                    next.control_l.orientation = Quaternion::rotation_x(-0.3);
189                    next.hand_l.position = Vec3::new(0.0, -0.5, 0.0);
190                    next.hand_l.orientation = Quaternion::rotation_x(PI / 2.0)
191                },
192                (_, _) => {},
193            };
194            match hands {
195                (None | Some(Hands::One), Some(Hands::One)) => {
196                    next.control_r.position = Vec3::new(7.0, 8.0, 2.0);
197                    next.control_r.orientation = Quaternion::rotation_x(-0.3);
198                    next.hand_r.position = Vec3::new(0.0, -0.5, 0.0);
199                    next.hand_r.orientation = Quaternion::rotation_x(PI / 2.0)
200                },
201                (_, _) => {},
202            };
203            match hands {
204                (None, None) | (None, Some(Hands::One)) => {
205                    next.hand_l.position = Vec3::new(-4.5, 8.0, 5.0);
206                    next.hand_l.orientation =
207                        Quaternion::rotation_x(1.9) * Quaternion::rotation_y(-0.5)
208                },
209                (_, _) => {},
210            };
211            match hands {
212                (None, None) | (Some(Hands::One), None) => {
213                    next.hand_r.position = Vec3::new(4.5, 8.0, 5.0);
214                    next.hand_r.orientation =
215                        Quaternion::rotation_x(1.9) * Quaternion::rotation_y(0.5)
216                },
217                (_, _) => {},
218            }
219        } else {
220            next.do_tools_on_back(hands, active_tool_kind, second_tool_kind);
221        }
222        next.head.position = Vec3::new(
223            0.0,
224            s_a.head.0 + 1.5 * movement1,
225            s_a.head.1 - 1.0 * movement1,
226        );
227        next.head.orientation = if prev_aimed_dir.is_some() {
228            Quaternion::identity()
229        } else {
230            Quaternion::rotation_x(-0.3 * movement1) * Quaternion::rotation_y(-0.4)
231        };
232
233        next.chest.position = Vec3::new(0.0, s_a.chest.0, -9.5 * movement1 + s_a.chest.1);
234
235        next.belt.position = Vec3::new(
236            0.0,
237            s_a.belt.0 + 1.0 * movement1,
238            s_a.belt.1 + 1.0 * movement1,
239        );
240        next.belt.orientation = Quaternion::rotation_x(0.55 * movement1);
241
242        if let Some(prev_aimed_dir) = prev_aimed_dir {
243            let forward = prev_aimed_dir.dot(orientation).abs();
244            let sideways = 1.0 - forward;
245
246            if matches!(hands.0, None | Some(Hands::One)) {
247                next.hand_l.position += Vec3::new(-2.0, -8.0, 6.0);
248                next.hand_l.orientation =
249                    next.hand_l.orientation * Quaternion::rotation_z(PI * -0.25);
250
251                next.main.position += Vec3::new(-2.0, -6.0, 8.0);
252                next.main.orientation = next.main.orientation * Quaternion::rotation_x(PI * 0.6);
253            }
254
255            if matches!(hands.1, None | Some(Hands::One)) {
256                next.hand_r.position += Vec3::new(2.0, -6.0, 6.0);
257                next.hand_r.orientation =
258                    next.hand_r.orientation * Quaternion::rotation_z(PI * 0.25);
259
260                next.second.position += Vec3::new(2.0, -8.0, 8.0);
261                next.second.orientation =
262                    next.second.orientation * Quaternion::rotation_x(PI * 0.6);
263            }
264
265            next.shorts.position =
266                Vec3::new(0.0, s_a.shorts.0 + 0.5 * movement1, s_a.shorts.1 - 1.0);
267
268            next.shorts.orientation = Quaternion::rotation_x(0.0 * movement1);
269
270            next.foot_l.position = Vec3::new(
271                1.0 * movement1 - s_a.foot.0 - 4.0 * sideways,
272                s_a.foot.1 + 5.5 * movement1 * forward,
273                s_a.foot.2 - 5.0 * movement1 - 5.0,
274            );
275            next.foot_l.orientation =
276                Quaternion::rotation_x(0.5 * forward) * Quaternion::rotation_y(0.8 * sideways);
277
278            next.foot_r.position = Vec3::new(
279                1.0 * movement1 + s_a.foot.0 + 4.0 * sideways,
280                s_a.foot.1 - (5.5 * movement1 + 4.0) * forward,
281                s_a.foot.2 - 5.0 * movement1 - 3.0,
282            );
283            next.foot_r.orientation =
284                Quaternion::rotation_x(1.5 * forward) * Quaternion::rotation_y(-0.8 * sideways);
285        } else {
286            next.chest.orientation = Quaternion::rotation_x(-0.2 * movement1);
287
288            next.shorts.position = Vec3::new(
289                0.0,
290                s_a.shorts.0 + 4.5 * movement1,
291                s_a.shorts.1 + 2.5 * movement1,
292            );
293            next.shorts.orientation = Quaternion::rotation_x(0.8 * movement1);
294
295            next.foot_l.position = Vec3::new(
296                1.0 * movement1 - s_a.foot.0 + 5.0,
297                s_a.foot.1 + 5.5 * movement1,
298                s_a.foot.2 - 5.0 * movement1,
299            );
300            next.foot_l.orientation = Quaternion::rotation_x(0.9 * movement1);
301
302            next.foot_r.position = Vec3::new(
303                1.0 * movement1 + s_a.foot.0 + 3.0,
304                s_a.foot.1 + 5.5 * movement1,
305                s_a.foot.2 - 5.0 * movement1,
306            );
307            next.foot_r.orientation = Quaternion::rotation_x(0.9 * movement1);
308        }
309
310        next.torso.position = if prev_aimed_dir.is_some() {
311            Vec3::new(0.0, 0.0, 4.0 + 7.0 * movement1)
312        } else {
313            Vec3::new(4.0, 0.0, 7.0 * movement1)
314        };
315        let roll_spin = Quaternion::rotation_x(-0.3 + movement1 * -0.4 + movement2 * -2.0 * PI);
316        next.torso.orientation = if let Some(prev_aimed_dir) = prev_aimed_dir {
317            // This is *slightly* hacky. Because rolling is not strafed movement, we
318            // actually correct for the entity orientation to make sure that our
319            // rolling motion is correct with respect to our original orientation
320            roll_spin
321                * Dir::from_unnormalized(orientation.into_array().into())
322                    .zip(prev_aimed_dir.to_horizontal())
323                    .map(|(ori, prev_aimed_dir)| {
324                        Quaternion::<f32>::from_vec4(
325                            ori.rotation_between(prev_aimed_dir)
326                                .into_vec4()
327                                .into_array()
328                                .into(),
329                        )
330                    })
331                    .unwrap_or_default()
332        } else {
333            roll_spin * Quaternion::rotation_z(tilt * -10.0) * Quaternion::rotation_y(-0.6)
334        };
335
336        next
337    }
338}