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