veloren_common/comp/body/
bird_large.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::BirdLarge(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        Phoenix = 0,
38        Cockatrice = 1,
39        Roc = 2,
40        FlameWyvern = 3,
41        CloudWyvern = 4,
42        FrostWyvern = 5,
43        SeaWyvern = 6,
44        WealdWyvern = 7,
45    }
46);
47
48/// Data representing per-species generic data.
49///
50/// NOTE: Deliberately don't (yet?) implement serialize.
51#[derive(Clone, Debug, Deserialize)]
52pub struct AllSpecies<SpeciesMeta> {
53    pub phoenix: SpeciesMeta,
54    pub cockatrice: SpeciesMeta,
55    pub roc: SpeciesMeta,
56    pub wyvern_flame: SpeciesMeta,
57    pub wyvern_cloud: SpeciesMeta,
58    pub wyvern_frost: SpeciesMeta,
59    pub wyvern_sea: SpeciesMeta,
60    pub wyvern_weald: SpeciesMeta,
61}
62
63impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
64    type Output = SpeciesMeta;
65
66    #[inline]
67    fn index(&self, &index: &'a Species) -> &Self::Output {
68        match index {
69            Species::Phoenix => &self.phoenix,
70            Species::Cockatrice => &self.cockatrice,
71            Species::Roc => &self.roc,
72            Species::FlameWyvern => &self.wyvern_flame,
73            Species::CloudWyvern => &self.wyvern_cloud,
74            Species::FrostWyvern => &self.wyvern_frost,
75            Species::SeaWyvern => &self.wyvern_sea,
76            Species::WealdWyvern => &self.wyvern_weald,
77        }
78    }
79}
80
81pub const ALL_SPECIES: [Species; 8] = [
82    Species::Phoenix,
83    Species::Cockatrice,
84    Species::Roc,
85    Species::FlameWyvern,
86    Species::CloudWyvern,
87    Species::FrostWyvern,
88    Species::SeaWyvern,
89    Species::WealdWyvern,
90];
91
92impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
93    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
94    type Item = Species;
95
96    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
97}
98
99make_case_elim!(
100    body_type,
101    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
102    #[repr(u32)]
103    pub enum BodyType {
104        Female = 0,
105        Male = 1,
106    }
107);
108
109pub const ALL_BODY_TYPES: [BodyType; 2] = [BodyType::Female, BodyType::Male];