112 lines
4.1 KiB
Rust
112 lines
4.1 KiB
Rust
|
use std::fmt::{Debug, Formatter};
|
||
|
|
||
|
use crate::dynamic_data::DamageSource;
|
||
|
use crate::dynamic_data::ExecutingMove;
|
||
|
use crate::dynamic_data::Pokemon;
|
||
|
use crate::static_data::Form;
|
||
|
use crate::static_data::Species;
|
||
|
|
||
|
/// 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<'battle, 'library> 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<'b>(&self, evt: Event<'b, 'battle, 'library>) {
|
||
|
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, 'battle, 'library> {
|
||
|
/// 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<'battle, 'library>>,
|
||
|
},
|
||
|
/// 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<'battle, 'library>,
|
||
|
/// The new species of the Pokemon.
|
||
|
species: &'own Species,
|
||
|
/// The form of the species the Pokemon will have.
|
||
|
form: &'own Form,
|
||
|
},
|
||
|
/// This event happens when a Pokemon changes form during battle. This is rather common.
|
||
|
FormChange {
|
||
|
/// The pokemon that changed forms.
|
||
|
pokemon: &'own Pokemon<'battle, 'library>,
|
||
|
/// 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<'battle, 'library>,
|
||
|
/// 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 faints.
|
||
|
Faint {
|
||
|
/// The pokemon that has fainted.
|
||
|
pokemon: &'own Pokemon<'battle, 'library>,
|
||
|
},
|
||
|
/// 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<'own, 'battle, 'library>,
|
||
|
},
|
||
|
/// This event happens when a Pokemon missed.
|
||
|
Miss {
|
||
|
/// The pokemon that missed.
|
||
|
user: &'own Pokemon<'battle, 'library>,
|
||
|
},
|
||
|
/// The turn is finished running, waiting for new input.
|
||
|
EndTurn,
|
||
|
}
|