Initial commit, implements most of the static_data side.
This commit is contained in:
62
src/static_data/species_data/species.rs
Normal file
62
src/static_data/species_data/species.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use self::super::form::Form;
|
||||
use crate::static_data::species_data::gender::Gender;
|
||||
use crate::utils::random::Random;
|
||||
use derive_getters::Getters;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[derive(Debug, Getters)]
|
||||
pub struct Species<'a> {
|
||||
id: u16,
|
||||
name: String,
|
||||
gender_rate: f32,
|
||||
growth_rate: String,
|
||||
capture_rate: u8,
|
||||
forms: HashMap<String, Form<'a>>,
|
||||
flags: HashSet<String>,
|
||||
}
|
||||
|
||||
impl<'a> Species<'a> {
|
||||
pub fn new(
|
||||
id: u16,
|
||||
name: &str,
|
||||
gender_rate: f32,
|
||||
growth_rate: &str,
|
||||
capture_rate: u8,
|
||||
default_form: Form<'a>,
|
||||
flags: HashSet<String>,
|
||||
) -> Species<'a> {
|
||||
Species {
|
||||
id,
|
||||
name: name.to_string(),
|
||||
gender_rate,
|
||||
growth_rate: growth_rate.to_string(),
|
||||
capture_rate,
|
||||
forms: hashmap! {
|
||||
"default".to_string() => default_form,
|
||||
},
|
||||
flags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_form(&mut self, id: String, form: Form<'a>) {
|
||||
self.forms.insert(id, form);
|
||||
}
|
||||
|
||||
pub fn get_form(&self, id: &str) -> Option<&Form> {
|
||||
self.forms.get(id)
|
||||
}
|
||||
|
||||
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: &str) -> bool {
|
||||
self.flags.contains(key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user