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