veloren_common/comp/body/
fish_medium.rs

1use crate::{make_case_elim, make_proj_elim};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4
5make_proj_elim!(
6    body,
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
32make_case_elim!(
33    species,
34    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
35    #[repr(u32)]
36    pub enum Species {
37        Marlin = 0,
38        Icepike = 1,
39    }
40);
41
42/// Data representing per-species generic data.
43///
44/// NOTE: Deliberately don't (yet?) implement serialize.
45#[derive(Clone, Debug, Deserialize)]
46pub struct AllSpecies<SpeciesMeta> {
47    pub marlin: SpeciesMeta,
48    pub icepike: SpeciesMeta,
49}
50
51impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
52    type Output = SpeciesMeta;
53
54    #[inline]
55    fn index(&self, &index: &'a Species) -> &Self::Output {
56        match index {
57            Species::Marlin => &self.marlin,
58            Species::Icepike => &self.icepike,
59        }
60    }
61}
62
63pub const ALL_SPECIES: [Species; 2] = [Species::Marlin, Species::Icepike];
64
65impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
66    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
67    type Item = Species;
68
69    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
70}
71
72make_case_elim!(
73    body_type,
74    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
75    #[repr(u32)]
76    pub enum BodyType {
77        Female = 0,
78        Male = 1,
79    }
80);
81pub const ALL_BODY_TYPES: [BodyType; 2] = [BodyType::Female, BodyType::Male];