Beginning the conversion to an Effects system

This commit is contained in:
Llywelwyn 2023-08-16 15:11:49 +01:00
parent a6690029e6
commit efe15705ad
13 changed files with 382 additions and 67 deletions

View file

@ -16,59 +16,56 @@ impl<'a> System<'a> for AdjacentAI {
);
fn run(&mut self, data: Self::SystemData) {
let (mut turns, factions, positions, map, mut wants_to_melee, entities, player) = data;
let (mut turns, factions, positions, map, mut want_melee, entities, player) = data;
let mut turn_done: Vec<Entity> = Vec::new();
for (entity, _turn, faction, pos) in (&entities, &turns, &factions, &positions).join() {
if entity == *player {
continue;
}
let mut reactions: Vec<(Entity, Reaction)> = Vec::new();
let idx = map.xy_idx(pos.x, pos.y);
let (w, h) = (map.width, map.height);
// Evaluate adjacent squares, add possible reactions
let mut eval_idx: usize = idx;
if pos.x > 0 {
eval_idx = idx - 1;
}
if pos.x < w - 1 {
eval_idx = idx + 1;
}
if pos.y > 0 {
eval_idx = idx - w as usize;
}
if pos.y < h - 1 {
eval_idx = idx + w as usize;
}
if pos.y > 0 && pos.x > 0 {
eval_idx = (idx - w as usize) - 1;
}
if pos.y > 0 && pos.x < w - 1 {
eval_idx = (idx - w as usize) + 1;
}
if pos.y < h - 1 && pos.x > 0 {
eval_idx = (idx + w as usize) - 1;
}
if pos.y < h - 1 && pos.x < w - 1 {
eval_idx = (idx + w as usize) + 1;
}
if eval_idx != idx {
evaluate(eval_idx, &factions, &faction.name, &mut reactions);
}
let mut done = false;
for reaction in reactions.iter() {
if let Reaction::Attack = reaction.1 {
wants_to_melee
.insert(entity, WantsToMelee { target: reaction.0 })
.expect("Error inserting WantsToMelee");
done = true;
for (entity, _turn, my_faction, pos) in (&entities, &turns, &factions, &positions).join() {
if entity != *player {
let mut reactions: Vec<(Entity, Reaction)> = Vec::new();
let idx = map.xy_idx(pos.x, pos.y);
let w = map.width;
let h = map.height;
// Add possible reactions to adjacents for each direction
if pos.x > 0 {
evaluate(idx - 1, &factions, &my_faction.name, &mut reactions);
}
if pos.x < w - 1 {
evaluate(idx + 1, &factions, &my_faction.name, &mut reactions);
}
if pos.y > 0 {
evaluate(idx - w as usize, &factions, &my_faction.name, &mut reactions);
}
if pos.y < h - 1 {
evaluate(idx + w as usize, &factions, &my_faction.name, &mut reactions);
}
if pos.y > 0 && pos.x > 0 {
evaluate((idx - w as usize) - 1, &factions, &my_faction.name, &mut reactions);
}
if pos.y > 0 && pos.x < w - 1 {
evaluate((idx - w as usize) + 1, &factions, &my_faction.name, &mut reactions);
}
if pos.y < h - 1 && pos.x > 0 {
evaluate((idx + w as usize) - 1, &factions, &my_faction.name, &mut reactions);
}
if pos.y < h - 1 && pos.x < w - 1 {
evaluate((idx + w as usize) + 1, &factions, &my_faction.name, &mut reactions);
}
let mut done = false;
for reaction in reactions.iter() {
if let Reaction::Attack = reaction.1 {
want_melee.insert(entity, WantsToMelee { target: reaction.0 }).expect("Error inserting melee");
done = true;
}
}
if done {
turn_done.push(entity);
}
}
if done {
turn_done.push(entity);
}
}
// Remove turn from entities that are done
// Remove turn marker for those that are done
for done in turn_done.iter() {
turns.remove(*done);
}