veloren_common/comp/body/
golem.rs

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