PkmnLib_rs/src/dynamic_data/script_handling/mod.rs

87 lines
2.3 KiB
Rust
Raw Normal View History

2022-06-03 14:35:18 +00:00
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet;
pub mod script;
pub mod script_set;
2022-06-03 14:35:18 +00:00
#[macro_export]
macro_rules! script_hook {
($hook_name: ident, $source: ident, $($parameters: expr),*) => {
let mut aggregator = $source.get_script_iterator();
while let Some(script) = aggregator.get_next() {
if script.is_suppressed() {
continue;
}
script.$hook_name($($parameters),*);
}
};
}
pub trait ScriptSource {
fn get_script_iterator(&self) -> ScriptAggregator {
todo!()
}
fn get_script_count(&self);
}
pub enum ScriptWrapper<'a> {
Script(&'a Box<dyn Script>),
Set(&'a ScriptSet),
}
pub struct ScriptAggregator<'a> {
scripts: Vec<Option<ScriptWrapper<'a>>>,
size: i32,
index: i32,
set_index: i32,
}
impl<'a> ScriptAggregator<'a> {
pub fn new(scripts: Vec<Option<ScriptWrapper<'a>>>) -> Self {
let len = scripts.len();
Self {
scripts,
size: len as i32,
index: -1,
set_index: -1,
}
}
fn increment_to_next_value(&mut self) -> bool {
if self.index != -1 {
if let Some(wrapper) = &self.scripts[self.index as usize] {
if let ScriptWrapper::Set(set) = wrapper {
self.set_index += 1;
if self.set_index as usize >= set.count() {
self.set_index = -1;
} else {
return true;
}
}
}
}
self.index += 1;
for index in self.index..self.size {
self.index = index;
if let Some(wrapper) = &self.scripts[index as usize] {
if let ScriptWrapper::Set(..) = wrapper {
self.set_index = 0;
}
return true;
}
}
false
}
pub fn get_next(&mut self) -> Option<&Box<dyn Script>> {
if !self.increment_to_next_value() {
return None;
}
return match self.scripts[self.index as usize].as_ref().unwrap() {
ScriptWrapper::Script(script) => Some(script),
ScriptWrapper::Set(set) => Some(set.at(self.set_index as usize)),
};
}
}