2022-10-15 15:21:24 +00:00
|
|
|
use parking_lot::RwLock;
|
2022-06-19 19:34:08 +00:00
|
|
|
use std::fmt::{Debug, Formatter};
|
2022-08-20 11:17:20 +00:00
|
|
|
use std::sync::Arc;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
|
|
|
use crate::dynamic_data::DamageSource;
|
|
|
|
use crate::dynamic_data::ExecutingMove;
|
|
|
|
use crate::dynamic_data::Pokemon;
|
|
|
|
use crate::static_data::Species;
|
2022-06-27 16:26:27 +00:00
|
|
|
use crate::static_data::{Form, Statistic};
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2022-10-15 15:21:24 +00:00
|
|
|
/// 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>;
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// 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.
|
2022-10-15 15:21:24 +00:00
|
|
|
evt_hook_function: RwLock<EvtHookCollection>,
|
2022-06-19 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 16:23:35 +00:00
|
|
|
impl EventHook {
|
2022-06-19 19:34:08 +00:00
|
|
|
/// 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.
|
2022-10-15 15:21:24 +00:00
|
|
|
pub fn register_listener(&self, func: EvtHookFn) {
|
|
|
|
self.evt_hook_function.write().push(func);
|
2022-06-19 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run a new event. This will send the event to all externally defined event listeners. It will
|
|
|
|
/// dispose of the event afterwards.
|
2022-08-20 11:17:20 +00:00
|
|
|
pub fn trigger(&self, evt: Event) {
|
2022-06-19 19:34:08 +00:00
|
|
|
let b = Box::new(&evt);
|
2022-10-15 15:21:24 +00:00
|
|
|
for f in self.evt_hook_function.read().iter() {
|
2022-06-19 19:34:08 +00:00
|
|
|
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)]
|
2022-08-20 11:17:20 +00:00
|
|
|
pub enum Event<'own> {
|
2022-06-19 19:34:08 +00:00
|
|
|
/// 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.
|
2022-11-26 10:25:56 +00:00
|
|
|
pokemon: Option<Arc<Pokemon>>,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// 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.
|
2022-08-20 11:17:20 +00:00
|
|
|
pokemon: &'own Pokemon,
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The new species of the Pokemon.
|
2022-11-28 20:34:28 +00:00
|
|
|
species: Arc<dyn Species>,
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The form of the species the Pokemon will have.
|
2022-11-28 20:34:28 +00:00
|
|
|
form: Arc<dyn Form>,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// This event happens when a Pokemon changes form during battle. This is rather common.
|
|
|
|
FormChange {
|
|
|
|
/// The pokemon that changed forms.
|
2022-08-20 11:17:20 +00:00
|
|
|
pokemon: &'own Pokemon,
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The new form of the Pokemon.
|
2022-11-28 20:34:28 +00:00
|
|
|
form: Arc<dyn Form>,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// This event happens when a Pokemon takes damage.
|
|
|
|
Damage {
|
|
|
|
/// The Pokemon that takes damage.
|
2022-08-20 11:17:20 +00:00
|
|
|
pokemon: &'own Pokemon,
|
2022-06-19 19:34:08 +00:00
|
|
|
/// 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,
|
2022-08-20 10:22:12 +00:00
|
|
|
},
|
|
|
|
/// This event happens when a Pokemon gets healed
|
|
|
|
Heal {
|
|
|
|
/// The Pokemon that gets healed.
|
2022-08-20 11:17:20 +00:00
|
|
|
pokemon: &'own Pokemon,
|
2022-08-20 10:22:12 +00:00
|
|
|
/// The health of the Pokemon before the heal.
|
|
|
|
original_health: u32,
|
|
|
|
/// The health of the Pokemon after the heal.
|
|
|
|
new_health: u32,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// This event happens when a Pokemon faints.
|
|
|
|
Faint {
|
|
|
|
/// The pokemon that has fainted.
|
2022-08-20 11:17:20 +00:00
|
|
|
pokemon: &'own Pokemon,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// This event happens when a Pokemon uses a move on a target, just before any hits.
|
|
|
|
MoveUse {
|
|
|
|
/// The data of the move used.
|
2022-08-20 11:17:20 +00:00
|
|
|
executing_move: &'own ExecutingMove,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// This event happens when a Pokemon missed.
|
|
|
|
Miss {
|
|
|
|
/// The pokemon that missed.
|
2022-08-20 11:17:20 +00:00
|
|
|
user: &'own Pokemon,
|
2022-06-19 19:34:08 +00:00
|
|
|
},
|
|
|
|
/// The turn is finished running, waiting for new input.
|
|
|
|
EndTurn,
|
2022-06-27 16:26:27 +00:00
|
|
|
/// A pokemon had its stat boost changed
|
|
|
|
StatBoostChange {
|
|
|
|
/// The pokemon that had its stat boosts changed.
|
2022-08-20 11:17:20 +00:00
|
|
|
user: &'own Pokemon,
|
2022-06-27 16:26:27 +00:00
|
|
|
/// 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,
|
|
|
|
},
|
2022-06-19 19:34:08 +00:00
|
|
|
}
|