Skip to content

Commit

Permalink
NPCs do not follow forever. We add a max_turns constant to limit foll…
Browse files Browse the repository at this point in the history
…owing
  • Loading branch information
rpruizc committed Sep 12, 2020
1 parent 2af0af9 commit 5798eb5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -13,11 +13,15 @@ use grid_search_cardinal::{
use line_2d::LineSegment;
use shadowcast::{vision_distance, VisionDistance};

pub struct Agent {}
pub struct Agent {
turns_since_last_saw_player: u32,
}

impl Agent {
pub fn new() -> Self {
Self {}
Self {
turns_since_last_saw_player: u32::MAX,
}
}

// Chooses an action for an NPC to take
@@ -40,6 +44,12 @@ impl Agent {
let npc_coord = world.entity_coord(entity).expect("npc has no coord");
let player_coord = world.entity_coord(player).expect("player has no coord");
if !npc_has_line_of_sight(npc_coord, player_coord, world) {
self.turns_since_last_saw_player = 0;
} else {
self.turns_since_last_saw_player = self.turns_since_last_saw_player.saturating_add(1);
}
const MAX_TURNS_TO_CHASE_PLAYER_AFTER_LOSING_SIGHT: u32 = 3;
if self.turns_since_last_saw_player > MAX_TURNS_TO_CHASE_PLAYER_AFTER_LOSING_SIGHT {
return NpcAction::Wait;
}
const SEARCH_DISTANCE: u32 = 5;

0 comments on commit 5798eb5

Please sign in to comment.