summaryrefslogtreecommitdiff
path: root/src/components/faction.rs
blob: 5fc02a2510aa815c193cc4ed0a4a2fe567a6e404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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))
	}
}