217 lines
5.9 KiB
Rust
Executable File
217 lines
5.9 KiB
Rust
Executable File
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<T>
|
|
where
|
|
T: TryFrom<i64>,
|
|
T: TryInto<i64>,
|
|
{
|
|
reference: ExternRef<Self>,
|
|
_p: PhantomData<T>,
|
|
}
|
|
|
|
impl<T> StatisticSet<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 {
|
|
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<T> ExternalReferenceType for StatisticSet<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 {
|
|
StatisticSet::<T>::new(reference)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ClampedStatisticSet<T>
|
|
where
|
|
T: TryFrom<i64>,
|
|
T: TryInto<i64>,
|
|
{
|
|
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<StatisticSet<i64>>, stat: Statistic) -> i64;
|
|
fn statistic_set_set(r: ExternRef<StatisticSet<i64>>, stat: Statistic, value: i64);
|
|
fn statistic_set_increase_stat(r: ExternRef<StatisticSet<i64>>, stat: Statistic, value: i64);
|
|
fn statistic_set_decrease_stat(r: ExternRef<StatisticSet<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;
|
|
}
|