veloren/common/src/sys/stats.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

use crate::{
comp::{HealthSource, Stats},
2019-08-25 16:48:12 +00:00
event::{EventBus, ServerEvent},
state::DeltaTime,
};
use log::warn;
use specs::{Entities, Join, Read, System, WriteStorage};
2019-06-09 19:33:20 +00:00
/// This system kills players
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
Read<'a, DeltaTime>,
Read<'a, EventBus<ServerEvent>>,
WriteStorage<'a, Stats>,
);
fn run(&mut self, (entities, dt, event_bus, mut stats): Self::SystemData) {
let mut event_emitter = event_bus.emitter();
for (entity, mut stat) in (&entities, &mut stats).join() {
if stat.should_die() && !stat.is_dead {
2019-09-17 12:43:19 +00:00
event_emitter.emit(ServerEvent::Destroy {
entity,
cause: match stat.health.last_change {
Some(change) => change.2,
None => {
warn!("Nothing caused an entity to die!");
HealthSource::Unknown
}
},
});
stat.is_dead = true;
}
2019-06-30 11:48:28 +00:00
if let Some(change) = &mut stat.health.last_change {
change.1 += f64::from(dt.0);
}
if stat.exp.current() >= stat.exp.maximum() {
while stat.exp.current() >= stat.exp.maximum() {
stat.exp.change_by(-(stat.exp.maximum() as i64));
stat.exp.change_maximum_by(25);
stat.level.change_by(1);
}
2019-10-08 16:12:08 +00:00
stat.update_max_hp();
stat.health
.set_to(stat.health.maximum(), HealthSource::LevelUp)
}
}
}
}