scroll of confusion

This commit is contained in:
Llywelwyn 2023-07-09 09:51:42 +01:00
parent d4d25955cc
commit 22d90a46b4
5 changed files with 107 additions and 28 deletions

View file

@ -1,4 +1,4 @@
use super::{Map, Monster, Position, RunState, Viewshed, WantsToMelee};
use super::{gamelog::GameLog, Confusion, Map, Monster, Name, Position, RunState, Viewshed, WantsToMelee};
use rltk::Point;
use specs::prelude::*;
@ -8,6 +8,7 @@ impl<'a> System<'a> for MonsterAI {
#[allow(clippy::type_complexity)]
type SystemData = (
WriteExpect<'a, Map>,
WriteExpect<'a, GameLog>,
ReadExpect<'a, Point>,
ReadExpect<'a, Entity>,
ReadExpect<'a, RunState>,
@ -16,11 +17,14 @@ impl<'a> System<'a> for MonsterAI {
ReadStorage<'a, Monster>,
WriteStorage<'a, Position>,
WriteStorage<'a, WantsToMelee>,
WriteStorage<'a, Confusion>,
ReadStorage<'a, Name>,
);
fn run(&mut self, data: Self::SystemData) {
let (
mut map,
mut gamelog,
player_pos,
player_entity,
runstate,
@ -29,6 +33,8 @@ impl<'a> System<'a> for MonsterAI {
monster,
mut position,
mut wants_to_melee,
mut confused,
name,
) = data;
if *runstate != RunState::MonsterTurn {
@ -36,25 +42,42 @@ impl<'a> System<'a> for MonsterAI {
}
for (entity, mut viewshed, _monster, mut pos) in (&entities, &mut viewshed, &monster, &mut position).join() {
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) {
// If the player is visible, but the path is obstructed, this will currently search
// the entire map (i.e. Will do a huge ASTAR to find an alternate route), and the
// mob will follow that path until it leaves vision, then lose sight of the player
// and stop.
let path = rltk::a_star_search(map.xy_idx(pos.x, pos.y), map.xy_idx(player_pos.x, player_pos.y), &*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;
let mut can_act = true;
// Check confusion
let is_confused = confused.get_mut(entity);
if let Some(i_am_confused) = is_confused {
i_am_confused.turns -= 1;
if i_am_confused.turns < 1 {
confused.remove(entity);
}
let entity_name = name.get(entity).unwrap();
gamelog.entries.push(format!("{} is confused!", entity_name.name));
can_act = false;
}
if can_act {
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) {
// If the player is visible, but the path is obstructed, this will currently search
// the entire map (i.e. Will do a huge ASTAR to find an alternate route), and the
// mob will follow that path until it leaves vision, then lose sight of the player
// and stop.
let path =
rltk::a_star_search(map.xy_idx(pos.x, pos.y), map.xy_idx(player_pos.x, player_pos.y), &*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;
}
}
}
}