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

96 lines
3.7 KiB
Rust

use ::rltk::RandomNumberGenerator;
use ::specs::prelude::*;
use crate::components::{ApplyMove, MoveMode, Movement, MyTurn, Position};
use crate::{spatial, tile_walkable, Map};
pub struct DefaultMoveAI {}
impl<'a> System<'a> for DefaultMoveAI {
#[allow(clippy::type_complexity)]
type SystemData = (
WriteStorage<'a, MyTurn>,
WriteStorage<'a, MoveMode>,
ReadStorage<'a, Position>,
ReadExpect<'a, Map>,
WriteExpect<'a, RandomNumberGenerator>,
WriteStorage<'a, ApplyMove>,
Entities<'a>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut turns, mut move_mode, positions, map, mut rng, mut apply_move, entities) = data;
let mut turn_done: Vec<Entity> = Vec::new();
for (entity, pos, mut mode, _myturn) in
(&entities, &positions, &mut move_mode, &turns).join()
{
turn_done.push(entity);
match &mut mode.mode {
Movement::Static => {}
Movement::Random => {
let mut x = pos.x;
let mut y = pos.y;
match rng.roll_dice(1, 5) {
1 => x -= 1,
2 => x += 1,
3 => y -= 1,
4 => y += 1,
_ => {}
}
if x > 0 && x < map.width - 1 && y > 0 && y < map.height - 1 {
let dest_idx = map.xy_idx(x, y);
if !spatial::is_blocked(dest_idx) {
apply_move
.insert(entity, ApplyMove { dest_idx })
.expect("Unable to insert intent to move.");
turn_done.push(entity);
}
}
}
Movement::RandomWaypoint { path } => {
if let Some(path) = path {
// We have a target - go there
if path.len() > 1 {
if !spatial::is_blocked(path[1]) {
apply_move
.insert(entity, ApplyMove { dest_idx: path[1] })
.expect("Unable to insert intent to move.");
path.remove(0); // Remove the first step in the path
turn_done.push(entity);
}
} else {
// Otherwise we wait a turn to see if the path clears up
mode.mode = Movement::RandomWaypoint { path: None };
}
} else {
let target_x = rng.roll_dice(1, map.width - 2);
let target_y = rng.roll_dice(1, map.height - 2);
let idx = map.xy_idx(target_x, target_y);
if tile_walkable(map.tiles[idx]) {
let path = ::rltk::a_star_search(
map.xy_idx(pos.x, pos.y) as i32,
map.xy_idx(target_x, target_y) as i32,
&*map,
);
if path.success && path.steps.len() > 1 {
mode.mode = Movement::RandomWaypoint {
path: Some(path.steps),
};
}
}
}
}
}
}
// Remove turn marker for those that are done
for done in turn_done.iter() {
turns.remove(*done);
}
}
}