2021-10-29 10:30:17 -04:00
|
|
|
use super::{Map, Monster, Name, Position, Viewshed};
|
2021-10-26 15:43:59 -04:00
|
|
|
use rltk::{console, Point};
|
|
|
|
use specs::prelude::*;
|
|
|
|
|
|
|
|
pub struct MonsterAI {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for MonsterAI {
|
2021-10-29 10:30:17 -04:00
|
|
|
#[allow(clippy::type_complexity)]
|
2021-10-26 15:43:59 -04:00
|
|
|
type SystemData = (
|
2021-10-29 10:30:17 -04:00
|
|
|
WriteExpect<'a, Map>,
|
2021-10-26 15:43:59 -04:00
|
|
|
ReadExpect<'a, Point>,
|
2021-10-29 10:30:17 -04:00
|
|
|
WriteStorage<'a, Viewshed>,
|
2021-10-26 15:43:59 -04:00
|
|
|
ReadStorage<'a, Monster>,
|
|
|
|
ReadStorage<'a, Name>,
|
2021-10-29 10:30:17 -04:00
|
|
|
WriteStorage<'a, Position>,
|
2021-10-26 15:43:59 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2021-10-29 10:30:17 -04:00
|
|
|
let (mut map, player_pos, mut viewshed, monster, name, mut position) = data;
|
2021-10-26 15:43:59 -04:00
|
|
|
|
2021-10-29 10:30:17 -04:00
|
|
|
for (mut viewshed, _monster, name, mut pos) in
|
|
|
|
(&mut viewshed, &monster, &name, &mut position).join()
|
|
|
|
{
|
2021-10-29 11:11:17 -04:00
|
|
|
let distance =
|
|
|
|
rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos);
|
|
|
|
if distance < 1.5 {
|
|
|
|
// Attack goes here
|
2021-10-29 10:30:17 -04:00
|
|
|
console::log(&format!("{} shouts insults", name.name));
|
2021-10-29 11:11:17 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-10-29 10:30:17 -04:00
|
|
|
|
2021-10-29 11:11:17 -04:00
|
|
|
if viewshed.visible_tiles.contains(&*player_pos) {
|
2021-10-29 10:30:17 -04:00
|
|
|
let path = rltk::a_star_search(
|
|
|
|
map.xy_idx(pos.x, pos.y) as i32,
|
|
|
|
map.xy_idx(player_pos.x, player_pos.y) as i32,
|
|
|
|
&mut *map,
|
|
|
|
);
|
|
|
|
|
|
|
|
if path.success && path.steps.len() > 1 {
|
|
|
|
pos.x = path.steps[1] as i32 % map.width;
|
|
|
|
pos.y = path.steps[1] as i32 / map.width;
|
|
|
|
|
|
|
|
viewshed.dirty = true;
|
|
|
|
}
|
2021-10-26 15:43:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|