veloren_common/comp/body/
bird_large.rs1use common_base::{enum_iter, struct_iter};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4use strum::{Display, EnumString};
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::BirdLarge(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 Phoenix = 0,
39 Cockatrice = 1,
40 Roc = 2,
41 FlameWyvern = 3,
42 CloudWyvern = 4,
43 FrostWyvern = 5,
44 SeaWyvern = 6,
45 WealdWyvern = 7,
46 }
47}
48
49#[derive(Clone, Debug, Deserialize, Serialize)]
51pub struct AllSpecies<SpeciesMeta> {
52 pub phoenix: SpeciesMeta,
53 pub cockatrice: SpeciesMeta,
54 pub roc: SpeciesMeta,
55 pub wyvern_flame: SpeciesMeta,
56 pub wyvern_cloud: SpeciesMeta,
57 pub wyvern_frost: SpeciesMeta,
58 pub wyvern_sea: SpeciesMeta,
59 pub wyvern_weald: SpeciesMeta,
60}
61
62impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
63 type Output = SpeciesMeta;
64
65 #[inline]
66 fn index(&self, &index: &'a Species) -> &Self::Output {
67 match index {
68 Species::Phoenix => &self.phoenix,
69 Species::Cockatrice => &self.cockatrice,
70 Species::Roc => &self.roc,
71 Species::FlameWyvern => &self.wyvern_flame,
72 Species::CloudWyvern => &self.wyvern_cloud,
73 Species::FrostWyvern => &self.wyvern_frost,
74 Species::SeaWyvern => &self.wyvern_sea,
75 Species::WealdWyvern => &self.wyvern_weald,
76 }
77 }
78}
79
80pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
81
82impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
83 type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
84 type Item = Species;
85
86 fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
87}
88
89enum_iter! {
90 ~const_array(ALL)
91 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, EnumString, Display)]
92 #[repr(u32)]
93 pub enum BodyType {
94 Female = 0,
95 Male = 1,
96 }
97}
98pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;