veloren_common/comp/body/
arthropod.rs

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