2021-11-17 15:55:48 -05:00
|
|
|
use rltk::{Point, RGB};
|
2021-10-26 15:43:59 -04:00
|
|
|
use specs::prelude::*;
|
|
|
|
|
2021-12-10 20:16:48 -05:00
|
|
|
use crate::components::{Confusion, Monster, Position, Viewshed, WantsToMelee};
|
|
|
|
use crate::particle_system::ParticleBuilder;
|
|
|
|
use crate::{EntityMoved, Map, RunState};
|
|
|
|
|
2021-10-26 15:43:59 -04:00
|
|
|
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 15:15:22 -04:00
|
|
|
ReadExpect<'a, Entity>,
|
|
|
|
ReadExpect<'a, RunState>,
|
|
|
|
Entities<'a>,
|
2021-10-29 10:30:17 -04:00
|
|
|
WriteStorage<'a, Viewshed>,
|
2021-10-26 15:43:59 -04:00
|
|
|
ReadStorage<'a, Monster>,
|
2021-10-29 10:30:17 -04:00
|
|
|
WriteStorage<'a, Position>,
|
2021-10-29 15:15:22 -04:00
|
|
|
WriteStorage<'a, WantsToMelee>,
|
2021-11-05 14:32:14 -04:00
|
|
|
WriteStorage<'a, Confusion>,
|
2021-11-17 15:55:48 -05:00
|
|
|
WriteExpect<'a, ParticleBuilder>,
|
2021-11-29 16:00:07 -05:00
|
|
|
WriteStorage<'a, EntityMoved>,
|
2021-10-26 15:43:59 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2021-10-29 15:15:22 -04:00
|
|
|
let (
|
|
|
|
mut map,
|
|
|
|
player_pos,
|
|
|
|
player_entity,
|
|
|
|
runstate,
|
|
|
|
entities,
|
|
|
|
mut viewshed,
|
|
|
|
monster,
|
|
|
|
mut position,
|
|
|
|
mut wants_to_melee,
|
2021-11-05 14:32:14 -04:00
|
|
|
mut confused,
|
2021-11-17 15:55:48 -05:00
|
|
|
mut particle_builder,
|
2021-11-29 16:00:07 -05:00
|
|
|
mut entity_moved,
|
2021-10-29 15:15:22 -04:00
|
|
|
) = data;
|
2021-10-26 15:43:59 -04:00
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
if *runstate != RunState::MonsterTurn {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (entity, mut viewshed, _monster, mut pos) in
|
|
|
|
(&entities, &mut viewshed, &monster, &mut position).join()
|
2021-10-29 10:30:17 -04:00
|
|
|
{
|
2021-11-05 14:32:14 -04:00
|
|
|
let mut can_act = true;
|
|
|
|
|
2021-11-19 19:52:15 -05:00
|
|
|
if let Some(i_am_confused) = confused.get_mut(entity) {
|
2021-11-05 14:32:14 -04:00
|
|
|
i_am_confused.turns -= 1;
|
|
|
|
if i_am_confused.turns < 1 {
|
|
|
|
confused.remove(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
can_act = false;
|
2021-11-17 15:55:48 -05:00
|
|
|
|
|
|
|
particle_builder.request(
|
|
|
|
pos.x,
|
|
|
|
pos.y,
|
|
|
|
RGB::named(rltk::MAGENTA),
|
|
|
|
RGB::named(rltk::BLACK),
|
|
|
|
rltk::to_cp437('?'),
|
|
|
|
200.0,
|
|
|
|
);
|
2021-11-05 14:32:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if can_act {
|
|
|
|
let distance =
|
|
|
|
rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos);
|
|
|
|
if distance < 1.5 {
|
|
|
|
// Attack goes here
|
|
|
|
wants_to_melee
|
|
|
|
.insert(
|
|
|
|
entity,
|
|
|
|
WantsToMelee {
|
|
|
|
target: *player_entity,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Unable to insert attack");
|
|
|
|
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
|
|
|
// The path to the player
|
|
|
|
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,
|
2021-11-16 10:45:39 -05:00
|
|
|
&*map,
|
2021-11-05 14:32:14 -04:00
|
|
|
);
|
2021-10-29 10:30:17 -04:00
|
|
|
|
2021-11-05 14:32:14 -04:00
|
|
|
if path.success && path.steps.len() > 1 {
|
|
|
|
let mut idx = map.xy_idx(pos.x, pos.y);
|
2021-10-29 15:15:22 -04:00
|
|
|
|
2021-11-05 14:32:14 -04:00
|
|
|
map.blocked[idx] = false;
|
|
|
|
pos.x = path.steps[1] as i32 % map.width;
|
|
|
|
pos.y = path.steps[1] as i32 / map.width;
|
2021-10-29 10:30:17 -04:00
|
|
|
|
2021-11-29 16:00:07 -05:00
|
|
|
entity_moved
|
|
|
|
.insert(entity, EntityMoved {})
|
|
|
|
.expect("Unable to add EntityMoved flag to monster");
|
|
|
|
|
2021-11-05 14:32:14 -04:00
|
|
|
idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
map.blocked[idx] = true;
|
|
|
|
viewshed.dirty = true;
|
|
|
|
}
|
2021-10-29 10:30:17 -04:00
|
|
|
}
|
2021-10-26 15:43:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|