diff options
| author | troido <troido@protonmail.com> | 2020-03-03 19:11:32 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-03 19:11:32 +0100 |
| commit | f844ed18e29465177a339f244800fb7a2e77daa9 (patch) | |
| tree | c3445f869eb072a35162e008609cc1dc75c427fb /src/components/faction.rs | |
| parent | 986c82723cf9a4adada02287309999f4ebbf94e3 (diff) | |
factions now work
Diffstat (limited to 'src/components/faction.rs')
| -rw-r--r-- | src/components/faction.rs | 45 |
1 files changed, 45 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)) + } +} |
