use crate::app_interface::Statistic; use crate::{ExternRef, ExternalReferenceType}; use core::convert::{TryFrom, TryInto}; use core::fmt::Debug; use core::marker::PhantomData; #[derive(Clone)] pub struct StatisticSet where T: TryFrom, T: TryInto, { reference: ExternRef, _p: PhantomData, } impl StatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { pub(crate) fn new(reference: ExternRef) -> 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 { 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 { 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) { unsafe { 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) { unsafe { statistic_set_decrease_stat(self.reference.cast(), stat, value.try_into().unwrap()) } } } impl ExternalReferenceType for StatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { fn from_extern_value(reference: ExternRef) -> Self { StatisticSet::::new(reference) } } #[derive(Clone)] pub struct ClampedStatisticSet where T: TryFrom, T: TryInto, { reference: ExternRef, _p: PhantomData, } impl ClampedStatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { pub(crate) fn new(reference: ExternRef) -> 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 ExternalReferenceType for ClampedStatisticSet where T: TryFrom, T: TryInto, >::Error: Debug, >::Error: Debug, { fn from_extern_value(reference: ExternRef) -> Self { ClampedStatisticSet::::new(reference) } } #[cfg(not(feature = "mock_data"))] extern "wasm" { fn statistic_set_get(r: ExternRef>, stat: Statistic) -> i64; fn statistic_set_set(r: ExternRef>, stat: Statistic, value: i64); fn statistic_set_increase_stat(r: ExternRef>, stat: Statistic, value: i64); fn statistic_set_decrease_stat(r: ExternRef>, stat: Statistic, value: i64); fn clamped_statistic_set_get(r: ExternRef>, stat: Statistic) -> i64; fn clamped_statistic_set_set( r: ExternRef>, stat: Statistic, value: i64, ); fn clamped_statistic_set_increase_stat( r: ExternRef>, stat: Statistic, value: i64, ) -> bool; fn clamped_statistic_set_decrease_stat( r: ExternRef>, stat: Statistic, value: i64, ) -> bool; }