PkmnLib_rs/src/dynamic_data/libraries/dynamic_library.rs

110 lines
4.4 KiB
Rust
Executable File

use std::ops::Deref;
use std::sync::Arc;
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
use crate::dynamic_data::libraries::damage_library::DamageLibrary;
use crate::dynamic_data::libraries::misc_library::MiscLibrary;
use crate::dynamic_data::libraries::script_resolver::ScriptCategory;
use crate::dynamic_data::{ItemScript, ScriptResolver};
use crate::dynamic_data::{Script, ScriptOwnerData};
use crate::static_data::Item;
use crate::static_data::StaticData;
use crate::{PkmnResult, StringKey};
/// The dynamic library stores a static data library, as well as holding different libraries and
/// calculators that might be customized between different generations and implementations.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct DynamicLibrary {
/// The static data is the immutable storage data for this library.
static_data: StaticData,
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
stat_calculator: Box<dyn BattleStatCalculator>,
/// The damage calculator deals with the calculation of things relating to damage.
damage_calculator: Box<dyn DamageLibrary>,
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
misc_library: Box<dyn MiscLibrary>,
/// The script resolver deals with how to resolve the scripts from specific unique key combinations.
script_resolver: Box<dyn ScriptResolver>,
}
unsafe impl Sync for DynamicLibrary {}
unsafe impl Send for DynamicLibrary {}
impl DynamicLibrary {
/// Instantiates a new DynamicLibrary with given parameters.
pub fn new(
static_data: StaticData,
stat_calculator: Box<dyn BattleStatCalculator>,
damage_calculator: Box<dyn DamageLibrary>,
misc_library: Box<dyn MiscLibrary>,
script_resolver: Box<dyn ScriptResolver>,
) -> Self {
Self {
static_data,
stat_calculator,
damage_calculator,
misc_library,
script_resolver,
}
}
/// The static data is the immutable storage data for this library.
pub fn static_data(&self) -> &StaticData {
&self.static_data
}
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
pub fn stat_calculator(&self) -> &dyn BattleStatCalculator {
self.stat_calculator.deref()
}
/// The damage calculator deals with the calculation of things relating to damage.
pub fn damage_calculator(&self) -> &dyn DamageLibrary {
self.damage_calculator.deref()
}
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
pub fn misc_library(&self) -> &dyn MiscLibrary {
self.misc_library.deref()
}
/// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None.
pub fn load_script(
&self,
owner: ScriptOwnerData,
_category: ScriptCategory,
_key: &StringKey,
) -> PkmnResult<Option<Arc<dyn Script>>> {
self.script_resolver.load_script(owner, _category, _key)
}
/// Loads an item script with the given unique key. If no script can be created with this
/// combinations, returns None. Note that ItemScripts are immutable, as their script should be
/// shared between all different usages.
pub fn load_item_script(&self, _key: &Arc<Item>) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
todo!()
}
}
#[cfg(test)]
pub mod test {
use crate::dynamic_data::libraries::battle_stat_calculator::Gen7BattleStatCalculator;
use crate::dynamic_data::libraries::damage_library::Gen7DamageLibrary;
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::dynamic_data::libraries::misc_library::Gen7MiscLibrary;
use crate::dynamic_data::EmptyScriptResolver;
pub fn build() -> DynamicLibrary {
DynamicLibrary {
static_data: crate::static_data::libraries::static_data::test::build(),
stat_calculator: Box::new(Gen7BattleStatCalculator {}),
damage_calculator: Box::new(Gen7DamageLibrary::new(false)),
misc_library: Box::new(Gen7MiscLibrary::new()),
script_resolver: Box::new(EmptyScriptResolver {}),
}
}
}