veloren_common/comp/body/
golem.rs

1use crate::{make_case_elim, make_proj_elim};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4
5make_proj_elim!(
6    body,
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
32make_case_elim!(
33    species,
34    #[derive(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///
51/// NOTE: Deliberately don't (yet?) implement serialize.
52#[derive(Clone, Debug, Deserialize)]
53pub struct AllSpecies<SpeciesMeta> {
54    pub stonegolem: SpeciesMeta,
55    pub treant: SpeciesMeta,
56    pub claygolem: SpeciesMeta,
57    pub woodgolem: SpeciesMeta,
58    pub coralgolem: SpeciesMeta,
59    pub gravewarden: SpeciesMeta,
60    pub ancienteffigy: SpeciesMeta,
61    pub mogwai: SpeciesMeta,
62    pub irongolem: SpeciesMeta,
63}
64
65impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
66    type Output = SpeciesMeta;
67
68    #[inline]
69    fn index(&self, &index: &'a Species) -> &Self::Output {
70        match index {
71            Species::StoneGolem => &self.stonegolem,
72            Species::Treant => &self.treant,
73            Species::ClayGolem => &self.claygolem,
74            Species::WoodGolem => &self.woodgolem,
75            Species::CoralGolem => &self.coralgolem,
76            Species::Gravewarden => &self.gravewarden,
77            Species::AncientEffigy => &self.ancienteffigy,
78            Species::Mogwai => &self.mogwai,
79            Species::IronGolem => &self.irongolem,
80        }
81    }
82}
83
84pub const ALL_SPECIES: [Species; 9] = [
85    Species::StoneGolem,
86    Species::Treant,
87    Species::ClayGolem,
88    Species::WoodGolem,
89    Species::CoralGolem,
90    Species::Gravewarden,
91    Species::AncientEffigy,
92    Species::Mogwai,
93    Species::IronGolem,
94];
95
96impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
97    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
98    type Item = Species;
99
100    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
101}
102
103make_case_elim!(
104    body_type,
105    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
106    #[repr(u32)]
107    pub enum BodyType {
108        Female = 0,
109        Male = 1,
110    }
111);
112
113pub const ALL_BODY_TYPES: [BodyType; 2] = [BodyType::Female, BodyType::Male];