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