veloren_voxygen_anim/ship/
mod.rs

1pub mod idle;
2
3// Reexports
4pub use self::idle::IdleAnimation;
5
6use super::{FigureBoneData, Skeleton, vek::*};
7use common::comp::{self};
8use core::convert::TryFrom;
9
10pub type Body = comp::ship::Body;
11
12skeleton_impls!(struct ShipSkeleton ComputedShipSkeleton {
13    + bone0
14    + bone1
15    + bone2
16    + bone3
17});
18
19impl Skeleton for ShipSkeleton {
20    type Attr = SkeletonAttr;
21    type Body = Body;
22    type ComputedSkeleton = ComputedShipSkeleton;
23
24    const BONE_COUNT: usize = ComputedShipSkeleton::BONE_COUNT;
25    #[cfg(feature = "use-dyn-lib")]
26    const COMPUTE_FN: &'static [u8] = b"ship_compute_mats\0";
27
28    #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "ship_compute_mats"))]
29    fn compute_matrices_inner(
30        &self,
31        base_mat: Mat4<f32>,
32        buf: &mut [FigureBoneData; super::MAX_BONE_COUNT],
33        _body: Self::Body,
34    ) -> Self::ComputedSkeleton {
35        // Ships are normal scale
36        let scale_mat = Mat4::scaling_3d(1.0);
37
38        let bone0_mat = base_mat * scale_mat * Mat4::<f32>::from(self.bone0);
39        let bone1_mat = bone0_mat * Mat4::<f32>::from(self.bone1);
40        let bone2_mat = bone0_mat * Mat4::<f32>::from(self.bone2);
41
42        let computed_skeleton = ComputedShipSkeleton {
43            bone0: bone0_mat,
44            bone1: bone1_mat,
45            bone2: bone2_mat,
46            bone3: bone0_mat * Mat4::<f32>::from(self.bone3),
47        };
48
49        computed_skeleton.set_figure_bone_data(buf);
50        computed_skeleton
51    }
52}
53
54pub struct SkeletonAttr {
55    bone0: (f32, f32, f32),
56    bone1: (f32, f32, f32),
57    bone2: (f32, f32, f32),
58    bone3: (f32, f32, f32),
59    bone1_ori: f32,
60    bone2_ori: f32,
61    bone_rotation_rate: f32,
62    pub bone1_prop_trail_offset: Option<f32>,
63    pub bone2_prop_trail_offset: Option<f32>,
64}
65
66impl<'a> TryFrom<&'a comp::Body> for SkeletonAttr {
67    type Error = ();
68
69    fn try_from(body: &'a comp::Body) -> Result<Self, Self::Error> {
70        match body {
71            comp::Body::Ship(body) => Ok(SkeletonAttr::from(body)),
72            _ => Err(()),
73        }
74    }
75}
76
77impl Default for SkeletonAttr {
78    fn default() -> Self {
79        Self {
80            bone0: (0.0, 0.0, 0.0),
81            bone1: (0.0, 0.0, 0.0),
82            bone2: (0.0, 0.0, 0.0),
83            bone3: (0.0, 0.0, 0.0),
84            bone1_ori: 0.0,
85            bone2_ori: 0.0,
86            bone_rotation_rate: 0.0,
87            bone1_prop_trail_offset: None,
88            bone2_prop_trail_offset: None,
89        }
90    }
91}
92
93impl<'a> From<&'a Body> for SkeletonAttr {
94    fn from(body: &'a Body) -> Self {
95        use comp::ship::Body::*;
96        Self {
97            bone0: match body {
98                DefaultAirship => (0.0, 0.0, 0.0),
99                AirBalloon => (0.0, 0.0, 0.0),
100                SailBoat => (0.0, 0.0, 0.0),
101                Galleon => (0.0, 0.0, 0.0),
102                Skiff => (0.0, 0.0, 0.0),
103                Submarine => (0.0, 0.0, 0.0),
104                Carriage => (0.0, 0.0, 0.0),
105                Cart => (0.0, 0.0, 0.0),
106                Volume => (0.0, 0.0, 0.0),
107                Train => (0.0, 0.0, 0.0),
108            },
109            bone1: match body {
110                DefaultAirship => (-13.0, -25.0, 10.0),
111                AirBalloon => (0.0, 0.0, 0.0),
112                SailBoat => (0.0, 0.0, 0.0),
113                Galleon => (0.0, 0.0, 0.0),
114                Skiff => (0.0, 0.0, 0.0),
115                Submarine => (0.0, -15.0, 3.5),
116                Carriage => (0.0, 3.0, 2.0),
117                Cart => (0.0, 1.0, 1.0),
118                Volume => (0.0, 0.0, 0.0),
119                Train => (0.0, -3.0, 2.0),
120            },
121            bone2: match body {
122                DefaultAirship => (13.0, -25.0, 10.0),
123                AirBalloon => (0.0, 0.0, 0.0),
124                SailBoat => (0.0, 0.0, 0.0),
125                Galleon => (0.0, 0.0, 0.0),
126                Skiff => (0.0, 0.0, 0.0),
127                Submarine => (0.0, 0.0, 0.0),
128                Carriage => (0.0, -3.0, 2.0),
129                Cart => (0.0, -2.5, 1.0),
130                Volume => (0.0, 0.0, 0.0),
131                Train => (0.0, -9.0, 2.0),
132            },
133            bone3: match body {
134                DefaultAirship => (0.0, -27.5, 8.5),
135                AirBalloon => (0.0, -9.0, 8.0),
136                SailBoat => (0.0, 0.0, 0.0),
137                Galleon => (0.0, 0.0, 0.0),
138                Skiff => (0.0, 0.0, 0.0),
139                Submarine => (0.0, -18.0, 3.5),
140                Carriage => (0.0, 0.0, 0.0),
141                Cart => (0.0, 0.0, 0.0),
142                Volume => (0.0, 0.0, 0.0),
143                Train => (0.0, 0.0, 0.0),
144            },
145            bone1_ori: match body {
146                Carriage | Cart | Train => std::f32::consts::PI * 0.5,
147                _ => 0.0,
148            },
149            bone2_ori: match body {
150                Carriage | Cart | Train => std::f32::consts::PI * -0.5,
151                _ => 0.0,
152            },
153            bone_rotation_rate: match body {
154                Carriage => 0.25,
155                Cart => 0.4,
156                Train => 0.25,
157                _ => 0.8,
158            },
159            bone1_prop_trail_offset: match body {
160                DefaultAirship => Some(8.5),
161                Submarine => Some(3.5),
162                _ => None,
163            },
164            bone2_prop_trail_offset: match body {
165                DefaultAirship => Some(8.5),
166                _ => None,
167            },
168        }
169    }
170}