PkmnLib_rs/src/static_data/species_data/species.rs

91 lines
2.2 KiB
Rust
Raw Normal View History

use self::super::form::Form;
use crate::static_data::species_data::gender::Gender;
use crate::utils::random::Random;
use crate::StringKey;
2022-06-06 11:54:59 +00:00
use hashbrown::{HashMap, HashSet};
#[derive(Debug)]
pub struct Species<'a> {
id: u16,
name: StringKey,
gender_rate: f32,
growth_rate: StringKey,
capture_rate: u8,
forms: HashMap<StringKey, Form<'a>>,
flags: HashSet<StringKey>,
}
lazy_static::lazy_static! {
static ref DEFAULT_KEY: StringKey = StringKey::new("default");
}
impl<'a> Species<'a> {
pub fn new(
id: u16,
name: &StringKey,
gender_rate: f32,
growth_rate: &StringKey,
capture_rate: u8,
default_form: Form<'a>,
flags: HashSet<StringKey>,
) -> Species<'a> {
2022-06-06 11:54:59 +00:00
let mut forms = HashMap::with_capacity(1);
forms.insert_unique_unchecked(DEFAULT_KEY.clone(), default_form);
Species {
id,
name: name.clone(),
gender_rate,
growth_rate: growth_rate.clone(),
capture_rate,
2022-06-06 11:54:59 +00:00
forms,
flags,
}
}
pub fn id(&self) -> u16 {
self.id
}
pub fn name(&self) -> &StringKey {
&self.name
}
pub fn gender_rate(&self) -> f32 {
self.gender_rate
}
pub fn growth_rate(&self) -> &StringKey {
&self.growth_rate
}
pub fn capture_rate(&self) -> u8 {
self.capture_rate
}
pub fn forms(&self) -> &HashMap<StringKey, Form<'a>> {
&self.forms
}
pub fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
pub fn add_form(&mut self, id: StringKey, form: Form<'a>) {
self.forms.insert(id, form);
}
pub fn get_form(&self, id: &StringKey) -> Option<&Form> {
self.forms.get(id)
}
pub fn get_default_form(&self) -> &Form {
self.forms.get(&DEFAULT_KEY).unwrap()
}
pub fn get_random_gender(&self, rand: &mut Random) -> Gender {
if self.gender_rate < 0.0 {
Gender::Genderless
} else if rand.get_float() >= self.gender_rate {
Gender::Female
} else {
Gender::Male
}
}
pub fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key)
}
}