PkmnLib_rs/src/static_data/moves/secondary_effect.rs

85 lines
2.6 KiB
Rust
Executable File

use crate::static_data::Parameter;
use crate::StringKey;
use std::fmt::Debug;
use std::sync::Arc;
/// A secondary effect is an effect on a move that happens after it hits.
pub trait SecondaryEffect: Debug {
/// The chance in percentages that the effect triggers. -1 to make it always trigger.
fn chance(&self) -> f32;
/// The name of the effect.
fn effect_name(&self) -> &StringKey;
/// A list of parameters for the effect.
fn parameters(&self) -> &Vec<Arc<Parameter>>;
}
/// A secondary effect is an effect on a move that happens after it hits.
#[derive(Debug)]
pub struct SecondaryEffectImpl {
/// The chance in percentages that the effect triggers. -1 to make it always trigger.
chance: f32,
/// The name of the effect.
effect_name: StringKey,
/// A list of parameters for the effect.
parameters: Vec<Arc<Parameter>>,
}
impl SecondaryEffectImpl {
/// Instantiates a new Secondary Effect.
pub fn new(chance: f32, effect_name: StringKey, parameters: Vec<Arc<Parameter>>) -> Self {
Self {
chance,
effect_name,
parameters,
}
}
}
impl SecondaryEffect for SecondaryEffectImpl {
/// The chance in percentages that the effect triggers. -1 to make it always trigger.
fn chance(&self) -> f32 {
self.chance
}
/// The name of the effect.
fn effect_name(&self) -> &StringKey {
&self.effect_name
}
/// A list of parameters for the effect.
fn parameters(&self) -> &Vec<Arc<Parameter>> {
&self.parameters
}
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
#[allow(clippy::unwrap_used)]
pub(crate) mod tests {
use super::*;
use assert_approx_eq::assert_approx_eq;
use crate::static_data::moves::secondary_effect::SecondaryEffect;
use crate::static_data::SecondaryEffectImpl;
mockall::mock! {
#[derive(Debug)]
pub SecondaryEffect{}
impl SecondaryEffect for SecondaryEffect {
fn chance(&self) -> f32;
fn effect_name(&self) -> &StringKey;
fn parameters(&self) -> &Vec<Arc<Parameter >>;
}
}
#[test]
fn create_secondary_effect() {
let empty = SecondaryEffectImpl::new(0.0, "".into(), vec![]);
assert_approx_eq!(empty.chance(), 0.0);
assert_eq!(empty.effect_name(), &"".into());
assert_eq!(empty.parameters().len(), 0);
let set = SecondaryEffectImpl::new(50.0, "foo".into(), Vec::new());
assert_approx_eq!(set.chance(), 50.0);
assert_eq!(set.effect_name(), &"foo".into());
assert_eq!(set.parameters().len(), 0);
}
}