2019-05-17 20:47:58 +00:00
|
|
|
use crate::{
|
2019-08-23 10:11:37 +00:00
|
|
|
comp::{HealthSource, Stats},
|
2019-11-23 08:26:39 +00:00
|
|
|
event::{EventBus, ServerEvent, SfxEvent, SfxEventItem},
|
2019-05-17 20:47:58 +00:00
|
|
|
state::DeltaTime,
|
|
|
|
};
|
2019-06-06 14:48:41 +00:00
|
|
|
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-11-23 08:26:39 +00:00
|
|
|
/// and handles players levelling up
|
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>,
|
2019-08-25 14:49:54 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2019-11-23 08:26:39 +00:00
|
|
|
Read<'a, EventBus<SfxEventItem>>,
|
2019-05-19 20:14:18 +00:00
|
|
|
WriteStorage<'a, Stats>,
|
2019-05-17 20:47:58 +00:00
|
|
|
);
|
|
|
|
|
2019-11-23 08:26:39 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(entities, dt, server_event_bus, audio_event_bus, mut stats): Self::SystemData,
|
|
|
|
) {
|
|
|
|
let mut server_event_emitter = server_event_bus.emitter();
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2019-05-19 20:14:18 +00:00
|
|
|
for (entity, mut stat) in (&entities, &mut stats).join() {
|
2019-05-27 17:45:43 +00:00
|
|
|
if stat.should_die() && !stat.is_dead {
|
2019-11-23 08:26:39 +00:00
|
|
|
server_event_emitter.emit(ServerEvent::Destroy {
|
2019-05-27 19:41:24 +00:00
|
|
|
entity,
|
2019-10-17 20:59:36 +00:00
|
|
|
cause: stat.health.last_change.1.cause,
|
2019-08-23 10:11:37 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 17:45:43 +00:00
|
|
|
stat.is_dead = true;
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2019-10-17 20:59:36 +00:00
|
|
|
stat.health.last_change.0 += f64::from(dt.0);
|
2019-08-03 19:30:01 +00:00
|
|
|
|
|
|
|
if stat.exp.current() >= stat.exp.maximum() {
|
2019-10-05 11:38:33 +00:00
|
|
|
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-11-23 08:26:39 +00:00
|
|
|
|
|
|
|
audio_event_bus
|
|
|
|
.emitter()
|
|
|
|
.emit(SfxEventItem::at_player_position(SfxEvent::LevelUp));
|
|
|
|
|
2019-10-08 16:12:08 +00:00
|
|
|
stat.update_max_hp();
|
2019-08-03 19:30:01 +00:00
|
|
|
stat.health
|
|
|
|
.set_to(stat.health.maximum(), HealthSource::LevelUp)
|
|
|
|
}
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|