roguelike-game/src/monster_ai_system.rs
2021-10-29 10:30:17 -04:00

43 lines
1.3 KiB
Rust

use super::{Map, Monster, Name, Position, Viewshed};
use rltk::{console, Point};
use specs::prelude::*;
pub struct MonsterAI {}
impl<'a> System<'a> for MonsterAI {
#[allow(clippy::type_complexity)]
type SystemData = (
WriteExpect<'a, Map>,
ReadExpect<'a, Point>,
WriteStorage<'a, Viewshed>,
ReadStorage<'a, Monster>,
ReadStorage<'a, Name>,
WriteStorage<'a, Position>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut map, player_pos, mut viewshed, monster, name, mut position) = data;
for (mut viewshed, _monster, name, mut pos) in
(&mut viewshed, &monster, &name, &mut position).join()
{
if viewshed.visible_tiles.contains(&*player_pos) {
console::log(&format!("{} shouts insults", name.name));
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;
}
}
}
}
}