2019-05-17 20:47:58 +00:00
|
|
|
use crate::{
|
2019-06-06 14:48:41 +00:00
|
|
|
comp::{Dying, HealthSource, Stats},
|
2019-05-17 20:47:58 +00:00
|
|
|
state::DeltaTime,
|
|
|
|
};
|
2019-06-06 14:48:41 +00:00
|
|
|
use log::warn;
|
|
|
|
use specs::{Entities, Join, Read, System, WriteStorage};
|
2019-05-17 20:47:58 +00:00
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
/// This system kills players
|
2019-05-17 20:47:58 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-05-19 20:14:18 +00:00
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
WriteStorage<'a, Stats>,
|
2019-05-17 20:47:58 +00:00
|
|
|
WriteStorage<'a, Dying>,
|
|
|
|
);
|
|
|
|
|
2019-05-19 20:14:18 +00:00
|
|
|
fn run(&mut self, (entities, dt, mut stats, mut dyings): Self::SystemData) {
|
|
|
|
for (entity, mut stat) in (&entities, &mut stats).join() {
|
2019-05-27 17:45:43 +00:00
|
|
|
if stat.should_die() && !stat.is_dead {
|
|
|
|
// TODO: Replace is_dead with client states
|
2019-06-06 14:48:41 +00:00
|
|
|
if let Err(err) = dyings.insert(
|
2019-05-27 19:41:24 +00:00
|
|
|
entity,
|
|
|
|
Dying {
|
2019-06-30 11:48:28 +00:00
|
|
|
cause: match stat.health.last_change {
|
2019-06-06 14:48:41 +00:00
|
|
|
Some(change) => change.2,
|
|
|
|
None => {
|
|
|
|
warn!("Nothing caused an entity to die!");
|
|
|
|
HealthSource::Unknown
|
|
|
|
}
|
|
|
|
},
|
2019-05-27 19:41:24 +00:00
|
|
|
},
|
2019-06-06 14:48:41 +00:00
|
|
|
) {
|
|
|
|
warn!("Inserting Dying for an entity failed: {:?}", err);
|
|
|
|
}
|
2019-05-27 17:45:43 +00:00
|
|
|
stat.is_dead = true;
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
2019-06-30 11:48:28 +00:00
|
|
|
if let Some(change) = &mut stat.health.last_change {
|
2019-05-19 20:14:18 +00:00
|
|
|
change.1 += dt.0 as f64;
|
|
|
|
}
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|