2021-11-04 10:44:52 -04:00
|
|
|
use crate::{game_log::GameLog, CombatStats, Name, Player, SufferDamage};
|
2021-10-29 15:15:22 -04:00
|
|
|
use rltk::console;
|
|
|
|
use specs::prelude::*;
|
|
|
|
|
|
|
|
pub struct DamageSystem {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for DamageSystem {
|
|
|
|
type SystemData = (
|
|
|
|
WriteStorage<'a, CombatStats>,
|
|
|
|
WriteStorage<'a, SufferDamage>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
|
|
|
let (mut stats, mut damage) = data;
|
|
|
|
|
|
|
|
for (mut stats, damage) in (&mut stats, &damage).join() {
|
|
|
|
stats.hp -= damage.amount.iter().sum::<i32>();
|
|
|
|
}
|
|
|
|
|
|
|
|
damage.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_the_dead(ecs: &mut World) {
|
|
|
|
let mut dead: Vec<Entity> = Vec::new();
|
|
|
|
|
|
|
|
// Scope for the sake of the borrow checker
|
|
|
|
{
|
|
|
|
let combat_stats = ecs.read_storage::<CombatStats>();
|
|
|
|
let players = ecs.read_storage::<Player>();
|
2021-11-01 14:46:45 -04:00
|
|
|
let names = ecs.read_storage::<Name>();
|
2021-10-29 15:15:22 -04:00
|
|
|
let entities = ecs.entities();
|
2021-11-01 14:46:45 -04:00
|
|
|
let mut log = ecs.write_resource::<GameLog>();
|
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
for (entity, stats) in (&entities, &combat_stats).join() {
|
|
|
|
if stats.hp < 1 {
|
|
|
|
let player = players.get(entity);
|
|
|
|
match player {
|
2021-11-01 14:46:45 -04:00
|
|
|
None => {
|
|
|
|
let victim_name = names.get(entity);
|
|
|
|
if let Some(victim_name) = victim_name {
|
|
|
|
log.entries.push(format!("{} is dead", &victim_name.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
dead.push(entity)
|
|
|
|
}
|
2021-10-29 15:15:22 -04:00
|
|
|
Some(_) => console::log("You are dead"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for victim in dead {
|
|
|
|
ecs.delete_entity(victim)
|
|
|
|
.expect("Unable to delete the dead");
|
|
|
|
}
|
|
|
|
}
|