Files
PkmnLib_rs/src/static_data/statistic_set.rs

209 lines
6.5 KiB
Rust

use super::statistics::Statistic;
use num_traits::PrimInt;
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
pub struct StatisticSet<T>
where
T: PrimInt,
{
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
}
impl<T> StatisticSet<T>
where
T: PrimInt,
{
pub fn new(
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
) -> Self {
Self {
hp,
attack,
defense,
special_attack,
special_defense,
speed,
}
}
pub fn hp(&self) -> T {
self.hp
}
pub fn attack(&self) -> T {
self.attack
}
pub fn defense(&self) -> T {
self.defense
}
pub fn special_attack(&self) -> T {
self.special_attack
}
pub fn special_defense(&self) -> T {
self.special_defense
}
pub fn speed(&self) -> T {
self.speed
}
pub const fn get_stat(&self, stat: Statistic) -> T {
match stat {
Statistic::HP => self.hp,
Statistic::Attack => self.attack,
Statistic::Defense => self.defense,
Statistic::SpecialAttack => self.special_attack,
Statistic::SpecialDefense => self.special_defense,
Statistic::Speed => self.speed,
}
}
pub fn set_stat(&mut self, stat: Statistic, value: T) {
match stat {
Statistic::HP => self.hp = value,
Statistic::Attack => self.attack = value,
Statistic::Defense => self.defense = value,
Statistic::SpecialAttack => self.special_attack = value,
Statistic::SpecialDefense => self.special_defense = value,
Statistic::Speed => self.speed = value,
}
}
pub fn increase_stat(&mut self, stat: Statistic, value: T) {
match stat {
Statistic::HP => self.hp = self.hp + value,
Statistic::Attack => self.attack = self.attack + value,
Statistic::Defense => self.defense = self.defense + value,
Statistic::SpecialAttack => self.special_attack = self.special_attack + value,
Statistic::SpecialDefense => self.special_defense = self.special_defense + value,
Statistic::Speed => self.speed = self.speed + value,
}
}
pub fn decrease_stat(&mut self, stat: Statistic, value: T) {
match stat {
Statistic::HP => self.hp = self.hp - value,
Statistic::Attack => self.attack = self.attack - value,
Statistic::Defense => self.defense = self.defense - value,
Statistic::SpecialAttack => self.special_attack = self.special_attack - value,
Statistic::SpecialDefense => self.special_defense = self.special_defense - value,
Statistic::Speed => self.speed = self.speed - value,
}
}
}
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
pub struct ClampedStatisticSet<T, const MIN: i64, const MAX: i64>
where
T: PrimInt,
{
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
}
impl<T, const MIN: i64, const MAX: i64> ClampedStatisticSet<T, MIN, MAX>
where
T: PrimInt,
{
pub fn hp(&self) -> T {
self.hp
}
pub fn attack(&self) -> T {
self.attack
}
pub fn defense(&self) -> T {
self.defense
}
pub fn special_attack(&self) -> T {
self.special_attack
}
pub fn special_defense(&self) -> T {
self.special_defense
}
pub fn speed(&self) -> T {
self.speed
}
pub const fn get_stat(&self, stat: Statistic) -> T {
match stat {
Statistic::HP => self.hp,
Statistic::Attack => self.attack,
Statistic::Defense => self.defense,
Statistic::SpecialAttack => self.special_attack,
Statistic::SpecialDefense => self.special_defense,
Statistic::Speed => self.speed,
}
}
pub fn set_stat(&mut self, stat: Statistic, mut value: T) {
if value < T::from(MIN).unwrap() {
value = T::from(MIN).unwrap();
} else if value > T::from(MAX).unwrap() {
value = T::from(MAX).unwrap();
}
match stat {
Statistic::HP => self.hp = value,
Statistic::Attack => self.attack = value,
Statistic::Defense => self.defense = value,
Statistic::SpecialAttack => self.special_attack = value,
Statistic::SpecialDefense => self.special_defense = value,
Statistic::Speed => self.speed = value,
}
}
fn change_stat(mut new_value: T, original_value: &mut T) -> bool {
if new_value < T::from(MIN).unwrap() {
new_value = T::from(MIN).unwrap();
} else if new_value > T::from(MAX).unwrap() {
new_value = T::from(MAX).unwrap();
}
if *original_value == new_value {
return false;
}
*original_value = new_value;
true
}
pub fn increase_stat(&mut self, stat: Statistic, value: T) -> bool {
match stat {
Statistic::HP => Self::change_stat(self.hp + value, &mut self.hp),
Statistic::Attack => Self::change_stat(self.attack + value, &mut self.attack),
Statistic::Defense => Self::change_stat(self.defense + value, &mut self.defense),
Statistic::SpecialAttack => {
Self::change_stat(self.special_attack + value, &mut self.special_attack)
}
Statistic::SpecialDefense => {
Self::change_stat(self.special_defense + value, &mut self.special_defense)
}
Statistic::Speed => Self::change_stat(self.speed + value, &mut self.speed),
}
}
pub fn decrease_stat(&mut self, stat: Statistic, value: T) -> bool {
match stat {
Statistic::HP => Self::change_stat(self.hp - value, &mut self.hp),
Statistic::Attack => Self::change_stat(self.attack - value, &mut self.attack),
Statistic::Defense => Self::change_stat(self.defense - value, &mut self.defense),
Statistic::SpecialAttack => {
Self::change_stat(self.special_attack - value, &mut self.special_attack)
}
Statistic::SpecialDefense => {
Self::change_stat(self.special_defense - value, &mut self.special_defense)
}
Statistic::Speed => Self::change_stat(self.speed - value, &mut self.speed),
}
}
}