veloren_common/comp/body/
golem.rs

1use common_base::{enum_iter, struct_iter};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4
5struct_iter! {
6    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
7    pub struct Body {
8        pub species: Species,
9        pub body_type: BodyType,
10    }
11}
12
13impl Body {
14    pub fn random() -> Self {
15        let mut rng = thread_rng();
16        let species = *ALL_SPECIES.choose(&mut rng).unwrap();
17        Self::random_with(&mut rng, &species)
18    }
19
20    #[inline]
21    pub fn random_with(rng: &mut impl rand::Rng, &species: &Species) -> Self {
22        let body_type = *ALL_BODY_TYPES.choose(rng).unwrap();
23        Self { species, body_type }
24    }
25}
26
27impl From<Body> for super::Body {
28    fn from(body: Body) -> Self { super::Body::Golem(body) }
29}
30
31enum_iter! {
32    ~const_array(ALL)
33    #[derive(
34        Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
35    #[repr(u32)]
36    pub enum Species {
37        StoneGolem = 0,
38        Treant = 1,
39        ClayGolem = 2,
40        WoodGolem = 3,
41        CoralGolem = 4,
42        Gravewarden = 5,
43        AncientEffigy = 6,
44        Mogwai = 7,
45        IronGolem = 8,
46    }
47}
48
49/// Data representing per-species generic data.
50#[derive(Clone, Debug, Deserialize, Serialize)]
51pub struct AllSpecies<SpeciesMeta> {
52    pub stonegolem: SpeciesMeta,
53    pub treant: SpeciesMeta,
54    pub claygolem: SpeciesMeta,
55    pub woodgolem: SpeciesMeta,
56    pub coralgolem: SpeciesMeta,
57    pub gravewarden: SpeciesMeta,
58    pub ancienteffigy: SpeciesMeta,
59    pub mogwai: SpeciesMeta,
60    pub irongolem: SpeciesMeta,
61}
62
63impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
64    type Output = SpeciesMeta;
65
66    #[inline]
67    fn index(&self, &index: &'a Species) -> &Self::Output {
68        match index {
69            Species::StoneGolem => &self.stonegolem,
70            Species::Treant => &self.treant,
71            Species::ClayGolem => &self.claygolem,
72            Species::WoodGolem => &self.woodgolem,
73            Species::CoralGolem => &self.coralgolem,
74            Species::Gravewarden => &self.gravewarden,
75            Species::AncientEffigy => &self.ancienteffigy,
76            Species::Mogwai => &self.mogwai,
77            Species::IronGolem => &self.irongolem,
78        }
79    }
80}
81
82pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
83
84impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
85    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
86    type Item = Species;
87
88    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
89}
90
91enum_iter! {
92    ~const_array(ALL)
93    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
94    #[repr(u32)]
95    pub enum BodyType {
96        Female = 0,
97        Male = 1,
98    }
99}
100pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;