A lot more work on a bunch of different parts of the system.

This commit is contained in:
2022-06-11 17:22:46 +02:00
parent 10e93949e4
commit 6e8f4dd4a5
35 changed files with 735 additions and 197 deletions

View File

@@ -1,4 +1,5 @@
use crate::static_data::statistics::Statistic;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
@@ -45,7 +46,7 @@ impl Nature {
#[derive(Debug)]
pub struct NatureLibrary {
map: HashMap<String, Nature>,
map: HashMap<StringKey, Nature>,
}
impl NatureLibrary {
@@ -55,20 +56,20 @@ impl NatureLibrary {
}
}
pub fn load_nature(&mut self, name: &str, nature: Nature) {
self.map.insert(name.to_string(), nature);
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
self.map.insert(name, nature);
}
pub fn get_nature(&self, key: &str) -> Option<&Nature> {
pub fn get_nature(&self, key: &StringKey) -> Option<&Nature> {
self.map.get(key)
}
pub fn get_nature_name(&self, nature: &Nature) -> String {
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
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();
return kv.0.clone();
}
}
panic!("No name was found for the given nature. This should never happen.");
@@ -84,7 +85,7 @@ pub mod tests {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"test_nature",
"test_nature".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
@@ -95,14 +96,14 @@ pub mod tests {
fn create_nature_library_insert_and_retrieve() {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"foo",
"foo".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib.load_nature(
"bar",
"bar".into(),
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature("foo").expect("Nature was not found");
let n1 = lib.get_nature(&"foo".into()).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);
@@ -113,19 +114,19 @@ pub mod tests {
fn create_nature_library_insert_and_get_name() {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"foo",
"foo".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib.load_nature(
"bar",
"bar".into(),
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature("foo").expect("Nature was not found");
let n1 = lib.get_nature(&"foo".into()).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");
assert_eq!(name, "foo".into());
let n2 = lib.get_nature(&"bar".into()).expect("Nature was not found");
let name2 = lib.get_nature_name(n2);
assert_eq!(name2, "bar");
assert_eq!(name2, "bar".into());
}
}