2022-01-27 18:28:40 +01:00
|
|
|
use rltk::Point;
|
2022-01-25 22:18:17 +01:00
|
|
|
use specs::prelude::*;
|
|
|
|
|
2022-01-29 00:31:16 +01:00
|
|
|
use crate::particle_system::ParticleBuilder;
|
2022-01-27 18:28:40 +01:00
|
|
|
use crate::{
|
|
|
|
components::{Monster, Position, Viewshed, WantsToMelee},
|
2022-01-29 00:31:16 +01:00
|
|
|
map::Map,
|
2022-01-29 16:07:01 +01:00
|
|
|
Confusion, EntityMoved, RunState,
|
2022-01-27 18:28:40 +01:00
|
|
|
};
|
2022-01-25 22:18:17 +01:00
|
|
|
|
|
|
|
pub struct MonsterAI {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for MonsterAI {
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
type SystemData = (
|
|
|
|
WriteExpect<'a, Map>,
|
|
|
|
ReadExpect<'a, Point>,
|
|
|
|
ReadExpect<'a, Entity>,
|
|
|
|
ReadExpect<'a, RunState>,
|
|
|
|
Entities<'a>,
|
|
|
|
WriteStorage<'a, Viewshed>,
|
|
|
|
ReadStorage<'a, Monster>,
|
|
|
|
WriteStorage<'a, Position>,
|
|
|
|
WriteStorage<'a, WantsToMelee>,
|
2022-01-27 14:31:31 +01:00
|
|
|
WriteStorage<'a, Confusion>,
|
2022-01-29 00:31:16 +01:00
|
|
|
WriteExpect<'a, ParticleBuilder>,
|
2022-01-29 16:07:01 +01:00
|
|
|
WriteStorage<'a, EntityMoved>,
|
2022-01-25 22:18:17 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
|
|
|
let (
|
|
|
|
mut map,
|
|
|
|
player_pos,
|
|
|
|
player_entity,
|
|
|
|
runstate,
|
|
|
|
entities,
|
|
|
|
mut viewshed,
|
|
|
|
monster,
|
|
|
|
mut position,
|
|
|
|
mut wants_to_melee,
|
2022-01-27 14:31:31 +01:00
|
|
|
mut confused,
|
2022-01-29 00:31:16 +01:00
|
|
|
mut particle_builder,
|
2022-01-29 16:07:01 +01:00
|
|
|
mut entity_moved,
|
2022-01-25 22:18:17 +01:00
|
|
|
) = data;
|
|
|
|
|
|
|
|
if *runstate != RunState::MonsterTurn {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (entity, mut viewshed, _monster, mut pos) in
|
2022-01-29 00:31:16 +01:00
|
|
|
(&entities, &mut viewshed, &monster, &mut position).join()
|
2022-01-25 22:18:17 +01:00
|
|
|
{
|
2022-01-27 14:31:31 +01:00
|
|
|
let mut can_act = true;
|
|
|
|
|
|
|
|
if let Some(i_am_confused) = confused.get_mut(entity) {
|
|
|
|
i_am_confused.turns -= 1;
|
|
|
|
if i_am_confused.turns < 1 {
|
|
|
|
confused.remove(entity);
|
|
|
|
}
|
|
|
|
can_act = false;
|
2022-01-28 21:21:29 +01:00
|
|
|
|
2022-01-29 00:31:16 +01:00
|
|
|
particle_builder.request(
|
|
|
|
pos.x,
|
|
|
|
pos.y,
|
|
|
|
rltk::RGB::named(rltk::MAGENTA),
|
|
|
|
rltk::RGB::named(rltk::BLACK),
|
|
|
|
rltk::to_cp437('?'),
|
|
|
|
200.,
|
|
|
|
);
|
2022-01-27 14:31:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !can_act {
|
2022-01-27 20:14:25 +01:00
|
|
|
continue;
|
2022-01-27 14:31:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 22:18:17 +01:00
|
|
|
let distance =
|
|
|
|
rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos);
|
|
|
|
if distance < 1.5 {
|
|
|
|
wants_to_melee
|
|
|
|
.insert(
|
|
|
|
entity,
|
|
|
|
WantsToMelee {
|
|
|
|
target: *player_entity,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Unable to insert attack");
|
|
|
|
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
|
|
|
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 {
|
|
|
|
let mut idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
map.blocked[idx] = false;
|
|
|
|
pos.x = path.steps[1] as i32 % map.width;
|
|
|
|
pos.y = path.steps[1] as i32 / map.width;
|
|
|
|
idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
map.blocked[idx] = true;
|
|
|
|
viewshed.dirty = true;
|
2022-01-29 16:07:01 +01:00
|
|
|
|
|
|
|
entity_moved
|
|
|
|
.insert(entity, EntityMoved {})
|
|
|
|
.expect("Unable to insert entity moved");
|
2022-01-25 22:18:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|