use crate::components::{CombatStats, Name, Player, SufferDamage}; use crate::{game_log::GameLog, Map, Position, RunState}; use specs::prelude::*; pub struct DamageSystem {} impl<'a> System<'a> for DamageSystem { type SystemData = ( WriteStorage<'a, CombatStats>, WriteStorage<'a, SufferDamage>, ReadStorage<'a, Position>, WriteExpect<'a, Map>, Entities<'a>, ); fn run(&mut self, data: Self::SystemData) { let (mut stats, mut damage, positions, mut map, entities) = data; for (entity, mut stats, damage) in (&entities, &mut stats, &damage).join() { stats.hp -= damage.amount.iter().sum::(); if let Some(pos) = positions.get(entity) { let idx = map.xy_idx(pos.x, pos.y); map.bloodstains.insert(idx); } } damage.clear(); } } pub fn delete_the_dead(ecs: &mut World) { let mut dead: Vec = Vec::new(); // Scope for the sake of the borrow checker { let combat_stats = ecs.read_storage::(); let players = ecs.read_storage::(); let names = ecs.read_storage::(); let entities = ecs.entities(); let mut log = ecs.write_resource::(); for (entity, stats) in (&entities, &combat_stats).join() { if stats.hp < 1 { let player = players.get(entity); match player { 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) } Some(_) => { let mut runstate = ecs.write_resource::(); *runstate = RunState::GameOver; } } } } } for victim in dead { ecs.delete_entity(victim) .expect("Unable to delete the dead"); } }