2022-07-01 15:07:22 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::{PkmnResult, StringKey};
|
2022-06-06 11:54:59 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// This trait adds a bunch of helper functions to deal with volatile scripts on a struct.
|
2022-08-20 11:17:20 +00:00
|
|
|
pub trait VolatileScriptsOwner {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Return the [`ScriptSet`] that are our volatile scripts.
|
2022-06-19 10:07:54 +00:00
|
|
|
fn volatile_scripts(&self) -> &Arc<ScriptSet>;
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Loads a volatile script by name.
|
2022-06-18 16:41:23 +00:00
|
|
|
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Arc<dyn Script>>>;
|
2022-06-06 11:54:59 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Check if a volatile script with given name exists.
|
2022-06-11 15:22:46 +00:00
|
|
|
fn has_volatile_script(&self, key: &StringKey) -> bool {
|
2022-06-19 10:07:54 +00:00
|
|
|
self.volatile_scripts().has(key)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets a volatile script by name.
|
2022-06-12 15:57:39 +00:00
|
|
|
fn get_volatile_script(&self, key: &StringKey) -> Option<ScriptContainer> {
|
2022-06-19 10:07:54 +00:00
|
|
|
self.volatile_scripts().get(key)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Adds a volatile script by name.
|
2022-06-12 15:57:39 +00:00
|
|
|
fn add_volatile_script(&mut self, key: &StringKey) -> PkmnResult<Option<ScriptContainer>> {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.volatile_scripts()
|
|
|
|
.stack_or_add(key, &|| self.load_volatile_script(key))
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Removes a volatile script by name.
|
2022-06-11 15:22:46 +00:00
|
|
|
fn remove_volatile_script(&mut self, key: &StringKey) {
|
2022-06-19 10:07:54 +00:00
|
|
|
self.volatile_scripts().remove(key)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
}
|