FFI for Battle
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-10-15 17:21:24 +02:00
parent 6c976216b8
commit ddfb00d36b
10 changed files with 313 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
use parking_lot::RwLock;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
@@ -7,6 +8,11 @@ use crate::dynamic_data::Pokemon;
use crate::static_data::Species;
use crate::static_data::{Form, Statistic};
/// A function that will be called when an event occured.
type EvtHookFn = Box<dyn Fn(&Box<&Event>)>;
/// A collection of event hooks.
type EvtHookCollection = Vec<EvtHookFn>;
/// The event hook is used to store external functions that listen to events.
///
/// Events happen in many
@@ -15,22 +21,22 @@ use crate::static_data::{Form, Statistic};
#[derive(Default)]
pub struct EventHook {
/// All the registered event listeners on the hook.
evt_hook_function: Vec<fn(&Box<&Event>)>,
evt_hook_function: RwLock<EvtHookCollection>,
}
impl EventHook {
/// Register a new listener. This will start receiving all events in the battle. Multiple event
/// listeners can exist at the same time. Note that for these functions the event will be disposed
/// of after the event is finished being sent.
pub fn register_listener(&mut self, func: fn(&Box<&Event>)) {
self.evt_hook_function.push(func);
pub fn register_listener(&self, func: EvtHookFn) {
self.evt_hook_function.write().push(func);
}
/// Run a new event. This will send the event to all externally defined event listeners. It will
/// dispose of the event afterwards.
pub fn trigger(&self, evt: Event) {
let b = Box::new(&evt);
for f in &self.evt_hook_function {
for f in self.evt_hook_function.read().iter() {
f(&b);
}
}