117 lines
3.9 KiB
Rust
117 lines
3.9 KiB
Rust
use alloc::rc::Rc;
|
|
|
|
#[repr(u8)]
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
|
pub enum Statistic {
|
|
HP = 0,
|
|
Attack = 1,
|
|
Defense = 2,
|
|
SpecialAttack = 3,
|
|
SpecialDefense = 4,
|
|
Speed = 5,
|
|
}
|
|
|
|
pub trait ImmutableStatisticSetTrait {
|
|
fn hp(&self) -> u16;
|
|
fn attack(&self) -> u16;
|
|
fn defense(&self) -> u16;
|
|
fn special_attack(&self) -> u16;
|
|
fn special_defense(&self) -> u16;
|
|
fn speed(&self) -> u16;
|
|
}
|
|
|
|
pub type ImmutableStatisticSet = Rc<dyn ImmutableStatisticSetTrait>;
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
mod implementation {
|
|
use super::*;
|
|
use crate::cached_value;
|
|
use crate::handling::cached_value::CachedValue;
|
|
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
|
|
use crate::handling::Cacheable;
|
|
|
|
pub struct ImmutableStatisticSetInner {
|
|
reference: ExternRef<ImmutableStatisticSetImpl>,
|
|
/// The health point stat value.
|
|
hp: CachedValue<u16>,
|
|
/// The physical attack stat value.
|
|
attack: CachedValue<u16>,
|
|
/// The physical defense stat value.
|
|
defense: CachedValue<u16>,
|
|
/// The special attack stat value.
|
|
special_attack: CachedValue<u16>,
|
|
/// The special defense stat value.
|
|
special_defense: CachedValue<u16>,
|
|
/// The speed stat value.
|
|
speed: CachedValue<u16>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ImmutableStatisticSetImpl {
|
|
inner: Rc<ImmutableStatisticSetInner>,
|
|
}
|
|
|
|
impl ImmutableStatisticSetImpl {
|
|
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
|
|
Self::from_ref(reference, &|reference| Self {
|
|
inner: Rc::new(ImmutableStatisticSetInner {
|
|
reference,
|
|
hp: cached_value!({ static_statistics_set_get_hp(reference) }),
|
|
attack: cached_value!({ static_statistics_set_get_attack(reference) }),
|
|
defense: cached_value!({ static_statistics_set_get_defense(reference) }),
|
|
special_attack: cached_value!({
|
|
static_statistics_set_get_special_attack(reference)
|
|
}),
|
|
special_defense: cached_value!({
|
|
static_statistics_set_get_special_defense(reference)
|
|
}),
|
|
speed: cached_value!({ static_statistics_set_get_speed(reference) }),
|
|
}),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl ImmutableStatisticSetTrait for ImmutableStatisticSetImpl {
|
|
fn hp(&self) -> u16 {
|
|
self.inner.hp.value()
|
|
}
|
|
fn attack(&self) -> u16 {
|
|
self.inner.attack.value()
|
|
}
|
|
fn defense(&self) -> u16 {
|
|
self.inner.defense.value()
|
|
}
|
|
fn special_attack(&self) -> u16 {
|
|
self.inner.special_attack.value()
|
|
}
|
|
fn special_defense(&self) -> u16 {
|
|
self.inner.special_defense.value()
|
|
}
|
|
fn speed(&self) -> u16 {
|
|
self.inner.speed.value()
|
|
}
|
|
}
|
|
|
|
impl ExternalReferenceType for ImmutableStatisticSetImpl {
|
|
fn from_extern_value(reference: ExternRef<Self>) -> Self {
|
|
Self::new(reference)
|
|
}
|
|
}
|
|
|
|
crate::handling::cacheable::cacheable!(ImmutableStatisticSetImpl);
|
|
|
|
extern "wasm" {
|
|
fn static_statistics_set_get_hp(r: ExternRef<ImmutableStatisticSetImpl>) -> u16;
|
|
fn static_statistics_set_get_attack(r: ExternRef<ImmutableStatisticSetImpl>) -> u16;
|
|
fn static_statistics_set_get_defense(r: ExternRef<ImmutableStatisticSetImpl>) -> u16;
|
|
fn static_statistics_set_get_special_attack(r: ExternRef<ImmutableStatisticSetImpl>)
|
|
-> u16;
|
|
fn static_statistics_set_get_special_defense(
|
|
r: ExternRef<ImmutableStatisticSetImpl>,
|
|
) -> u16;
|
|
fn static_statistics_set_get_speed(r: ExternRef<ImmutableStatisticSetImpl>) -> u16;
|
|
}
|
|
}
|
|
#[cfg(not(feature = "mock_data"))]
|
|
pub use implementation::*;
|