1
0
Fork 0
roguelike-game/src/systems/ai/approach_ai_system.rs

55 lines
1.6 KiB
Rust

use ::bracket_lib::prelude::*;
use ::specs::prelude::*;
use crate::components::{ApplyMove, MyTurn, Position, WantsToApproach};
use crate::Map;
pub struct ApproachAI {}
impl<'a> System<'a> for ApproachAI {
#[allow(clippy::type_complexity)]
type SystemData = (
WriteStorage<'a, MyTurn>,
WriteStorage<'a, WantsToApproach>,
ReadStorage<'a, Position>,
ReadExpect<'a, Map>,
Entities<'a>,
WriteStorage<'a, ApplyMove>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut turns, mut want_approach, positions, map, entities, mut apply_move) = data;
let mut turn_done: Vec<Entity> = Vec::new();
for (entity, pos, approach, _myturn) in
(&entities, &positions, &want_approach, &turns).join()
{
turn_done.push(entity);
let path = a_star_search(
map.xy_idx(pos.x, pos.y) as i32,
map.xy_idx(approach.idx % map.width, approach.idx / map.width) as i32,
&*map,
);
if path.success && path.steps.len() > 1 {
apply_move
.insert(
entity,
ApplyMove {
dest_idx: path.steps[1],
},
)
.expect("Unable to insert intent to move.");
}
}
want_approach.clear();
// Remove turn marker for those that are done
for done in turn_done.iter() {
turns.remove(*done);
}
}
}