PkmnLib_rs/src/static_data/moves/secondary_effect.rs

98 lines
3.1 KiB
Rust
Executable File

use crate::static_data::EffectParameter;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use std::fmt::Debug;
/// A secondary effect is an effect on a move that happens after it hits.
pub trait SecondaryEffect: Debug + ValueIdentifiable {
/// 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<EffectParameter>;
}
/// A secondary effect is an effect on a move that happens after it hits.
#[derive(Debug)]
pub struct SecondaryEffectImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// 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<EffectParameter>,
}
impl SecondaryEffectImpl {
/// Instantiates a new Secondary Effect.
pub fn new(chance: f32, effect_name: StringKey, parameters: Vec<EffectParameter>) -> Self {
Self {
identifier: Default::default(),
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<EffectParameter> {
&self.parameters
}
}
impl ValueIdentifiable for SecondaryEffectImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[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<EffectParameter>;
}
impl ValueIdentifiable for SecondaryEffect{
fn value_identifier(&self) -> ValueIdentifier{
ValueIdentifier::new(0)
}
}
}
#[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);
}
}