Don't hunt friendly animals

This commit is contained in:
Joshua Barretto 2023-04-03 20:33:06 +01:00
parent c8d0443111
commit 082bcdb755
2 changed files with 26 additions and 1 deletions

View File

@ -83,7 +83,7 @@ impl Alignment {
} }
} }
// Never attacks // Usually never attacks
pub fn passive_towards(self, other: Alignment) -> bool { pub fn passive_towards(self, other: Alignment) -> bool {
match (self, other) { match (self, other) {
(Alignment::Enemy, Alignment::Enemy) => true, (Alignment::Enemy, Alignment::Enemy) => true,
@ -98,6 +98,20 @@ impl Alignment {
_ => false, _ => false,
} }
} }
// Never attacks
pub fn friendly_towards(self, other: Alignment) -> bool {
match (self, other) {
(Alignment::Enemy, Alignment::Enemy) => true,
(Alignment::Owned(a), Alignment::Owned(b)) if a == b => true,
(Alignment::Npc, Alignment::Npc) => true,
(Alignment::Npc, Alignment::Tame) => true,
(Alignment::Tame, Alignment::Npc) => true,
(Alignment::Tame, Alignment::Tame) => true,
(_, Alignment::Passive) => true,
_ => false,
}
}
} }
impl Component for Alignment { impl Component for Alignment {

View File

@ -1632,6 +1632,7 @@ impl<'a> AgentData<'a> {
pub fn is_hunting_animal(&self, entity: EcsEntity, read_data: &ReadData) -> bool { pub fn is_hunting_animal(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
(entity != *self.entity) (entity != *self.entity)
&& !self.friendly_towards(entity, read_data)
&& matches!(read_data.bodies.get(entity), Some(Body::QuadrupedSmall(_))) && matches!(read_data.bodies.get(entity), Some(Body::QuadrupedSmall(_)))
} }
@ -1667,6 +1668,16 @@ impl<'a> AgentData<'a> {
} }
} }
fn friendly_towards(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
if let (Some(self_alignment), Some(other_alignment)) =
(self.alignment, read_data.alignments.get(entity))
{
self_alignment.friendly_towards(*other_alignment)
} else {
false
}
}
pub fn can_see_entity( pub fn can_see_entity(
&self, &self,
agent: &Agent, agent: &Agent,