This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,12 @@ pub trait ScriptResolver: Debug + ValueIdentifiable {
|
||||
fn load_item_script(&self, _key: &Item) -> PkmnResult<Option<Arc<dyn ItemScript>>>;
|
||||
}
|
||||
|
||||
use std::fmt::Display;
|
||||
/// A script category defines a sub-group of scripts. This can be used to have multiple scripts with
|
||||
/// the same name, but a different script. It should be completely valid for a move to have the same
|
||||
/// name as an ability, or more commonly: for a script attached to a Pokemon to have the same name as
|
||||
/// a move that placed it there.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Display)]
|
||||
#[repr(u8)]
|
||||
pub enum ScriptCategory {
|
||||
/// A script that belongs to a move. This generally is only the script that is attached to a
|
||||
|
||||
@@ -158,7 +158,7 @@ impl Battle {
|
||||
pub fn current_turn(&self) -> u32 {
|
||||
self.current_turn.load(Ordering::Relaxed)
|
||||
}
|
||||
/// The time the last turn took to run. Defaults to 0.
|
||||
/// The time in nanoseconds the last turn took to run. Defaults to 0.
|
||||
pub fn last_turn_time(&self) -> u64 {
|
||||
self.last_turn_time.load(Ordering::Relaxed)
|
||||
}
|
||||
@@ -261,7 +261,7 @@ impl Battle {
|
||||
}
|
||||
|
||||
/// Try and set the choice for the battle. If the choice is not valid, this returns false.
|
||||
pub fn try_set_choice(&mut self, choice: TurnChoice) -> PkmnResult<bool> {
|
||||
pub fn try_set_choice(&self, choice: TurnChoice) -> PkmnResult<bool> {
|
||||
if !self.can_use(&choice) {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -399,7 +399,7 @@ impl ValueIdentifiable for Battle {
|
||||
}
|
||||
|
||||
/// The result of a battle.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum BattleResult {
|
||||
/// The battle has no winner. Either the battle has not ended, or everyone is dead, or one of
|
||||
/// the parties has ran away.
|
||||
|
||||
@@ -4,13 +4,15 @@ use std::sync::{Arc, Mutex};
|
||||
use crate::dynamic_data::models::executing_move::ExecutingMove;
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use crate::dynamic_data::script_handling::ScriptSource;
|
||||
use crate::script_hook;
|
||||
use crate::utils::Random;
|
||||
use crate::{script_hook, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// The RNG for a battle.
|
||||
#[derive(Default)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct BattleRandom {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The actual underlying RNG. This is in a mutex, so it is thread safe, and can be ran
|
||||
/// predictably, with guaranteed the same outputs.
|
||||
random: Mutex<Random>,
|
||||
@@ -20,6 +22,7 @@ impl BattleRandom {
|
||||
/// Initializes a new RNG with a given seed.
|
||||
pub fn new_with_seed(seed: u128) -> Self {
|
||||
BattleRandom {
|
||||
identifier: Default::default(),
|
||||
random: Mutex::new(Random::new(seed)),
|
||||
}
|
||||
}
|
||||
@@ -89,7 +92,14 @@ impl Debug for BattleRandom {
|
||||
impl Clone for BattleRandom {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
random: Mutex::new(self.random.lock().unwrap().clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for BattleRandom {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,14 @@ use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, Scrip
|
||||
use crate::dynamic_data::Script;
|
||||
use crate::dynamic_data::ScriptSet;
|
||||
use crate::dynamic_data::VolatileScriptsOwner;
|
||||
use crate::{script_hook, PkmnResult, StringKey};
|
||||
use crate::{script_hook, PkmnResult, StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
|
||||
/// A side on a battle.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct BattleSide {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The index of the side on the battle.
|
||||
index: u8,
|
||||
/// The number of Pokemon that can be on the side.
|
||||
@@ -60,6 +62,7 @@ impl BattleSide {
|
||||
let pokemon = RwLock::new(pokemon);
|
||||
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
index,
|
||||
pokemon_per_side,
|
||||
pokemon,
|
||||
@@ -322,3 +325,9 @@ impl ScriptSource for BattleSide {
|
||||
self.battle().collect_scripts(scripts);
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for BattleSide {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user