roguelike-game/src/systems/ai/turn_status.rs

63 lines
1.9 KiB
Rust
Raw Normal View History

use std::collections::HashSet;
use ::bracket_lib::prelude::*;
2022-01-11 10:30:32 -05:00
use ::specs::prelude::*;
use crate::components::{Confusion, MyTurn, StatusEffect};
use crate::effects::{add_effect, EffectType, Targets};
use crate::{colors, RunState};
2022-01-11 10:30:32 -05:00
pub struct TurnStatusSystem {}
impl<'a> System<'a> for TurnStatusSystem {
#[allow(clippy::type_complexity)]
type SystemData = (
WriteStorage<'a, MyTurn>,
ReadStorage<'a, Confusion>,
2022-01-11 10:30:32 -05:00
Entities<'a>,
ReadExpect<'a, RunState>,
ReadStorage<'a, StatusEffect>,
2022-01-11 10:30:32 -05:00
);
fn run(&mut self, data: Self::SystemData) {
let (mut turns, confusion, entities, runstate, statuses) = data;
2022-01-11 10:30:32 -05:00
if *runstate != RunState::Ticking {
return;
}
// Collect a set of all entities whose turn it is
let mut entity_turns = HashSet::new();
for (entity, _turn) in (&entities, &turns).join() {
entity_turns.insert(entity);
}
2022-01-11 10:30:32 -05:00
// Find status effects affecting entities whose turn it is
let mut not_my_turn: Vec<Entity> = Vec::new();
for (effect_entity, status_effect) in (&entities, &statuses).join() {
if entity_turns.contains(&status_effect.target) {
// Skip turn for confusion
if confusion.get(effect_entity).is_some() {
add_effect(
None,
EffectType::Particle {
glyph: to_cp437('?'),
fg: colors::CYAN,
bg: colors::BLACK,
lifespan: 200.0,
},
Targets::Single {
target: status_effect.target,
},
);
not_my_turn.push(status_effect.target);
}
2022-01-11 10:30:32 -05:00
}
}
for e in not_my_turn {
turns.remove(e);
}
}
}