Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

use crate::app_interface::{Party, PartyImpl};
2022-08-14 11:37:17 +00:00
use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable;
use crate::{cached_value, cached_value_getters, ExternRef, ExternalReferenceType};
use alloc::rc::Rc;
pub trait BattlePartyTrait {
fn party(&self) -> Party;
}
pub type BattleParty = Rc<dyn BattlePartyTrait>;
2022-08-14 11:37:17 +00:00
struct BattlePartyInner {
reference: ExternRef<BattlePartyImpl>,
party: CachedValue<Rc<PartyImpl>>,
2022-08-14 11:37:17 +00:00
}
#[derive(Clone)]
pub struct BattlePartyImpl {
2022-08-14 11:37:17 +00:00
inner: Rc<BattlePartyInner>,
}
#[cfg(not(feature = "mock_data"))]
impl BattlePartyImpl {
pub fn new(reference: ExternRef<BattlePartyImpl>) -> Self {
2022-08-14 11:37:17 +00:00
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattlePartyInner {
reference,
party: cached_value!({
Rc::new(battle_party_get_party(reference).get_value().unwrap())
}),
2022-08-14 11:37:17 +00:00
}),
})
}
}
2022-08-14 11:37:17 +00:00
#[cfg(not(feature = "mock_data"))]
impl BattlePartyTrait for BattlePartyImpl {
2022-08-14 11:37:17 +00:00
cached_value_getters! {
fn party(&self) -> Party;
2022-08-14 11:37:17 +00:00
}
}
crate::handling::cacheable::cacheable!(BattlePartyImpl);
2022-08-14 11:37:17 +00:00
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for BattlePartyImpl {
2022-08-14 11:37:17 +00:00
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
2022-08-14 11:37:17 +00:00
extern "wasm" {
fn battle_party_get_party(r: ExternRef<BattlePartyImpl>) -> ExternRef<PartyImpl>;
2022-08-14 11:37:17 +00:00
}