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

243 lines
6.5 KiB
Rust
Executable File

use crate::app_interface::Statistic;
use crate::{ExternRef, ExternalReferenceType};
use alloc::rc::Rc;
use core::convert::{TryFrom, TryInto};
use core::fmt::Debug;
use core::marker::PhantomData;
pub trait StatisticSetTrait<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn hp(&self) -> T {
self.get_stat(Statistic::Attack)
}
fn attack(&self) -> T {
self.get_stat(Statistic::Attack)
}
fn defense(&self) -> T {
self.get_stat(Statistic::Defense)
}
fn special_attack(&self) -> T {
self.get_stat(Statistic::SpecialAttack)
}
fn special_defense(&self) -> T {
self.get_stat(Statistic::SpecialDefense)
}
fn speed(&self) -> T {
self.get_stat(Statistic::Speed)
}
fn get_stat(&self, stat: Statistic) -> T;
fn set_stat(&self, stat: Statistic, value: T);
fn increase_stat(&self, stat: Statistic, value: T);
fn decrease_stat(&self, stat: Statistic, value: T);
}
pub type StatisticSet<T> = Rc<dyn StatisticSetTrait<T>>;
#[derive(Clone)]
pub struct StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
reference: ExternRef<Self>,
_p: PhantomData<T>,
}
#[cfg(not(feature = "mock_data"))]
impl<T> StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self {
reference,
_p: Default::default(),
}
}
}
#[cfg(not(feature = "mock_data"))]
impl<T> StatisticSetTrait<T> for StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn get_stat(&self, stat: Statistic) -> T {
unsafe {
statistic_set_get(self.reference.cast(), stat)
.try_into()
.unwrap()
}
}
fn set_stat(&self, stat: Statistic, value: T) {
unsafe { statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) }
}
fn increase_stat(&self, stat: Statistic, value: T) {
unsafe {
statistic_set_increase_stat(self.reference.cast(), stat, value.try_into().unwrap())
}
}
fn decrease_stat(&self, stat: Statistic, value: T) {
unsafe {
statistic_set_decrease_stat(self.reference.cast(), stat, value.try_into().unwrap())
}
}
}
#[cfg(not(feature = "mock_data"))]
impl<T> ExternalReferenceType for StatisticSetImpl<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn from_extern_value(reference: ExternRef<Self>) -> Self {
StatisticSetImpl::<T>::new(reference)
}
}
#[derive(Clone)]
pub struct ClampedStatisticSet<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
reference: ExternRef<Self>,
_p: PhantomData<T>,
}
impl<T> ClampedStatisticSet<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self {
reference,
_p: Default::default(),
}
}
#[cfg(not(feature = "mock_data"))]
pub fn hp(&self) -> T {
self.get_stat(Statistic::HP)
}
#[cfg(not(feature = "mock_data"))]
pub fn attack(&self) -> T {
self.get_stat(Statistic::Attack)
}
#[cfg(not(feature = "mock_data"))]
pub fn defense(&self) -> T {
self.get_stat(Statistic::Defense)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_attack(&self) -> T {
self.get_stat(Statistic::SpecialAttack)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_defense(&self) -> T {
self.get_stat(Statistic::SpecialDefense)
}
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> T {
self.get_stat(Statistic::Speed)
}
#[cfg(not(feature = "mock_data"))]
pub fn get_stat(&self, stat: Statistic) -> T {
unsafe {
clamped_statistic_set_get(self.reference.cast(), stat)
.try_into()
.unwrap()
}
}
#[cfg(not(feature = "mock_data"))]
pub fn set_stat(&self, stat: Statistic, value: T) {
unsafe { clamped_statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) }
}
#[cfg(not(feature = "mock_data"))]
pub fn increase_stat(&self, stat: Statistic, value: T) -> bool {
unsafe {
clamped_statistic_set_increase_stat(
self.reference.cast(),
stat,
value.try_into().unwrap(),
)
}
}
#[cfg(not(feature = "mock_data"))]
pub fn decrease_stat(&self, stat: Statistic, value: T) -> bool {
unsafe {
clamped_statistic_set_decrease_stat(
self.reference.cast(),
stat,
value.try_into().unwrap(),
)
}
}
}
impl<T> ExternalReferenceType for ClampedStatisticSet<T>
where
T: TryFrom<i64>,
T: TryInto<i64>,
<T as TryFrom<i64>>::Error: Debug,
<T as TryInto<i64>>::Error: Debug,
{
fn from_extern_value(reference: ExternRef<Self>) -> Self {
ClampedStatisticSet::<T>::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn statistic_set_get(r: ExternRef<StatisticSetImpl<i64>>, stat: Statistic) -> i64;
fn statistic_set_set(r: ExternRef<StatisticSetImpl<i64>>, stat: Statistic, value: i64);
fn statistic_set_increase_stat(
r: ExternRef<StatisticSetImpl<i64>>,
stat: Statistic,
value: i64,
);
fn statistic_set_decrease_stat(
r: ExternRef<StatisticSetImpl<i64>>,
stat: Statistic,
value: i64,
);
fn clamped_statistic_set_get(r: ExternRef<ClampedStatisticSet<i64>>, stat: Statistic) -> i64;
fn clamped_statistic_set_set(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
value: i64,
);
fn clamped_statistic_set_increase_stat(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
value: i64,
) -> bool;
fn clamped_statistic_set_decrease_stat(
r: ExternRef<ClampedStatisticSet<i64>>,
stat: Statistic,
value: i64,
) -> bool;
}