Skip to content

Commit

Permalink
Fixed the processing of traits to have an order.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 9, 2023
1 parent b101ba9 commit e20a020
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
24 changes: 24 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ pub struct Brain {
pub task: Option<Task>,
pub order: Option<Order>,
pub personality: Vec<PersonalityTrait>,
pub last_considered_personality_trait: Option<PersonalityTrait>,
}
impl Brain {
pub fn remotivate(&mut self) {
Expand All @@ -523,6 +524,29 @@ impl Brain {
pub fn add_personality_trait(&mut self, trait_: PersonalityTrait) {
self.personality.push(trait_);
}
pub fn get_next_personality_trait(&mut self) -> Option<PersonalityTrait> {
if self.last_considered_personality_trait.is_none() {
// return the first personality
if self.personality.len() > 0 {
self.last_considered_personality_trait = Some(self.personality[0]);
return Some(self.personality[0]);
} else {
return None;
}
}
for (i, personality_trait) in self.personality.iter().enumerate() {
if self.last_considered_personality_trait.unwrap() == *personality_trait {
if i == self.personality.len() - 1 {
self.last_considered_personality_trait = None;
return None;
} else {
self.last_considered_personality_trait = Some(self.personality[i+1]);
return Some(self.personality[i+1]);
}
}
}
return None;
}
}
impl InfoPanel for Brain {
fn info_panel(&self) -> Vec<String> {
Expand Down
22 changes: 21 additions & 1 deletion src/task_system/personality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,29 @@ impl Plugin for PersonalityPlugin {
app
.add_systems(
Update,
(territorial::personality_territorial, nopersonality::personality_nopersonality)
personalities
.run_if(bevy::time::common_conditions::on_timer(bevy::utils::Duration::from_secs_f32(0.5)))
.run_if(in_state(GameState::InGame))
);
}
}

pub fn personalities(
mut entities: Query<(Entity, &mut Brain, &mut PhysicalBody, &Position, Option<&Nest>)>
) {
let potential_targets = entities.iter()
.map(|(entity, _, _, position, _)| (entity, *position)) // Clone the Position data
.collect::<Vec<(Entity, Position)>>();
for (entity, mut brain, mut physical_body, position, nest) in entities.iter_mut() {
if brain.task != Some(Task::Personality) { continue; }
let next_trait = brain.get_next_personality_trait();
match next_trait {
Some(PersonalityTrait::Territorial) => {
territorial::territorial(entity, brain, physical_body, position, nest, &potential_targets);
},
_ => {
nopersonality::nopersonality(entity, brain, physical_body, position, nest);
},
}
}
}
20 changes: 11 additions & 9 deletions src/task_system/personality/nopersonality.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::prelude::*;

pub fn personality_nopersonality(
mut entities: Query<(Entity, &mut Brain, &mut PhysicalBody, &Position)>
pub fn nopersonality(
entity: Entity,
mut brain: Mut<Brain>,
mut physical_body: Mut<PhysicalBody>,
position: &Position,
_: Option<&Nest>
) {
for (entity, mut brain, mut physical_body, position) in entities.iter_mut() {
if brain.task != Some(Task::Personality) { continue; }
if !brain.personality.is_empty() { continue; }

brain.motivation = Some(Motivation::Meander);
brain.task = None;
}
if brain.task != Some(Task::Personality) { return; }
if !brain.personality.is_empty() { return; }

brain.motivation = Some(Motivation::Meander);
brain.task = None;
}
36 changes: 18 additions & 18 deletions src/task_system/personality/territorial.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::prelude::*;

pub fn personality_territorial(
mut entities: Query<(Entity, &mut Brain, &mut PhysicalBody, &Position, Option<&Nest>)>
pub fn territorial(
entity: Entity,
mut brain: Mut<Brain>,
mut physical_body: Mut<PhysicalBody>,
position: &Position,
nest: Option<&Nest>,
potential_targets: &Vec<(Entity, Position)>
) {
let potential_targets = entities.iter()
.map(|(entity, _, _, position, _)| (entity, *position)) // Clone the Position data
.collect::<Vec<(Entity, Position)>>();
for (entity, mut brain, mut physical_body, position, nest) in entities.iter_mut() {
if brain.task != Some(Task::Personality) { continue; }
if !brain.personality.contains(&PersonalityTrait::Territorial) { continue; }
// Anything to defend against?
for (target_entity, target_position) in potential_targets.iter() {
if entity == *target_entity { continue; }
if target_position.distance(position) < 5 {
physical_body.danger = Some(Danger { danger_type: DangerType::Attacked, danger_source: Some(*target_entity) });
brain.remotivate();
break;
}
if brain.task != Some(Task::Personality) { return; }
if !brain.personality.contains(&PersonalityTrait::Territorial) { return; }
// Anything to defend against?
for (target_entity, target_position) in potential_targets.iter() {
if entity == *target_entity { continue; }
if target_position.distance(position) < 5 {
physical_body.danger = Some(Danger { danger_type: DangerType::Attacked, danger_source: Some(*target_entity) });
brain.remotivate();
break;
}
brain.motivation = Some(Motivation::Meander);
brain.task = None;
}
brain.motivation = Some(Motivation::Meander);
brain.task = None;
}

0 comments on commit e20a020

Please sign in to comment.