veloren_common/comp/body/
dragon.rs

1use 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::Dragon(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        Reddragon = 0,
38    }
39}
40
41/// Data representing per-species generic data.
42#[derive(Clone, Debug, Deserialize, Serialize)]
43pub struct AllSpecies<SpeciesMeta> {
44    pub reddragon: SpeciesMeta,
45}
46
47impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
48    type Output = SpeciesMeta;
49
50    #[inline]
51    fn index(&self, &index: &'a Species) -> &Self::Output {
52        match index {
53            Species::Reddragon => &self.reddragon,
54        }
55    }
56}
57
58pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
59
60impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
61    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
62    type Item = Species;
63
64    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
65}
66
67enum_iter! {
68    ~const_array(ALL)
69    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
70    #[repr(u32)]
71    pub enum BodyType {
72        Female = 0,
73        Male = 1,
74    }
75}
76pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;