veloren_common/comp/body/
dragon.rs

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