1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
pub mod idle;

// Reexports
pub use self::idle::IdleAnimation;

use super::{make_bone, vek::*, FigureBoneData, Offsets, Skeleton, TrailSource};
use common::comp::{self};
use core::convert::TryFrom;

pub type Body = comp::ship::Body;

skeleton_impls!(struct ShipSkeleton {
    + bone0,
    + bone1,
    + bone2,
    + bone3,
});

impl Skeleton for ShipSkeleton {
    type Attr = SkeletonAttr;
    type Body = Body;

    const BONE_COUNT: usize = 4;
    #[cfg(feature = "use-dyn-lib")]
    const COMPUTE_FN: &'static [u8] = b"ship_compute_mats\0";

    #[cfg_attr(feature = "be-dyn-lib", export_name = "ship_compute_mats")]
    fn compute_matrices_inner(
        &self,
        base_mat: Mat4<f32>,
        buf: &mut [FigureBoneData; super::MAX_BONE_COUNT],
        body: Self::Body,
    ) -> Offsets {
        // Ships are normal scale
        let scale_mat = Mat4::scaling_3d(1.0);

        let attr = SkeletonAttr::from(&body);

        let bone0_mat = base_mat * scale_mat * Mat4::<f32>::from(self.bone0);
        let bone1_mat = bone0_mat * Mat4::<f32>::from(self.bone1);
        let bone2_mat = bone0_mat * Mat4::<f32>::from(self.bone2);

        *(<&mut [_; Self::BONE_COUNT]>::try_from(&mut buf[0..Self::BONE_COUNT]).unwrap()) = [
            make_bone(bone0_mat),
            make_bone(bone1_mat),
            make_bone(bone2_mat),
            make_bone(bone0_mat * Mat4::<f32>::from(self.bone3)),
        ];
        Offsets {
            // TODO: see quadruped_medium for how to animate this
            mount_bone: Transform {
                position: (base_mat * scale_mat)
                    .mul_point(comp::Body::Ship(body).mount_offset().into_tuple().into()),
                ..Default::default()
            },
            primary_trail_mat: attr
                .bone1_prop_trail_offset
                .map(|offset| (bone1_mat, TrailSource::Propeller(offset))),
            secondary_trail_mat: attr
                .bone2_prop_trail_offset
                .map(|offset| (bone2_mat, TrailSource::Propeller(offset))),
            ..Default::default()
        }
    }
}

pub struct SkeletonAttr {
    bone0: (f32, f32, f32),
    bone1: (f32, f32, f32),
    bone2: (f32, f32, f32),
    bone3: (f32, f32, f32),
    bone1_ori: f32,
    bone2_ori: f32,
    bone_rotation_rate: f32,
    bone1_prop_trail_offset: Option<f32>,
    bone2_prop_trail_offset: Option<f32>,
}

impl<'a> TryFrom<&'a comp::Body> for SkeletonAttr {
    type Error = ();

    fn try_from(body: &'a comp::Body) -> Result<Self, Self::Error> {
        match body {
            comp::Body::Ship(body) => Ok(SkeletonAttr::from(body)),
            _ => Err(()),
        }
    }
}

impl Default for SkeletonAttr {
    fn default() -> Self {
        Self {
            bone0: (0.0, 0.0, 0.0),
            bone1: (0.0, 0.0, 0.0),
            bone2: (0.0, 0.0, 0.0),
            bone3: (0.0, 0.0, 0.0),
            bone1_ori: 0.0,
            bone2_ori: 0.0,
            bone_rotation_rate: 0.0,
            bone1_prop_trail_offset: None,
            bone2_prop_trail_offset: None,
        }
    }
}

impl<'a> From<&'a Body> for SkeletonAttr {
    fn from(body: &'a Body) -> Self {
        use comp::ship::Body::*;
        Self {
            bone0: match body {
                DefaultAirship => (0.0, 0.0, 0.0),
                AirBalloon => (0.0, 0.0, 0.0),
                SailBoat => (0.0, 0.0, 0.0),
                Galleon => (0.0, 0.0, 0.0),
                Skiff => (0.0, 0.0, 0.0),
                Submarine => (0.0, 0.0, 0.0),
                Carriage => (0.0, 0.0, 0.0),
                Cart => (0.0, 0.0, 0.0),
                Volume => (0.0, 0.0, 0.0),
            },
            bone1: match body {
                DefaultAirship => (-13.0, -25.0, 10.0),
                AirBalloon => (0.0, 0.0, 0.0),
                SailBoat => (0.0, 0.0, 0.0),
                Galleon => (0.0, 0.0, 0.0),
                Skiff => (0.0, 0.0, 0.0),
                Submarine => (0.0, -15.0, 3.5),
                Carriage => (0.0, 3.0, 2.0),
                Cart => (0.0, 1.0, 1.0),
                Volume => (0.0, 0.0, 0.0),
            },
            bone2: match body {
                DefaultAirship => (13.0, -25.0, 10.0),
                AirBalloon => (0.0, 0.0, 0.0),
                SailBoat => (0.0, 0.0, 0.0),
                Galleon => (0.0, 0.0, 0.0),
                Skiff => (0.0, 0.0, 0.0),
                Submarine => (0.0, 0.0, 0.0),
                Carriage => (0.0, -3.0, 2.0),
                Cart => (0.0, -2.5, 1.0),
                Volume => (0.0, 0.0, 0.0),
            },
            bone3: match body {
                DefaultAirship => (0.0, -27.5, 8.5),
                AirBalloon => (0.0, -9.0, 8.0),
                SailBoat => (0.0, 0.0, 0.0),
                Galleon => (0.0, 0.0, 0.0),
                Skiff => (0.0, 0.0, 0.0),
                Submarine => (0.0, -18.0, 3.5),
                Carriage => (0.0, 0.0, 0.0),
                Cart => (0.0, 0.0, 0.0),
                Volume => (0.0, 0.0, 0.0),
            },
            bone1_ori: match body {
                Carriage | Cart => std::f32::consts::PI * 0.5,
                _ => 0.0,
            },
            bone2_ori: match body {
                Carriage | Cart => std::f32::consts::PI * -0.5,
                _ => 0.0,
            },
            bone_rotation_rate: match body {
                Carriage => 0.25,
                Cart => 0.4,
                _ => 0.8,
            },
            bone1_prop_trail_offset: match body {
                DefaultAirship => Some(8.5),
                Submarine => Some(3.5),
                _ => None,
            },
            bone2_prop_trail_offset: match body {
                DefaultAirship => Some(8.5),
                _ => None,
            },
        }
    }
}