Make SecondaryEffect a trait
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-11-27 18:22:57 +01:00
parent e04f61d9e6
commit 1c0b953d9a
6 changed files with 95 additions and 40 deletions

View File

@@ -60,7 +60,7 @@ pub enum MoveTarget {
}
/// A move is the skill Pokémon primarily use in battle. This is the data related to that.
#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub struct MoveData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
@@ -82,7 +82,7 @@ pub struct MoveData {
/// The priority of the move. A higher priority means the move should go before other moves.
priority: i8,
/// The optional secondary effect the move has.
secondary_effect: Option<SecondaryEffect>,
secondary_effect: Option<Box<dyn SecondaryEffect>>,
/// Arbitrary flags that can be applied to the move.
flags: HashSet<StringKey>,
}
@@ -98,7 +98,7 @@ impl MoveData {
base_usages: u8,
target: MoveTarget,
priority: i8,
secondary_effect: Option<SecondaryEffect>,
secondary_effect: Option<Box<dyn SecondaryEffect>>,
flags: HashSet<StringKey>,
) -> MoveData {
MoveData {
@@ -151,7 +151,7 @@ impl MoveData {
}
/// The optional secondary effect the move has.
pub fn secondary_effect(&self) -> &Option<SecondaryEffect> {
pub fn secondary_effect(&self) -> &Option<Box<dyn SecondaryEffect>> {
&self.secondary_effect
}

View File

@@ -1,9 +1,20 @@
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.
#[derive(PartialEq, Debug)]
pub struct SecondaryEffect {
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.
@@ -14,32 +25,34 @@ pub struct SecondaryEffect {
parameters: Vec<EffectParameter>,
}
impl SecondaryEffect {
impl SecondaryEffectImpl {
/// Instantiates a new Secondary Effect.
pub fn new(chance: f32, effect_name: StringKey, parameters: Vec<EffectParameter>) -> SecondaryEffect {
SecondaryEffect {
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.
pub fn chance(&self) -> f32 {
fn chance(&self) -> f32 {
self.chance
}
/// The name of the effect.
pub fn effect_name(&self) -> &StringKey {
fn effect_name(&self) -> &StringKey {
&self.effect_name
}
/// A list of parameters for the effect.
pub fn parameters(&self) -> &Vec<EffectParameter> {
fn parameters(&self) -> &Vec<EffectParameter> {
&self.parameters
}
}
impl ValueIdentifiable for SecondaryEffect {
impl ValueIdentifiable for SecondaryEffectImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
@@ -50,14 +63,15 @@ mod tests {
use assert_approx_eq::assert_approx_eq;
use crate::static_data::moves::secondary_effect::SecondaryEffect;
use crate::static_data::SecondaryEffectImpl;
#[test]
fn create_secondary_effect() {
let empty = SecondaryEffect::new(0.0, "".into(), vec![]);
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 = SecondaryEffect::new(50.0, "foo".into(), Vec::new());
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);