veloren_common/comp/body/
arthropod.rs

1use common_base::{enum_iter, struct_iter};
2
3use rand::{seq::SliceRandom, thread_rng};
4use serde::{Deserialize, Serialize};
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::Arthropod(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        Tarantula = 0,
39        Blackwidow = 1,
40        Antlion = 2,
41        Hornbeetle = 3,
42        Leafbeetle = 4,
43        Stagbeetle = 5,
44        Weevil = 6,
45        Cavespider = 7,
46        Moltencrawler = 8,
47        Mosscrawler = 9,
48        Sandcrawler = 10,
49        Dagonite = 11,
50        Emberfly = 12,
51    }
52}
53
54/// Data representing per-species generic data.
55#[derive(Clone, Debug, Deserialize, Serialize)]
56pub struct AllSpecies<SpeciesMeta> {
57    pub tarantula: SpeciesMeta,
58    pub black_widow: SpeciesMeta,
59    pub antlion: SpeciesMeta,
60    pub horn_beetle: SpeciesMeta,
61    pub leaf_beetle: SpeciesMeta,
62    pub stag_beetle: SpeciesMeta,
63    pub weevil: SpeciesMeta,
64    pub cave_spider: SpeciesMeta,
65    pub crawler_molten: SpeciesMeta,
66    pub crawler_moss: SpeciesMeta,
67    pub crawler_sand: SpeciesMeta,
68    pub dagonite: SpeciesMeta,
69    pub emberfly: SpeciesMeta,
70}
71
72impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
73    type Output = SpeciesMeta;
74
75    #[inline]
76    fn index(&self, &index: &'a Species) -> &Self::Output {
77        match index {
78            Species::Tarantula => &self.tarantula,
79            Species::Blackwidow => &self.black_widow,
80            Species::Antlion => &self.antlion,
81            Species::Hornbeetle => &self.horn_beetle,
82            Species::Leafbeetle => &self.leaf_beetle,
83            Species::Stagbeetle => &self.stag_beetle,
84            Species::Weevil => &self.weevil,
85            Species::Cavespider => &self.cave_spider,
86            Species::Moltencrawler => &self.crawler_molten,
87            Species::Mosscrawler => &self.crawler_moss,
88            Species::Sandcrawler => &self.crawler_sand,
89            Species::Dagonite => &self.dagonite,
90            Species::Emberfly => &self.emberfly,
91        }
92    }
93}
94
95pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
96
97impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
98    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
99    type Item = Species;
100
101    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
102}
103
104enum_iter! {
105    ~const_array(ALL)
106    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
107    #[repr(u32)]
108    pub enum BodyType {
109        Female = 0,
110        Male = 1,
111    }
112}
113pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;