summaryrefslogtreecommitdiff
path: root/src/components/faction.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/faction.rs')
-rw-r--r--src/components/faction.rs45
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))
+ }
+}