veloren_common/comp/body/
bird_large.rs1use common_base::{enum_iter, struct_iter};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4
5struct_iter! {
6 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
7 pub struct Body {
8 pub species: Species,
9 pub body_type: BodyType,
10 }
11}
12
13impl Body {
14 pub fn random() -> Self {
15 let mut rng = thread_rng();
16 let species = *ALL_SPECIES.choose(&mut rng).unwrap();
17 Self::random_with(&mut rng, &species)
18 }
19
20 #[inline]
21 pub fn random_with(rng: &mut impl rand::Rng, &species: &Species) -> Self {
22 let body_type = *ALL_BODY_TYPES.choose(rng).unwrap();
23 Self { species, body_type }
24 }
25}
26
27impl From<Body> for super::Body {
28 fn from(body: Body) -> Self { super::Body::BirdLarge(body) }
29}
30
31enum_iter! {
32 ~const_array(ALL)
33 #[derive(
34 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#[derive(Clone, Debug, Deserialize, Serialize)]
50pub struct AllSpecies<SpeciesMeta> {
51 pub phoenix: SpeciesMeta,
52 pub cockatrice: SpeciesMeta,
53 pub roc: SpeciesMeta,
54 pub wyvern_flame: SpeciesMeta,
55 pub wyvern_cloud: SpeciesMeta,
56 pub wyvern_frost: SpeciesMeta,
57 pub wyvern_sea: SpeciesMeta,
58 pub wyvern_weald: SpeciesMeta,
59}
60
61impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
62 type Output = SpeciesMeta;
63
64 #[inline]
65 fn index(&self, &index: &'a Species) -> &Self::Output {
66 match index {
67 Species::Phoenix => &self.phoenix,
68 Species::Cockatrice => &self.cockatrice,
69 Species::Roc => &self.roc,
70 Species::FlameWyvern => &self.wyvern_flame,
71 Species::CloudWyvern => &self.wyvern_cloud,
72 Species::FrostWyvern => &self.wyvern_frost,
73 Species::SeaWyvern => &self.wyvern_sea,
74 Species::WealdWyvern => &self.wyvern_weald,
75 }
76 }
77}
78
79pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
80
81impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
82 type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
83 type Item = Species;
84
85 fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
86}
87
88enum_iter! {
89 ~const_array(ALL)
90 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
91 #[repr(u32)]
92 pub enum BodyType {
93 Female = 0,
94 Male = 1,
95 }
96}
97pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;