PkmnLib_rs/src/dynamic_data/event_hooks.rs

133 lines
4.6 KiB
Rust
Executable File

use std::fmt::{Debug, Formatter};
use std::sync::Arc;
use crate::dynamic_data::DamageSource;
use crate::dynamic_data::ExecutingMove;
use crate::dynamic_data::Pokemon;
use crate::static_data::Species;
use crate::static_data::{Form, Statistic};
/// The event hook is used to store external functions that listen to events.
///
/// Events happen in many
/// different places in the battle, and can be used for GUI applications to show that these events
/// are happening.
#[derive(Default)]
pub struct EventHook {
/// All the registered event listeners on the hook.
evt_hook_function: Vec<fn(&Box<&Event>)>,
}
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);
}
/// 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 {
f(&b);
}
}
}
impl Debug for EventHook {
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}
/// The different events that can occur during the battle, for which a GUI should show something.
#[derive(Debug)]
pub enum Event<'own> {
/// A switch event happens when a Pokemon gets switched out for something else.
Switch {
/// The side the Pokemon got switched from/on
side_index: u8,
/// The index of the Pokemon that got switched in/out on its side
index: u8,
/// The new Pokemon that will be on the spot. If none, the spot will now be empty.
pokemon: Option<&'own Pokemon>,
},
/// A swap event happens when two Pokemon on a side swap positions. Note that this is rare.
Swap {
/// The side the Pokemon swapped on.
side_index: u8,
/// The index on the side of the first Pokemon that was swapped.
index_a: u8,
/// The index on the side of the second pokemon that was swapped.
index_b: u8,
},
/// This event happens when a Pokemon changes species during battle. While not normally occuring
/// this can be useful for things such as mid-battle evolutions, which some fanmade implementations
/// enjoy.
SpeciesChange {
/// The pokemon that changed species.
pokemon: &'own Pokemon,
/// The new species of the Pokemon.
species: Arc<Species>,
/// The form of the species the Pokemon will have.
form: Arc<Form>,
},
/// This event happens when a Pokemon changes form during battle. This is rather common.
FormChange {
/// The pokemon that changed forms.
pokemon: &'own Pokemon,
/// The new form of the Pokemon.
form: &'own Form,
},
/// This event happens when a Pokemon takes damage.
Damage {
/// The Pokemon that takes damage.
pokemon: &'own Pokemon,
/// The source of damage.
source: DamageSource,
/// The health of the Pokemon before the damage.
original_health: u32,
/// The health of the Pokemon after the damage.
new_health: u32,
},
/// This event happens when a Pokemon gets healed
Heal {
/// The Pokemon that gets healed.
pokemon: &'own Pokemon,
/// The health of the Pokemon before the heal.
original_health: u32,
/// The health of the Pokemon after the heal.
new_health: u32,
},
/// This event happens when a Pokemon faints.
Faint {
/// The pokemon that has fainted.
pokemon: &'own Pokemon,
},
/// This event happens when a Pokemon uses a move on a target, just before any hits.
MoveUse {
/// The data of the move used.
executing_move: &'own ExecutingMove,
},
/// This event happens when a Pokemon missed.
Miss {
/// The pokemon that missed.
user: &'own Pokemon,
},
/// The turn is finished running, waiting for new input.
EndTurn,
/// A pokemon had its stat boost changed
StatBoostChange {
/// The pokemon that had its stat boosts changed.
user: &'own Pokemon,
/// The statistic that changed.
stat: Statistic,
/// The value of the stat before the change.
old_value: i8,
/// The value of the stat after the change.
new_value: i8,
},
}