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

This commit is contained in:
2022-11-27 22:29:11 +01:00
parent 27164616e9
commit ad9f17ccf1
12 changed files with 118 additions and 81 deletions

View File

@@ -13,7 +13,7 @@ pub struct MoveLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying map.
map: IndexMap<StringKey, Arc<MoveData>>,
map: IndexMap<StringKey, Arc<dyn MoveData>>,
}
impl MoveLibrary {
@@ -26,11 +26,11 @@ impl MoveLibrary {
}
}
impl DataLibrary<MoveData> for MoveLibrary {
fn map(&self) -> &IndexMap<StringKey, Arc<MoveData>> {
impl DataLibrary<dyn MoveData> for MoveLibrary {
fn map(&self) -> &IndexMap<StringKey, Arc<dyn MoveData>> {
&self.map
}
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<MoveData>> {
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn MoveData>> {
&mut self.map
}
}
@@ -48,11 +48,11 @@ pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::move_library::MoveLibrary;
use crate::static_data::{MoveCategory, MoveData, MoveTarget};
use crate::static_data::{MoveCategory, MoveDataImpl, MoveTarget};
use crate::StringKey;
fn build_move() -> MoveData {
MoveData::new(
fn build_move() -> MoveDataImpl {
MoveDataImpl::new(
&"foo".into(),
0.into(),
MoveCategory::Physical,

View File

@@ -1,6 +1,7 @@
use hashbrown::HashSet;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use crate::static_data::{SecondaryEffect, TypeIdentifier};
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
@@ -59,9 +60,40 @@ pub enum MoveTarget {
SelfUse,
}
/// A move is the skill Pokémon primarily use in battle. This is the data related to that.
pub trait MoveData: Debug + ValueIdentifiable {
/// The name of the move.
fn name(&self) -> &StringKey;
/// The attacking type of the move.
fn move_type(&self) -> TypeIdentifier;
/// The category of the move.
fn category(&self) -> MoveCategory;
/// The base power, not considering any modifiers, the move has.
fn base_power(&self) -> u8;
/// The accuracy of the move in percentage. Should be 255 for moves that always hit.
fn accuracy(&self) -> u8;
/// The number of times the move can be used. This can be modified on actually learned moves using
/// PP-Ups
fn base_usages(&self) -> u8;
/// How the move handles targets.
fn target(&self) -> MoveTarget;
/// The priority of the move. A higher priority means the move should go before other moves.
fn priority(&self) -> i8;
/// The optional secondary effect the move has.
fn secondary_effect(&self) -> &Option<Box<dyn SecondaryEffect>>;
/// Arbitrary flags that can be applied to the move.
fn has_flag(&self, key: &StringKey) -> bool;
/// Arbitrary flags that can be applied to the move.
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
}
/// A move is the skill Pokémon primarily use in battle. This is the data related to that.
#[derive(Debug)]
pub struct MoveData {
pub struct MoveDataImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The name of the move.
@@ -87,7 +119,7 @@ pub struct MoveData {
flags: HashSet<StringKey>,
}
impl MoveData {
impl MoveDataImpl {
/// Instantiates a new move.
pub fn new(
name: &StringKey,
@@ -100,8 +132,8 @@ impl MoveData {
priority: i8,
secondary_effect: Option<Box<dyn SecondaryEffect>>,
flags: HashSet<StringKey>,
) -> MoveData {
MoveData {
) -> Self {
Self {
identifier: Default::default(),
name: name.clone(),
move_type,
@@ -115,58 +147,61 @@ impl MoveData {
flags,
}
}
}
impl MoveData for MoveDataImpl {
/// The name of the move.
pub fn name(&self) -> &StringKey {
fn name(&self) -> &StringKey {
&self.name
}
/// The attacking type of the move.
pub fn move_type(&self) -> TypeIdentifier {
fn move_type(&self) -> TypeIdentifier {
self.move_type
}
/// The category of the move.
pub fn category(&self) -> MoveCategory {
fn category(&self) -> MoveCategory {
self.category
}
/// The base power, not considering any modifiers, the move has.
pub fn base_power(&self) -> u8 {
fn base_power(&self) -> u8 {
self.base_power
}
/// The accuracy of the move in percentage. Should be 255 for moves that always hit.
pub fn accuracy(&self) -> u8 {
fn accuracy(&self) -> u8 {
self.accuracy
}
/// The number of times the move can be used. This can be modified on actually learned moves using
/// PP-Ups
pub fn base_usages(&self) -> u8 {
fn base_usages(&self) -> u8 {
self.base_usages
}
/// How the move handles targets.
pub fn target(&self) -> MoveTarget {
fn target(&self) -> MoveTarget {
self.target
}
/// The priority of the move. A higher priority means the move should go before other moves.
pub fn priority(&self) -> i8 {
fn priority(&self) -> i8 {
self.priority
}
/// The optional secondary effect the move has.
pub fn secondary_effect(&self) -> &Option<Box<dyn SecondaryEffect>> {
fn secondary_effect(&self) -> &Option<Box<dyn SecondaryEffect>> {
&self.secondary_effect
}
/// Arbitrary flags that can be applied to the move.
pub fn has_flag(&self, key: &StringKey) -> bool {
fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains::<StringKey>(key)
}
/// Arbitrary flags that can be applied to the move.
pub fn has_flag_by_hash(&self, key_hash: u32) -> bool {
fn has_flag_by_hash(&self, key_hash: u32) -> bool {
self.flags.contains::<u32>(&key_hash)
}
}
impl ValueIdentifiable for MoveData {
impl ValueIdentifiable for MoveDataImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}