Make npcs attack in a smarter way

This commit is contained in:
timokoesters 2019-09-07 11:46:30 +02:00
parent 9f83130466
commit f3593371ea
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
2 changed files with 19 additions and 10 deletions

View File

@ -65,6 +65,7 @@ impl<'a> System<'a> for Sys {
}
Agent::Enemy { bearing, target } => {
const SIGHT_DIST: f32 = 30.0;
const MIN_ATTACK_DIST: f32 = 3.5;
let mut choose_new = false;
if let Some((Some(target_pos), Some(target_stats), Some(target_character))) =
@ -81,16 +82,11 @@ impl<'a> System<'a> for Sys {
let dist = Vec2::<f32>::from(target_pos.0 - pos.0).magnitude();
if target_stats.is_dead {
choose_new = true;
} else if dist < 6.0 {
// Fight and slowly move closer
} else if dist < MIN_ATTACK_DIST {
// Fight (and slowly move closer)
controller.move_dir =
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.5;
if rand::random::<f32>() < 0.05 {
controller.primary = true;
} else {
controller.primary = false;
}
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.01;
controller.primary = true;
} else if dist < SIGHT_DIST {
controller.move_dir =
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.96;

View File

@ -114,7 +114,6 @@ impl<'a> System<'a> for Sys {
|| character.movement == Run
|| character.movement == Jump)
{
// TODO: Check if wield ability exists
if let Wield { time_left } = character.action {
if time_left == Duration::default() {
character.action = Attack {
@ -152,6 +151,20 @@ impl<'a> System<'a> for Sys {
});
}
}
None => {
// Attack
if controller.primary
&& (character.movement == Stand
|| character.movement == Run
|| character.movement == Jump)
&& !character.action.is_attack()
{
character.action = Attack {
time_left: ATTACK_DURATION,
applied: false,
};
}
}
_ => {}
}