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

This commit is contained in:
2022-11-27 17:47:51 +01:00
parent 996a35ffa4
commit e04f61d9e6
9 changed files with 50 additions and 31 deletions

View File

@@ -12,7 +12,7 @@ pub struct AbilityLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying map for the library.
map: IndexMap<StringKey, Arc<Ability>>,
map: IndexMap<StringKey, Arc<dyn Ability>>,
}
impl AbilityLibrary {
@@ -25,11 +25,11 @@ impl AbilityLibrary {
}
}
impl DataLibrary<Ability> for AbilityLibrary {
fn map(&self) -> &IndexMap<StringKey, Arc<Ability>> {
impl DataLibrary<dyn Ability> for AbilityLibrary {
fn map(&self) -> &IndexMap<StringKey, Arc<dyn Ability>> {
&self.map
}
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Ability>> {
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Ability>> {
&mut self.map
}
}
@@ -42,7 +42,7 @@ impl ValueIdentifiable for AbilityLibrary {
#[cfg(test)]
pub mod tests {
use crate::static_data::Ability;
use crate::static_data::AbilityImpl;
use crate::static_data::AbilityLibrary;
use crate::static_data::DataLibrary;
use crate::StringKey;
@@ -52,7 +52,11 @@ pub mod tests {
let mut lib = AbilityLibrary::new(1);
lib.add(
&StringKey::new("test_ability"),
Arc::new(Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new())),
Arc::new(AbilityImpl::new(
&"test_ability".into(),
&"test_ability".into(),
Vec::new(),
)),
);
lib
}

View File

@@ -1,9 +1,20 @@
use crate::static_data::EffectParameter;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use std::fmt::Debug;
/// An ability is a passive effect in battle that is attached to a Pokemon.
pub trait Ability: Debug + ValueIdentifiable {
/// The name of the ability.
fn name(&self) -> &StringKey;
/// The name of the script effect of the ability.
fn effect(&self) -> &StringKey;
/// The parameters for the script effect of the ability.
fn parameters(&self) -> &Vec<EffectParameter>;
}
/// An ability is a passive effect in battle that is attached to a Pokemon.
#[derive(Debug)]
pub struct Ability {
pub struct AbilityImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The name of the ability.
@@ -14,7 +25,7 @@ pub struct Ability {
parameters: Vec<EffectParameter>,
}
impl Ability {
impl AbilityImpl {
/// Instantiates a new ability.
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<EffectParameter>) -> Self {
Self {
@@ -24,22 +35,24 @@ impl Ability {
parameters,
}
}
}
impl Ability for AbilityImpl {
/// The name of the ability.
pub fn name(&self) -> &StringKey {
fn name(&self) -> &StringKey {
&self.name
}
/// The name of the script effect of the ability.
pub fn effect(&self) -> &StringKey {
fn effect(&self) -> &StringKey {
&self.effect
}
/// The parameters for the script effect of the ability.
pub fn parameters(&self) -> &Vec<EffectParameter> {
fn parameters(&self) -> &Vec<EffectParameter> {
&self.parameters
}
}
impl ValueIdentifiable for Ability {
impl ValueIdentifiable for AbilityImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}

View File

@@ -116,7 +116,7 @@ impl Form {
}
/// Find the index of an ability that can be on this form.
pub fn find_ability_index(&self, ability: &Ability) -> Option<AbilityIndex> {
pub fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex> {
for (index, a) in self.abilities.iter().enumerate() {
if a == ability.name() {
return Some(AbilityIndex {