veloren_common/comp/body/
crustacean.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::Crustacean(body) }
30}
31
32// Renaming any enum entries here (re-ordering is fine) will require a
33// database migration to ensure pets correctly de-serialize on player login.
34enum_iter! {
35    ~const_array(ALL)
36    #[derive(
37        Copy,
38        Clone,
39        Debug,
40        Display,
41        EnumString,
42        PartialEq,
43        Eq,
44        PartialOrd,
45        Ord,
46        Hash,
47        Serialize,
48        Deserialize,
49    )]
50    #[repr(u32)]
51    pub enum Species {
52        Crab = 0,
53        SoldierCrab = 1,
54        Karkatha = 2,
55    }
56}
57
58/// Data representing per-species generic data.
59#[derive(Clone, Debug, Serialize, Deserialize)]
60pub struct AllSpecies<SpeciesMeta> {
61    pub crab: SpeciesMeta,
62    pub soldier_crab: SpeciesMeta,
63    pub karkatha: SpeciesMeta,
64}
65
66impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
67    type Output = SpeciesMeta;
68
69    #[inline]
70    fn index(&self, &index: &'a Species) -> &Self::Output {
71        match index {
72            Species::Crab => &self.crab,
73            Species::SoldierCrab => &self.soldier_crab,
74            Species::Karkatha => &self.karkatha,
75        }
76    }
77}
78
79pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
80
81impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
82    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
83    type Item = Species;
84
85    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
86}
87
88enum_iter! {
89    ~const_array(ALL)
90    #[derive(
91        Copy,
92        Clone,
93        Debug,
94        Display,
95        EnumString,
96        PartialEq,
97        Eq,
98        PartialOrd,
99        Ord,
100        Hash,
101        Serialize,
102        Deserialize,
103    )]
104    #[repr(u32)]
105    pub enum BodyType {
106        Female = 0,
107        Male = 1,
108    }
109}
110pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;