Add function to get random nature, add shiny rate to library settings
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-04-14 10:36:38 +02:00
parent d7b3c0cd8d
commit 3058739ea0
7 changed files with 61 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
use crate::static_data::Nature;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use hashbrown::HashMap;
use crate::{Random, StringKey, ValueIdentifiable, ValueIdentifier};
use indexmap::IndexMap;
use std::fmt::Debug;
use std::sync::Arc;
@@ -10,6 +10,8 @@ pub trait NatureLibrary: Debug + ValueIdentifiable {
fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>);
/// Gets a nature by name.
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>>;
/// Gets a random nature.
fn get_random_nature(&self, rand: &mut Random) -> Arc<dyn Nature>;
/// Finds a nature name by nature.
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey;
}
@@ -20,7 +22,7 @@ pub struct NatureLibraryImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying data structure.
map: HashMap<StringKey, Arc<dyn Nature>>,
map: IndexMap<StringKey, Arc<dyn Nature>>,
}
impl NatureLibraryImpl {
@@ -28,7 +30,7 @@ impl NatureLibraryImpl {
pub fn new(capacity: usize) -> Self {
Self {
identifier: Default::default(),
map: HashMap::with_capacity(capacity),
map: IndexMap::with_capacity(capacity),
}
}
}
@@ -44,6 +46,11 @@ impl NatureLibrary for NatureLibraryImpl {
self.map.get(key).cloned()
}
fn get_random_nature(&self, rand: &mut Random) -> Arc<dyn Nature> {
let i = rand.get_between(0, self.map.len() as i32);
return self.map.get_index(i as usize).unwrap().1.clone();
}
/// Finds a nature name by nature.
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey {
for kv in &self.map {
@@ -87,6 +94,7 @@ pub mod tests {
fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>);
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>>;
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey;
fn get_random_nature(&self, rand: &mut Random) -> Arc<dyn Nature>;
}
impl ValueIdentifiable for NatureLibrary {
fn value_identifier(&self) -> ValueIdentifier{