veloren_common/comp/body/
bird_medium.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::BirdMedium(body) }
30}
31
32enum_iter! {
33 ~const_array(ALL)
34 #[derive(
35 Copy,
36 Clone,
37 Debug,
38 Display,
39 EnumString,
40 PartialEq,
41 Eq,
42 PartialOrd,
43 Ord,
44 Hash,
45 Serialize,
46 Deserialize,
47 )]
48 #[repr(u32)]
49 pub enum Species {
50 SnowyOwl = 0,
51 HornedOwl = 1,
52 Duck = 2,
53 Cockatiel = 3,
54 Chicken = 4,
55 Bat = 5,
56 Penguin = 6,
57 Goose = 7,
58 Peacock = 8,
59 Eagle = 9,
60 Parrot = 10,
61 Crow = 11,
62 Dodo = 12,
63 Parakeet = 13,
64 Puffin = 14,
65 Toucan = 15,
66 BloodmoonBat = 16,
67 VampireBat = 17,
68 }
69}
70
71#[derive(Clone, Debug, Deserialize, Serialize)]
73pub struct AllSpecies<SpeciesMeta> {
74 pub snowy_owl: SpeciesMeta,
75 pub horned_owl: SpeciesMeta,
76 pub duck: SpeciesMeta,
77 pub cockatiel: SpeciesMeta,
78 pub chicken: SpeciesMeta,
79 pub bat: SpeciesMeta,
80 pub penguin: SpeciesMeta,
81 pub goose: SpeciesMeta,
82 pub peacock: SpeciesMeta,
83 pub eagle: SpeciesMeta,
84 pub parrot: SpeciesMeta,
85 pub crow: SpeciesMeta,
86 pub dodo: SpeciesMeta,
87 pub parakeet: SpeciesMeta,
88 pub puffin: SpeciesMeta,
89 pub toucan: SpeciesMeta,
90 pub bloodmoon_bat: SpeciesMeta,
91 pub vampire_bat: SpeciesMeta,
92}
93
94impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
95 type Output = SpeciesMeta;
96
97 #[inline]
98 fn index(&self, &index: &'a Species) -> &Self::Output {
99 match index {
100 Species::SnowyOwl => &self.snowy_owl,
101 Species::HornedOwl => &self.horned_owl,
102 Species::Duck => &self.duck,
103 Species::Cockatiel => &self.cockatiel,
104 Species::Chicken => &self.chicken,
105 Species::Bat => &self.bat,
106 Species::Penguin => &self.penguin,
107 Species::Goose => &self.goose,
108 Species::Peacock => &self.peacock,
109 Species::Eagle => &self.eagle,
110 Species::Parrot => &self.parrot,
111 Species::Crow => &self.crow,
112 Species::Dodo => &self.dodo,
113 Species::Parakeet => &self.parakeet,
114 Species::Puffin => &self.puffin,
115 Species::Toucan => &self.toucan,
116 Species::BloodmoonBat => &self.bloodmoon_bat,
117 Species::VampireBat => &self.vampire_bat,
118 }
119 }
120}
121
122pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
123
124impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
125 type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
126 type Item = Species;
127
128 fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
129}
130
131enum_iter! {
132 ~const_array(ALL)
133 #[derive(
134 Copy,
135 Clone,
136 Debug,
137 Display,
138 EnumString,
139 PartialEq,
140 Eq,
141 PartialOrd,
142 Ord,
143 Hash,
144 Serialize,
145 Deserialize,
146 )]
147 #[repr(u32)]
148 pub enum BodyType {
149 Female = 0,
150 Male = 1,
151 }
152}
153pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;