This commit is contained in:
@@ -9,7 +9,7 @@ pub struct NatureLibrary {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The underlying data structure.
|
||||
map: HashMap<StringKey, Arc<Nature>>,
|
||||
map: HashMap<StringKey, Arc<dyn Nature>>,
|
||||
}
|
||||
|
||||
impl NatureLibrary {
|
||||
@@ -22,21 +22,21 @@ impl NatureLibrary {
|
||||
}
|
||||
|
||||
/// Adds a new nature with name to the library.
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Arc<Nature>) {
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>) {
|
||||
self.map.insert(name, nature);
|
||||
}
|
||||
|
||||
/// Gets a nature by name.
|
||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<Nature>> {
|
||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<dyn Nature>> {
|
||||
self.map.get(key)
|
||||
}
|
||||
|
||||
/// Finds a nature name by nature.
|
||||
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
|
||||
pub fn get_nature_name(&self, nature: &Arc<dyn 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 std::ptr::eq(Arc::as_ptr(kv.1), nature) {
|
||||
if kv.1.value_identifier() == nature.value_identifier() {
|
||||
return kv.0.clone();
|
||||
}
|
||||
}
|
||||
@@ -53,14 +53,14 @@ impl ValueIdentifiable for NatureLibrary {
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::static_data::statistics::Statistic;
|
||||
use crate::static_data::{Nature, NatureLibrary};
|
||||
use crate::static_data::{NatureImpl, NatureLibrary};
|
||||
|
||||
pub fn build() -> NatureLibrary {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
|
||||
lib.load_nature(
|
||||
"test_nature".into(),
|
||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
|
||||
lib
|
||||
@@ -69,10 +69,13 @@ pub mod tests {
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_retrieve() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
||||
lib.load_nature(
|
||||
"foo".into(),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
lib.load_nature(
|
||||
"bar".into(),
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
NatureImpl::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||
assert_eq!(n1.increased_stat(), Statistic::HP);
|
||||
@@ -84,10 +87,13 @@ pub mod tests {
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_get_name() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
||||
lib.load_nature(
|
||||
"foo".into(),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
lib.load_nature(
|
||||
"bar".into(),
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
NatureImpl::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
|
||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||
|
||||
Reference in New Issue
Block a user