Further massive amounts of work
This commit is contained in:
131
src/static_data/natures.rs
Normal file
131
src/static_data/natures.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use crate::static_data::statistics::Statistic;
|
||||
use hashbrown::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Nature {
|
||||
increase_stat: Statistic,
|
||||
decrease_stat: Statistic,
|
||||
increase_modifier: f32,
|
||||
decrease_modifier: f32,
|
||||
}
|
||||
|
||||
impl Nature {
|
||||
pub fn new(
|
||||
increase_stat: Statistic,
|
||||
decrease_stat: Statistic,
|
||||
increase_modifier: f32,
|
||||
decrease_modifier: f32,
|
||||
) -> Self {
|
||||
Self {
|
||||
increase_stat,
|
||||
decrease_stat,
|
||||
increase_modifier,
|
||||
decrease_modifier,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn increased_stat(&self) -> Statistic {
|
||||
self.increase_stat
|
||||
}
|
||||
|
||||
pub fn decreased_stat(&self) -> Statistic {
|
||||
self.decrease_stat
|
||||
}
|
||||
|
||||
pub fn get_stat_modifier(&self, stat: Statistic) -> f32 {
|
||||
if stat == self.increase_stat {
|
||||
self.increase_modifier
|
||||
} else if stat == self.decrease_stat {
|
||||
self.decrease_modifier
|
||||
} else {
|
||||
1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NatureLibrary {
|
||||
map: HashMap<String, Nature>,
|
||||
}
|
||||
|
||||
impl NatureLibrary {
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
NatureLibrary {
|
||||
map: HashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_nature(&mut self, name: &str, nature: Nature) {
|
||||
self.map.insert(name.to_string(), nature);
|
||||
}
|
||||
|
||||
pub fn get_nature(&self, key: &str) -> Option<&Nature> {
|
||||
self.map.get(key)
|
||||
}
|
||||
|
||||
pub fn get_nature_name(&self, nature: &Nature) -> String {
|
||||
for kv in &self.map {
|
||||
// As natures can't be copied, and should always be the same reference as the value
|
||||
// in the map, we just compare by reference.
|
||||
if (kv.1 as *const Nature) == (nature as *const Nature) {
|
||||
return kv.0.to_string();
|
||||
}
|
||||
}
|
||||
panic!("No name was found for the given nature. This should never happen.");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::static_data::natures::{Nature, NatureLibrary};
|
||||
use crate::static_data::statistics::Statistic;
|
||||
|
||||
pub fn build() -> NatureLibrary {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
|
||||
lib.load_nature(
|
||||
"test_nature",
|
||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
|
||||
lib
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_retrieve() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature(
|
||||
"foo",
|
||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
lib.load_nature(
|
||||
"bar",
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
let n1 = lib.get_nature("foo").expect("Nature was not found");
|
||||
assert_eq!(n1.increase_stat, Statistic::HP);
|
||||
assert_eq!(n1.decrease_stat, Statistic::Attack);
|
||||
assert_eq!(n1.increase_modifier, 1.1);
|
||||
assert_eq!(n1.decrease_modifier, 0.9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_get_name() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature(
|
||||
"foo",
|
||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
lib.load_nature(
|
||||
"bar",
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
|
||||
let n1 = lib.get_nature("foo").expect("Nature was not found");
|
||||
let name = lib.get_nature_name(n1);
|
||||
assert_eq!(name, "foo");
|
||||
let n2 = lib.get_nature("bar").expect("Nature was not found");
|
||||
let name2 = lib.get_nature_name(n2);
|
||||
assert_eq!(name2, "bar");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user