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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
pub mod basic;
pub mod idle;
pub mod jump;
pub mod multi;
pub mod run;
pub mod stunned;

// Reexports
pub use self::{
    basic::{BasicAction, BasicActionDependency},
    idle::IdleAnimation,
    jump::JumpAnimation,
    multi::{MultiAction, MultiActionDependency},
    run::RunAnimation,
    stunned::StunnedAnimation,
};

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

pub type Body = comp::arthropod::Body;

skeleton_impls!(struct ArthropodSkeleton {
    + head,
    + chest,
    + mandible_l,
    + mandible_r,
    + wing_fl,
    + wing_fr,
    + wing_bl,
    + wing_br,
    + leg_fl,
    + leg_fr,
    + leg_fcl,
    + leg_fcr,
    + leg_bcl,
    + leg_bcr,
    + leg_bl,
    + leg_br,
});

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

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

    #[cfg_attr(feature = "be-dyn-lib", export_name = "arthropod_compute_s")]

    fn compute_matrices_inner(
        &self,
        base_mat: Mat4<f32>,
        buf: &mut [FigureBoneData; super::MAX_BONE_COUNT],
        body: Self::Body,
    ) -> Offsets {
        let base_mat = base_mat * Mat4::scaling_3d(SkeletonAttr::from(&body).scaler / 6.0);

        let chest_mat = base_mat * Mat4::<f32>::from(self.chest);
        let head_mat = chest_mat * Mat4::<f32>::from(self.head);
        let mandible_l_mat = head_mat * Mat4::<f32>::from(self.mandible_l);
        let mandible_r_mat = head_mat * Mat4::<f32>::from(self.mandible_r);
        let wing_fl_mat = chest_mat * Mat4::<f32>::from(self.wing_fl);
        let wing_fr_mat = chest_mat * Mat4::<f32>::from(self.wing_fr);
        let wing_bl_mat = chest_mat * Mat4::<f32>::from(self.wing_bl);
        let wing_br_mat = chest_mat * Mat4::<f32>::from(self.wing_br);
        let leg_fl_mat = chest_mat * Mat4::<f32>::from(self.leg_fl);
        let leg_fr_mat = chest_mat * Mat4::<f32>::from(self.leg_fr);
        let leg_fcl_mat = chest_mat * Mat4::<f32>::from(self.leg_fcl);
        let leg_fcr_mat = chest_mat * Mat4::<f32>::from(self.leg_fcr);
        let leg_bcl_mat = chest_mat * Mat4::<f32>::from(self.leg_bcl);
        let leg_bcr_mat = chest_mat * Mat4::<f32>::from(self.leg_bcr);
        let leg_bl_mat = chest_mat * Mat4::<f32>::from(self.leg_bl);
        let leg_br_mat = chest_mat * Mat4::<f32>::from(self.leg_br);

        *(<&mut [_; Self::BONE_COUNT]>::try_from(&mut buf[0..Self::BONE_COUNT]).unwrap()) = [
            make_bone(head_mat),
            make_bone(chest_mat),
            make_bone(mandible_l_mat),
            make_bone(mandible_r_mat),
            make_bone(wing_fl_mat),
            make_bone(wing_fr_mat),
            make_bone(wing_bl_mat),
            make_bone(wing_br_mat),
            make_bone(leg_fl_mat),
            make_bone(leg_fr_mat),
            make_bone(leg_fcl_mat),
            make_bone(leg_fcr_mat),
            make_bone(leg_bcl_mat),
            make_bone(leg_bcr_mat),
            make_bone(leg_bl_mat),
            make_bone(leg_br_mat),
        ];

        // TODO: mount points
        //use comp::arthropod::Species::*;
        let (mount_bone_mat, mount_bone_ori) = (chest_mat, self.chest.orientation);
        // Offset from the mounted bone's origin.
        // Note: This could be its own bone if we need to animate it independently.
        let mount_position = (mount_bone_mat * Vec4::from_point(mount_point(&body)))
            .homogenized()
            .xyz();
        // NOTE: We apply the ori from base_mat externally so we don't need to worry
        // about it here for now.
        let mount_orientation = mount_bone_ori;

        Offsets {
            viewpoint: Some((head_mat * Vec4::new(0.0, 7.0, 0.0, 1.0)).xyz()),
            mount_bone: Transform {
                position: mount_position,
                orientation: mount_orientation,
                scale: Vec3::one(),
            },
            ..Default::default()
        }
    }
}

pub struct SkeletonAttr {
    head: (f32, f32),
    chest: (f32, f32),
    mandible: (f32, f32, f32),
    wing_f: (f32, f32, f32),
    wing_b: (f32, f32, f32),
    leg_f: (f32, f32, f32),
    leg_fc: (f32, f32, f32),
    leg_bc: (f32, f32, f32),
    leg_b: (f32, f32, f32),
    scaler: f32,
    leg_ori: (f32, f32, f32, f32),
    snapper: bool,
}

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::Arthropod(body) => Ok(SkeletonAttr::from(body)),
            _ => Err(()),
        }
    }
}

impl Default for SkeletonAttr {
    fn default() -> Self {
        Self {
            head: (0.0, 0.0),
            chest: (0.0, 0.0),
            mandible: (0.0, 0.0, 0.0),
            wing_f: (0.0, 0.0, 0.0),
            wing_b: (0.0, 0.0, 0.0),
            leg_f: (0.0, 0.0, 0.0),
            leg_fc: (0.0, 0.0, 0.0),
            leg_bc: (0.0, 0.0, 0.0),
            leg_b: (0.0, 0.0, 0.0),
            scaler: 0.0,
            leg_ori: (0.0, 0.0, 0.0, 0.0),
            snapper: false,
        }
    }
}

impl<'a> From<&'a Body> for SkeletonAttr {
    fn from(body: &'a Body) -> Self {
        use comp::arthropod::Species::*;
        Self {
            head: match (body.species, body.body_type) {
                (Tarantula, _) => (6.0, 0.5),
                (Blackwidow, _) => (5.5, -3.0),
                (Antlion, _) => (4.5, 0.0),
                (Hornbeetle, _) => (5.0, 3.0),
                (Leafbeetle, _) => (4.0, 0.0),
                (Stagbeetle, _) => (5.0, -1.0),
                (Weevil, _) => (4.0, 0.0),
                (Cavespider, _) => (6.0, 0.5),
                (Moltencrawler, _) => (4.0, -1.0),
                (Mosscrawler, _) => (4.0, -1.5),
                (Sandcrawler, _) => (4.0, -1.0),
                (Dagonite, _) => (4.0, -1.0),
                (Emberfly, _) => (-1.5, 0.5),
            },
            chest: match (body.species, body.body_type) {
                (Tarantula, _) => (-5.0, 6.0),
                (Blackwidow, _) => (-5.0, 10.0),
                (Antlion, _) => (-5.0, 8.5),
                (Hornbeetle, _) => (-5.0, 7.5),
                (Leafbeetle, _) => (-5.0, 6.0),
                (Stagbeetle, _) => (-5.0, 6.5),
                (Weevil, _) => (-5.0, 6.0),
                (Cavespider, _) => (-5.0, 7.0),
                (Moltencrawler, _) => (-7.0, 6.0),
                (Mosscrawler, _) => (-7.0, 6.5),
                (Sandcrawler, _) => (-7.0, 6.0),
                (Dagonite, _) => (-6.0, 8.0),
                (Emberfly, _) => (-5.0, 2.5),
            },
            mandible: match (body.species, body.body_type) {
                (Tarantula, _) => (1.5, 7.0, -0.5),
                (Blackwidow, _) => (2.5, 8.0, 0.0),
                (Antlion, _) => (8.5, 9.0, -3.5),
                (Hornbeetle, _) => (1.5, 7.0, -0.5),
                (Leafbeetle, _) => (1.5, 7.0, -0.5),
                (Stagbeetle, _) => (1.5, 10.0, 1.0),
                (Weevil, _) => (1.5, 7.0, -0.5),
                (Cavespider, _) => (2.5, 8.0, -0.5),
                (Moltencrawler, _) => (2.5, 8.0, 0.0),
                (Mosscrawler, _) => (2.5, 8.0, 0.0),
                (Sandcrawler, _) => (2.5, 8.0, 0.0),
                (Dagonite, _) => (2.5, 8.0, 0.0),
                (Emberfly, _) => (1.5, 7.0, -0.5),
            },
            wing_f: match (body.species, body.body_type) {
                (Tarantula, _) => (3.0, 0.0, -4.0),
                (Blackwidow, _) => (3.0, 0.0, -4.0),
                (Antlion, _) => (3.0, 0.0, -4.0),
                (Hornbeetle, _) => (5.5, 5.0, 3.0),
                (Leafbeetle, _) => (0.5, 5.0, 3.0),
                (Stagbeetle, _) => (0.5, 6.0, 4.5),
                (Weevil, _) => (0.5, 5.0, 3.0),
                (Cavespider, _) => (3.0, 0.0, -4.0),
                (Moltencrawler, _) => (3.0, 0.0, -4.0),
                (Mosscrawler, _) => (3.0, 0.0, -4.0),
                (Sandcrawler, _) => (3.0, 0.0, -4.0),
                (Dagonite, _) => (3.0, 0.0, -4.0),
                (Emberfly, _) => (5.5, 6.0, 3.0),
            },
            wing_b: match (body.species, body.body_type) {
                (Tarantula, _) => (3.0, 0.0, -4.0),
                (Blackwidow, _) => (3.0, 0.0, -4.0),
                (Antlion, _) => (3.0, 0.0, -4.0),
                (Hornbeetle, _) => (4.0, 6.0, 2.0),
                (Leafbeetle, _) => (0.5, 4.0, 2.0),
                (Stagbeetle, _) => (0.5, 6.0, 3.0),
                (Weevil, _) => (0.5, 4.0, 1.5),
                (Cavespider, _) => (3.0, 0.0, -4.0),
                (Moltencrawler, _) => (3.0, 0.0, -4.0),
                (Mosscrawler, _) => (3.0, 0.0, -4.0),
                (Sandcrawler, _) => (3.0, 0.0, -4.0),
                (Dagonite, _) => (3.0, 0.0, -4.0),
                (Emberfly, _) => (4.0, 6.0, 2.0),
            },
            leg_f: match (body.species, body.body_type) {
                (Tarantula, _) => (4.0, 11.0, -1.5),
                (Blackwidow, _) => (4.0, 13.5, -6.0),
                (Antlion, _) => (4.0, 11.5, -4.0),
                (Hornbeetle, _) => (5.0, 6.0, -3.0),
                (Leafbeetle, _) => (5.0, 6.0, -1.0),
                (Stagbeetle, _) => (4.5, 6.0, -2.0),
                (Weevil, _) => (5.0, 9.0, -2.0),
                (Cavespider, _) => (4.0, 13.0, -3.0),
                (Moltencrawler, _) => (2.5, 14.0, -3.0),
                (Mosscrawler, _) => (1.5, 14.0, -3.5),
                (Sandcrawler, _) => (1.5, 14.0, -3.0),
                (Dagonite, _) => (1.5, 14.0, -3.0),
                (Emberfly, _) => (2.5, 6.0, -2.5),
            },
            leg_fc: match (body.species, body.body_type) {
                (Tarantula, _) => (1.5, 13.5, -1.5),
                (Blackwidow, _) => (2.5, 13.0, -5.5),
                (Antlion, _) => (1.5, 6.0, -4.0),
                (Hornbeetle, _) => (1.5, 7.5, -3.0),
                (Leafbeetle, _) => (1.5, 6.5, -1.5),
                (Stagbeetle, _) => (1.5, 7.5, -2.0),
                (Weevil, _) => (1.5, 8.5, -2.0),
                (Cavespider, _) => (2.5, 12.5, -2.5),
                (Moltencrawler, _) => (3.5, 11.0, -3.0),
                (Mosscrawler, _) => (2.5, 11.0, -3.5),
                (Sandcrawler, _) => (2.5, 11.0, -3.0),
                (Dagonite, _) => (2.5, 11.0, -3.0),
                (Emberfly, _) => (1.5, 7.5, -2.5),
            },
            leg_bc: match (body.species, body.body_type) {
                (Tarantula, _) => (1.5, 10.5, -1.5),
                (Blackwidow, _) => (2.5, 10.0, -5.5),
                (Antlion, _) => (6.0, 7.5, -4.0),
                (Hornbeetle, _) => (5.0, 6.0, -3.0),
                (Leafbeetle, _) => (4.5, 5.0, -2.5),
                (Stagbeetle, _) => (5.0, 6.0, -2.0),
                (Weevil, _) => (6.0, 5.0, -2.5),
                (Cavespider, _) => (2.5, 9.5, -2.5),
                (Moltencrawler, _) => (2.5, 8.0, -3.0),
                (Mosscrawler, _) => (1.5, 8.0, -3.5),
                (Sandcrawler, _) => (1.5, 8.0, -3.0),
                (Dagonite, _) => (1.5, 8.0, -3.0),
                (Emberfly, _) => (2.5, 3.5, -2.5),
            },
            leg_b: match (body.species, body.body_type) {
                (Tarantula, _) => (1.5, 7.5, -1.5),
                (Blackwidow, _) => (2.5, 7.0, -5.5),
                (Antlion, _) => (1.5, 7.5, -1.5),
                (Hornbeetle, _) => (1.5, 7.5, -1.5),
                (Leafbeetle, _) => (1.5, 7.5, -1.5),
                (Stagbeetle, _) => (1.5, 7.5, -1.5),
                (Weevil, _) => (1.5, 7.5, -1.5),
                (Cavespider, _) => (2.5, 6.5, -2.5),
                (Moltencrawler, _) => (2.5, 7.0, -5.5),
                (Mosscrawler, _) => (2.5, 7.0, -5.5),
                (Sandcrawler, _) => (2.5, 7.0, -5.5),
                (Dagonite, _) => (2.5, 7.0, -5.5),
                (Emberfly, _) => (1.5, 7.5, -1.0),
            },
            scaler: match (body.species, body.body_type) {
                (Tarantula, _) => 1.0,
                (Blackwidow, _) => 1.0,
                (Antlion, _) => 1.0,
                (Hornbeetle, _) => 1.0,
                (Leafbeetle, _) => 0.8,
                (Stagbeetle, _) => 1.0,
                (Weevil, _) => 0.75,
                (Cavespider, _) => 1.0,
                (Moltencrawler, _) => 1.0,
                (Mosscrawler, _) => 1.0,
                (Sandcrawler, _) => 1.0,
                (Dagonite, _) => 1.0,
                (Emberfly, _) => 0.5,
            },
            // Z ori (front, front center, back center, center)
            leg_ori: match (body.species, body.body_type) {
                (Antlion, _) => (0.7, -0.3, -0.4, 0.4),
                (_, _) => (0.1, -0.3, 0.0, 0.4),
            },
            // Whether or not it used its mandibles for attacks
            snapper: match (body.species, body.body_type) {
                (Stagbeetle, _) => true,
                (Antlion, _) => true,
                (_, _) => false,
            },
        }
    }
}

fn mount_point(_body: &Body) -> Vec3<f32> {
    // TODO: mount points
    //use comp::arthropod::{BodyType::*, Species::*};
    (0.0, -6.0, 6.0).into()
}