A lot more work on a bunch of different parts of the system.
This commit is contained in:
1
src/dynamic_data/script_handling/item_script.rs
Normal file
1
src/dynamic_data/script_handling/item_script.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub trait ItemScript {}
|
||||
@@ -2,6 +2,7 @@ use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
||||
use std::sync::Weak;
|
||||
|
||||
pub mod item_script;
|
||||
pub mod script;
|
||||
pub mod script_set;
|
||||
pub mod volatile_scripts;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
use crate::dynamic_data::models::damage_source::DamageSource;
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use crate::static_data::moves::secondary_effect::EffectParameter;
|
||||
use crate::StringKey;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
pub trait Script {
|
||||
fn name(&self) -> &str;
|
||||
fn name(&self) -> &StringKey;
|
||||
|
||||
fn is_suppressed(&self) -> bool {
|
||||
self.get_suppressed_count() > 0
|
||||
@@ -14,7 +17,7 @@ pub trait Script {
|
||||
// FIXME: add missing parameters
|
||||
fn stack(&self);
|
||||
fn on_remove(&self);
|
||||
fn on_initialize(&self);
|
||||
fn on_initialize(&self, pars: &Vec<EffectParameter>);
|
||||
fn on_before_turn(&self);
|
||||
fn change_speed(&self);
|
||||
fn change_priority(&self);
|
||||
@@ -52,9 +55,9 @@ pub trait Script {
|
||||
fn prevent_self_run_away(&self);
|
||||
fn prevent_opponent_run_away(&self);
|
||||
fn on_end_turn(&self);
|
||||
fn on_damage(&self);
|
||||
fn on_faint(&self);
|
||||
fn on_switch_in<'b>(&self, pokemon: &'b Pokemon);
|
||||
fn on_damage(&self, pokemon: &Pokemon, source: DamageSource, old_health: u32, new_health: u32);
|
||||
fn on_faint(&self, pokemon: &Pokemon, source: DamageSource);
|
||||
fn on_switch_in(&self, pokemon: &Pokemon);
|
||||
fn on_after_held_item_consume(&self);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::PkmnResult;
|
||||
use crate::{PkmnResult, StringKey};
|
||||
use indexmap::IndexMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ScriptSet {
|
||||
scripts: IndexMap<String, Arc<Box<dyn Script>>>,
|
||||
scripts: IndexMap<StringKey, Arc<Box<dyn Script>>>,
|
||||
}
|
||||
|
||||
impl ScriptSet {
|
||||
@@ -15,33 +15,37 @@ impl ScriptSet {
|
||||
return existing.clone();
|
||||
}
|
||||
let arc = Arc::new(script);
|
||||
self.scripts.insert(arc.name().to_string(), arc.clone());
|
||||
self.scripts.insert(arc.name().clone(), arc.clone());
|
||||
self.scripts.last().unwrap().1.clone()
|
||||
}
|
||||
|
||||
pub fn stack_or_add<'b, F>(
|
||||
&mut self,
|
||||
key: &str,
|
||||
key: &StringKey,
|
||||
instantiation: &'b F,
|
||||
) -> PkmnResult<Arc<Box<dyn Script>>>
|
||||
) -> PkmnResult<Option<Arc<Box<dyn Script>>>>
|
||||
where
|
||||
F: Fn() -> PkmnResult<Box<dyn Script>>,
|
||||
F: Fn() -> PkmnResult<Option<Box<dyn Script>>>,
|
||||
{
|
||||
if let Some(existing) = self.scripts.get(key) {
|
||||
existing.stack();
|
||||
return Ok(existing.clone());
|
||||
return Ok(Some(existing.clone()));
|
||||
}
|
||||
let script = instantiation()?;
|
||||
let arc = Arc::new(script);
|
||||
self.scripts.insert(arc.name().to_string(), arc.clone());
|
||||
Ok(self.scripts.last().unwrap().1.clone())
|
||||
if let Some(script) = script {
|
||||
let arc = Arc::new(script);
|
||||
self.scripts.insert(arc.name().clone(), arc.clone());
|
||||
Ok(Some(self.scripts.last().unwrap().1.clone()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &str) -> Option<&Arc<Box<dyn Script>>> {
|
||||
pub fn get(&self, key: &StringKey) -> Option<&Arc<Box<dyn Script>>> {
|
||||
self.scripts.get(key)
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, key: &str) {
|
||||
pub fn remove(&mut self, key: &StringKey) {
|
||||
let value = self.scripts.shift_remove(key);
|
||||
if let Some(script) = value {
|
||||
script.on_remove();
|
||||
@@ -55,7 +59,7 @@ impl ScriptSet {
|
||||
self.scripts.clear();
|
||||
}
|
||||
|
||||
pub fn has(&self, key: &str) -> bool {
|
||||
pub fn has(&self, key: &StringKey) -> bool {
|
||||
self.scripts.contains_key(key)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
||||
use crate::PkmnResult;
|
||||
use crate::{PkmnResult, StringKey};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub trait VolatileScripts<'a> {
|
||||
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>>;
|
||||
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>>;
|
||||
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>>;
|
||||
|
||||
fn has_volatile_script(&self, key: &str) -> bool {
|
||||
fn has_volatile_script(&self, key: &StringKey) -> bool {
|
||||
self.volatile_scripts().read().unwrap().has(key)
|
||||
}
|
||||
|
||||
fn get_volatile_script(&self, key: &str) -> Option<Arc<Box<dyn Script>>> {
|
||||
fn get_volatile_script(&self, key: &StringKey) -> Option<Arc<Box<dyn Script>>> {
|
||||
let scripts = self.volatile_scripts().read().unwrap();
|
||||
let s = scripts.get(key);
|
||||
s.cloned()
|
||||
}
|
||||
|
||||
fn add_volatile_script(&mut self, key: &str) -> PkmnResult<Arc<Box<dyn Script>>> {
|
||||
fn add_volatile_script(&mut self, key: &StringKey) -> PkmnResult<Option<Arc<Box<dyn Script>>>> {
|
||||
self.volatile_scripts()
|
||||
.write()
|
||||
.unwrap()
|
||||
.stack_or_add(key, &|| self.load_volatile_script(key))
|
||||
}
|
||||
|
||||
fn remove_volatile_script(&mut self, key: &str) {
|
||||
fn remove_volatile_script(&mut self, key: &StringKey) {
|
||||
self.volatile_scripts().write().unwrap().remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user