119 lines
3.8 KiB
Rust
Executable File
119 lines
3.8 KiB
Rust
Executable File
use crate::static_data::Statistic;
|
|
use crate::{ValueIdentifiable, ValueIdentifier};
|
|
use std::fmt::Debug;
|
|
use std::sync::Arc;
|
|
|
|
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
|
|
/// can have an increased statistic and a decreased statistic, or be neutral.
|
|
pub trait Nature: ValueIdentifiable + Debug {
|
|
/// The stat that should receive the increased modifier.
|
|
fn increased_stat(&self) -> Statistic;
|
|
|
|
/// The stat that should receive the decreased modifier.
|
|
fn decreased_stat(&self) -> Statistic;
|
|
|
|
/// The amount that the increased stat gets modified by.
|
|
fn increased_modifier(&self) -> f32;
|
|
|
|
/// The amount that the decreased stat gets modified by.
|
|
fn decreased_modifier(&self) -> f32;
|
|
|
|
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
|
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
|
fn get_stat_modifier(&self, stat: Statistic) -> f32;
|
|
}
|
|
|
|
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
|
|
/// can have an increased statistic and a decreased statistic, or be neutral.
|
|
#[derive(Debug)]
|
|
pub struct NatureImpl {
|
|
/// A unique identifier so we know what value this is.
|
|
identifier: ValueIdentifier,
|
|
/// The stat that should receive the increased modifier.
|
|
increase_stat: Statistic,
|
|
/// The stat that should receive the decreased modifier.
|
|
decrease_stat: Statistic,
|
|
/// The amount by which the increased stat is multiplied.
|
|
increase_modifier: f32,
|
|
/// The amount by which the decreased stat is multiplied.
|
|
decrease_modifier: f32,
|
|
}
|
|
|
|
impl NatureImpl {
|
|
/// Instantiates a new statistic.
|
|
pub fn new(
|
|
increase_stat: Statistic,
|
|
decrease_stat: Statistic,
|
|
increase_modifier: f32,
|
|
decrease_modifier: f32,
|
|
) -> Arc<Self> {
|
|
Arc::new(Self {
|
|
identifier: Default::default(),
|
|
increase_stat,
|
|
decrease_stat,
|
|
increase_modifier,
|
|
decrease_modifier,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Nature for NatureImpl {
|
|
/// The stat that should receive the increased modifier.
|
|
fn increased_stat(&self) -> Statistic {
|
|
self.increase_stat
|
|
}
|
|
|
|
/// The stat that should receive the decreased modifier.
|
|
fn decreased_stat(&self) -> Statistic {
|
|
self.decrease_stat
|
|
}
|
|
|
|
fn increased_modifier(&self) -> f32 {
|
|
self.increase_modifier
|
|
}
|
|
|
|
fn decreased_modifier(&self) -> f32 {
|
|
self.decrease_modifier
|
|
}
|
|
|
|
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
|
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
|
fn get_stat_modifier(&self, stat: Statistic) -> f32 {
|
|
if stat == self.increase_stat && stat != self.decrease_stat {
|
|
self.increase_modifier
|
|
} else if stat == self.decrease_stat && stat != self.increase_stat {
|
|
self.decrease_modifier
|
|
} else {
|
|
1.0
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ValueIdentifiable for NatureImpl {
|
|
fn value_identifier(&self) -> ValueIdentifier {
|
|
self.identifier
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) mod tests {
|
|
use super::*;
|
|
|
|
mockall::mock! {
|
|
#[derive(Debug)]
|
|
pub Nature {}
|
|
impl Nature for Nature {
|
|
fn increased_stat(&self) -> Statistic;
|
|
fn decreased_stat(&self) -> Statistic;
|
|
fn increased_modifier(&self) -> f32;
|
|
fn decreased_modifier(&self) -> f32;
|
|
fn get_stat_modifier(&self, stat: Statistic) -> f32;
|
|
}
|
|
impl ValueIdentifiable for Nature {
|
|
fn value_identifier(&self) -> ValueIdentifier {
|
|
ValueIdentifier::new(0)
|
|
}
|
|
}
|
|
}
|
|
}
|