veloren/common/src/comp/body/theropod.rs

105 lines
2.9 KiB
Rust
Raw Normal View History

2020-08-23 07:39:18 +00:00
use rand::{seq::SliceRandom, thread_rng};
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Body {
pub species: Species,
pub body_type: BodyType,
}
impl Body {
pub fn random() -> Self {
let mut rng = thread_rng();
2022-09-08 19:51:02 +00:00
let species = *ALL_SPECIES.choose(&mut rng).unwrap();
2020-08-23 07:39:18 +00:00
Self::random_with(&mut rng, &species)
}
#[inline]
pub fn random_with(rng: &mut impl rand::Rng, &species: &Species) -> Self {
2022-09-08 19:51:02 +00:00
let body_type = *ALL_BODY_TYPES.choose(rng).unwrap();
2020-08-23 07:39:18 +00:00
Self { species, body_type }
}
}
impl From<Body> for super::Body {
fn from(body: Body) -> Self { super::Body::Theropod(body) }
2020-08-23 07:39:18 +00:00
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u32)]
pub enum Species {
Archaeos = 0,
2020-09-16 03:17:56 +00:00
Odonto = 1,
2020-10-04 23:47:54 +00:00
Sandraptor = 2,
Snowraptor = 3,
Woodraptor = 4,
2021-01-13 23:45:56 +00:00
Sunlizard = 5,
2021-03-12 00:20:05 +00:00
Yale = 6,
Ntouka = 7,
2022-09-06 10:35:10 +00:00
Dodarock = 8,
2022-12-19 12:37:51 +00:00
Axebeak = 9,
2020-08-23 07:39:18 +00:00
}
/// Data representing per-species generic data.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AllSpecies<SpeciesMeta> {
pub archaeos: SpeciesMeta,
2020-09-16 03:17:56 +00:00
pub odonto: SpeciesMeta,
2020-10-04 23:47:54 +00:00
pub raptor_sand: SpeciesMeta,
pub raptor_snow: SpeciesMeta,
pub raptor_wood: SpeciesMeta,
2021-01-13 23:45:56 +00:00
pub sunlizard: SpeciesMeta,
2021-03-12 00:20:05 +00:00
pub yale: SpeciesMeta,
2022-09-06 10:35:10 +00:00
pub dodarock: SpeciesMeta,
2021-03-12 00:20:05 +00:00
pub ntouka: SpeciesMeta,
2022-12-19 12:37:51 +00:00
pub axebeak: SpeciesMeta,
2020-08-23 07:39:18 +00:00
}
impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
type Output = SpeciesMeta;
#[inline]
fn index(&self, &index: &'a Species) -> &Self::Output {
match index {
Species::Archaeos => &self.archaeos,
2020-09-16 03:17:56 +00:00
Species::Odonto => &self.odonto,
2020-10-04 23:47:54 +00:00
Species::Sandraptor => &self.raptor_sand,
Species::Snowraptor => &self.raptor_snow,
Species::Woodraptor => &self.raptor_wood,
2021-01-13 23:45:56 +00:00
Species::Sunlizard => &self.sunlizard,
2021-03-12 00:20:05 +00:00
Species::Yale => &self.yale,
2022-09-06 10:35:10 +00:00
Species::Dodarock => &self.dodarock,
2021-03-12 00:20:05 +00:00
Species::Ntouka => &self.ntouka,
2022-12-19 12:37:51 +00:00
Species::Axebeak => &self.axebeak,
2020-08-23 07:39:18 +00:00
}
}
}
2022-12-19 12:37:51 +00:00
pub const ALL_SPECIES: [Species; 10] = [
2020-10-04 23:47:54 +00:00
Species::Archaeos,
Species::Odonto,
Species::Sandraptor,
Species::Snowraptor,
Species::Woodraptor,
2021-01-13 23:45:56 +00:00
Species::Sunlizard,
2021-03-12 00:20:05 +00:00
Species::Yale,
2022-09-06 10:35:10 +00:00
Species::Dodarock,
2021-03-12 00:20:05 +00:00
Species::Ntouka,
2022-12-19 12:37:51 +00:00
Species::Axebeak,
2020-10-04 23:47:54 +00:00
];
2020-08-23 07:39:18 +00:00
impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
type Item = Species;
fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u32)]
pub enum BodyType {
Female = 0,
Male = 1,
}
pub const ALL_BODY_TYPES: [BodyType; 2] = [BodyType::Female, BodyType::Male];