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