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
pub mod idle;

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

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

pub type Body = comp::item_drop::Body;

skeleton_impls!(struct ItemDropSkeleton {
    + bone0,
});

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

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

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

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

        *(<&mut [_; Self::BONE_COUNT]>::try_from(&mut buf[0..Self::BONE_COUNT]).unwrap()) =
            [make_bone(bone0_mat)];
        Offsets {
            lantern: Some((bone0_mat * Vec4::new(0.0, 0.0, 3.5, 1.0)).xyz()),
            mount_bone: Transform {
                position: comp::Body::ItemDrop(body)
                    .mount_offset()
                    .into_tuple()
                    .into(),
                ..Default::default()
            },
            ..Default::default()
        }
    }
}

impl ItemDropSkeleton {
    pub fn scale(body: &Body) -> f32 {
        match body {
            Body::Tool(_) => 0.8,
            Body::Glider => 0.45,
            Body::Coins => 0.3,
            Body::Armor(kind) => match kind {
                ItemDropArmorKind::Neck | ItemDropArmorKind::Ring => 0.5,
                ItemDropArmorKind::Back => 0.7,
                _ => 0.8,
            },
            _ => 1.0,
        }
    }
}

pub struct SkeletonAttr {
    bone0: (f32, f32, 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::ItemDrop(body) => Ok(SkeletonAttr::from(body)),
            _ => Err(()),
        }
    }
}

impl Default for SkeletonAttr {
    fn default() -> Self {
        Self {
            bone0: (0.0, 0.0, 0.0),
        }
    }
}

impl<'a> From<&'a Body> for SkeletonAttr {
    fn from(_body: &'a Body) -> Self {
        Self {
            bone0: (0.0, 0.0, 0.0),
        }
    }
}