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};
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::FishMedium(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        Marlin = 0,
40        Icepike = 1,
41    }
42}
43
44/// Data representing per-species generic data.
45#[derive(Clone, Debug, Deserialize, Serialize)]
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
72enum_iter! {
73    ~const_array(ALL)
74    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, EnumString, Display)]
75    #[repr(u32)]
76    pub enum BodyType {
77        Female = 0,
78        Male = 1,
79    }
80}
81pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;