diff options
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/faction.rs | 45 | ||||
| -rw-r--r-- | src/components/mod.rs | 2 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/components/faction.rs b/src/components/faction.rs new file mode 100644 index 0000000..5fc02a2 --- /dev/null +++ b/src/components/faction.rs @@ -0,0 +1,45 @@ + +use specs::{ + Component, + HashMapStorage, + ReadStorage, + Entity, +}; + +#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[storage(HashMapStorage)] +pub enum Faction { + Neutral, + Good, + Evil, + None +} + +use Faction::{Neutral, Good, Evil, None}; + +impl Faction { + + pub fn from_str(name: &str) -> Option<Faction> { + match name.to_lowercase().as_str() { + "neutral" => Some(Neutral), + "good" => Some(Good), + "evil" => Some(Evil), + "none" => Some(None), + "" => Some(None), + _ => Option::None + } + } + + pub fn is_enemy(&self, other: Faction) -> bool { + match self { + Neutral => false, + Good => other == Evil || other == None, + Evil => other == Good || other == None, + None => other != Neutral + } + } + + pub fn is_enemy_entity(factions: &ReadStorage<Self>, a: Entity, b: Entity) -> bool{ + factions.get(a).unwrap_or(&None).is_enemy(*factions.get(b).unwrap_or(&None)) + } +} diff --git a/src/components/mod.rs b/src/components/mod.rs index 202caf2..d2087b0 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,6 +1,7 @@ pub mod item; pub mod messages; +pub mod faction; pub use item::Item; pub use messages::{ @@ -8,6 +9,7 @@ pub use messages::{ AttackInbox, AttackType }; +pub use faction::Faction; use specs::{ DenseVecStorage, |
