veloren_common/comp/body/
fish_small.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::FishSmall(body) }
29}
30
31enum_iter! {
32    ~const_array(ALL)
33    #[derive(
34        Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
35    #[repr(u32)]
36    pub enum Species {
37        Clownfish = 0,
38        Piranha = 1,
39    }
40}
41
42/// Data representing per-species generic data.
43#[derive(Clone, Debug, Deserialize, Serialize)]
44pub struct AllSpecies<SpeciesMeta> {
45    pub clownfish: SpeciesMeta,
46    pub piranha: SpeciesMeta,
47}
48
49impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
50    type Output = SpeciesMeta;
51
52    #[inline]
53    fn index(&self, &index: &'a Species) -> &Self::Output {
54        match index {
55            Species::Clownfish => &self.clownfish,
56            Species::Piranha => &self.piranha,
57        }
58    }
59}
60
61pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
62
63impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
64    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
65    type Item = Species;
66
67    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
68}
69
70enum_iter! {
71    ~const_array(ALL)
72    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
73    #[repr(u32)]
74    pub enum BodyType {
75        Female = 0,
76        Male = 1,
77    }
78}
79pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;