veloren_common/comp/body/
theropod.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::Theropod(body) }
29}
30
31enum_iter! {
32    ~const_array(ALL)
33    #[derive(
34        Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
35    )]
36    #[repr(u32)]
37    pub enum Species {
38        Archaeos = 0,
39        Odonto = 1,
40        Sandraptor = 2,
41        Snowraptor = 3,
42        Woodraptor = 4,
43        Sunlizard = 5,
44        Yale = 6,
45        Ntouka = 7,
46        Dodarock = 8,
47        Axebeak = 9,
48    }
49}
50
51/// Data representing per-species generic data.
52#[derive(Clone, Debug, Serialize, Deserialize)]
53pub struct AllSpecies<SpeciesMeta> {
54    pub archaeos: SpeciesMeta,
55    pub odonto: SpeciesMeta,
56    pub raptor_sand: SpeciesMeta,
57    pub raptor_snow: SpeciesMeta,
58    pub raptor_wood: SpeciesMeta,
59    pub sunlizard: SpeciesMeta,
60    pub yale: SpeciesMeta,
61    pub dodarock: SpeciesMeta,
62    pub ntouka: SpeciesMeta,
63    pub axebeak: 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::Archaeos => &self.archaeos,
73            Species::Odonto => &self.odonto,
74            Species::Sandraptor => &self.raptor_sand,
75            Species::Snowraptor => &self.raptor_snow,
76            Species::Woodraptor => &self.raptor_wood,
77            Species::Sunlizard => &self.sunlizard,
78            Species::Yale => &self.yale,
79            Species::Dodarock => &self.dodarock,
80            Species::Ntouka => &self.ntouka,
81            Species::Axebeak => &self.axebeak,
82        }
83    }
84}
85
86pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
87
88impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
89    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
90    type Item = Species;
91
92    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
93}
94
95enum_iter! {
96    ~const_array(ALL)
97    #[derive(
98        Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
99    )]
100    #[repr(u32)]
101    pub enum BodyType {
102        Female = 0,
103        Male = 1,
104    }
105}
106pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;