Update target's last known position.

Before, it was set only upon targeting.
This commit is contained in:
holychowders 2022-11-16 20:35:07 -06:00
parent bae5c5b675
commit 2c745bed30
2 changed files with 53 additions and 4 deletions

View File

@ -701,9 +701,8 @@ impl<'a> AgentData<'a> {
},
};
let is_detected = |entity: EcsEntity, e_pos: &Pos| {
self.can_sense_directly_near(e_pos)
|| self.can_see_entity(agent, controller, entity, e_pos, read_data)
let is_detected = |entity: &EcsEntity, e_pos: &Pos| {
self.detects_other(agent, controller, entity, e_pos, read_data)
};
let target = entities_nearby
@ -713,7 +712,7 @@ impl<'a> AgentData<'a> {
.filter_map(|(entity, attack_target)| {
get_pos(entity).map(|pos| (entity, pos, attack_target))
})
.filter(|(entity, e_pos, _)| is_detected(*entity, e_pos))
.filter(|(entity, e_pos, _)| is_detected(entity, e_pos))
.min_by_key(|(_, e_pos, attack_target)| {
(
*attack_target,
@ -1562,6 +1561,18 @@ impl<'a> AgentData<'a> {
&& entities_have_line_of_sight(self.pos, self.body, other_pos, other_body, read_data)
}
pub fn detects_other(
&self,
agent: &Agent,
controller: &Controller,
other: &EcsEntity,
other_pos: &Pos,
read_data: &ReadData,
) -> bool {
self.can_sense_directly_near(other_pos)
|| self.can_see_entity(agent, controller, *other, other_pos, read_data)
}
pub fn can_sense_directly_near(&self, e_pos: &Pos) -> bool {
let chance = thread_rng().gen_bool(0.3);
e_pos.0.distance_squared(self.pos.0) < 5_f32.powi(2) && chance

View File

@ -100,6 +100,7 @@ impl BehaviorTree {
pub fn target() -> Self {
Self {
tree: vec![
update_last_known_pos,
untarget_if_dead,
update_target_awareness,
do_hostile_tree_if_hostile_and_aware,
@ -509,6 +510,43 @@ fn handle_timed_events(bdata: &mut BehaviorData) -> bool {
false
}
fn update_last_known_pos(bdata: &mut BehaviorData) -> bool {
let BehaviorData {
agent,
agent_data,
read_data,
controller,
..
} = bdata;
if let Some(target_info) = agent.target {
let target = target_info.target;
if let Some(target_pos) = read_data.positions.get(target) {
if agent_data.detects_other(agent, controller, &target, target_pos, read_data) {
let new_last_known_pos = Some(target_pos.0);
let Target {
hostile,
selected_at,
aggro_on,
..
} = target_info;
agent.target = Some(Target::new(
target,
hostile,
selected_at,
aggro_on,
new_last_known_pos,
));
}
}
}
false
}
/// Try to heal self if our damage went below a certain threshold
fn heal_self_if_hurt(bdata: &mut BehaviorData) -> bool {
if bdata.agent_data.damage < HEALING_ITEM_THRESHOLD