2020-08-10 22:54:45 +00:00
|
|
|
use crate::{
|
2020-10-13 00:48:25 +00:00
|
|
|
comp::{
|
2020-10-24 20:12:37 +00:00
|
|
|
BuffCategory, BuffChange, BuffEffect, BuffId, BuffSource, Buffs, HealthChange,
|
|
|
|
HealthSource, Stats,
|
2020-10-13 00:48:25 +00:00
|
|
|
},
|
2020-10-01 02:32:38 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-08-10 22:54:45 +00:00
|
|
|
state::DeltaTime,
|
2020-10-01 02:32:38 +00:00
|
|
|
sync::Uid,
|
2020-08-10 22:54:45 +00:00
|
|
|
};
|
2020-10-03 18:48:56 +00:00
|
|
|
use specs::{Join, Read, ReadStorage, System, WriteStorage};
|
2020-08-10 22:54:45 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
type SystemData = (
|
|
|
|
Read<'a, DeltaTime>,
|
2020-10-01 02:32:38 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
ReadStorage<'a, Uid>,
|
2020-10-13 00:48:25 +00:00
|
|
|
ReadStorage<'a, Stats>,
|
2020-08-10 22:54:45 +00:00
|
|
|
WriteStorage<'a, Buffs>,
|
|
|
|
);
|
|
|
|
|
2020-10-13 00:48:25 +00:00
|
|
|
fn run(&mut self, (dt, server_bus, uids, stats, mut buffs): Self::SystemData) {
|
2020-10-01 02:32:38 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
2020-10-19 03:00:35 +00:00
|
|
|
// Set to false to avoid spamming server
|
|
|
|
buffs.set_event_emission(false);
|
2020-10-24 20:12:37 +00:00
|
|
|
for (buff_comp, uid, stat) in (&mut buffs, &uids, &stats).join() {
|
|
|
|
let mut expired_buffs = Vec::<BuffId>::new();
|
|
|
|
for (id, buff) in buff_comp.buffs.iter_mut() {
|
2020-10-19 03:00:35 +00:00
|
|
|
// Tick the buff and subtract delta from it
|
2020-10-24 20:12:37 +00:00
|
|
|
if let Some(remaining_time) = &mut buff.time {
|
|
|
|
if let Some(new_duration) =
|
|
|
|
remaining_time.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
{
|
2020-08-10 22:54:45 +00:00
|
|
|
// The buff still continues.
|
2020-10-24 20:12:37 +00:00
|
|
|
*remaining_time = new_duration;
|
2020-08-10 22:54:45 +00:00
|
|
|
} else {
|
|
|
|
// The buff has expired.
|
2020-10-01 01:35:57 +00:00
|
|
|
// Remove it.
|
2020-10-24 20:12:37 +00:00
|
|
|
expired_buffs.push(*id);
|
|
|
|
}
|
2020-10-19 03:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-10 22:54:45 +00:00
|
|
|
|
2020-10-24 20:12:37 +00:00
|
|
|
for buff_ids in buff_comp.kinds.values() {
|
|
|
|
if let Some(buff) = buff_comp.buffs.get_mut(&buff_ids[0]) {
|
|
|
|
// Get buff owner
|
|
|
|
let buff_owner = if let BuffSource::Character { by: owner } = buff.source {
|
|
|
|
Some(owner)
|
2020-10-19 03:00:35 +00:00
|
|
|
} else {
|
2020-10-24 20:12:37 +00:00
|
|
|
None
|
2020-10-19 03:00:35 +00:00
|
|
|
};
|
2020-10-24 20:12:37 +00:00
|
|
|
// Now, execute the buff, based on it's delta
|
|
|
|
for effect in &mut buff.effects {
|
|
|
|
match effect {
|
|
|
|
// Only add an effect here if it is continuous or it is not immediate
|
|
|
|
BuffEffect::HealthChangeOverTime { rate, accumulated } => {
|
|
|
|
*accumulated += *rate * dt.0;
|
|
|
|
// Apply damage only once a second (with a minimum of 1 damage), or
|
|
|
|
// when a buff is removed
|
|
|
|
if accumulated.abs() > rate.abs().max(10.0)
|
|
|
|
|| buff.time.map_or(false, |dur| dur == Duration::default())
|
|
|
|
{
|
|
|
|
let cause = if *accumulated > 0.0 {
|
|
|
|
HealthSource::Healing { by: buff_owner }
|
|
|
|
} else {
|
|
|
|
HealthSource::Buff { owner: buff_owner }
|
|
|
|
};
|
|
|
|
server_emitter.emit(ServerEvent::Damage {
|
|
|
|
uid: *uid,
|
|
|
|
change: HealthChange {
|
|
|
|
amount: *accumulated as i32,
|
|
|
|
cause,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
*accumulated = 0.0;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
BuffEffect::NameChange { .. } => {},
|
|
|
|
};
|
|
|
|
}
|
2020-10-19 03:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 20:12:37 +00:00
|
|
|
// Remove buffs that expire
|
|
|
|
if !expired_buffs.is_empty() {
|
2020-10-19 03:00:35 +00:00
|
|
|
server_emitter.emit(ServerEvent::Buff {
|
|
|
|
uid: *uid,
|
2020-10-24 20:12:37 +00:00
|
|
|
buff_change: BuffChange::RemoveById(expired_buffs),
|
2020-10-19 03:00:35 +00:00
|
|
|
});
|
|
|
|
}
|
2020-10-11 21:39:52 +00:00
|
|
|
|
2020-10-24 20:12:37 +00:00
|
|
|
// Remove stats that don't persist on death
|
2020-10-13 00:48:25 +00:00
|
|
|
if stat.is_dead {
|
|
|
|
server_emitter.emit(ServerEvent::Buff {
|
|
|
|
uid: *uid,
|
|
|
|
buff_change: BuffChange::RemoveByCategory {
|
2020-10-24 20:12:37 +00:00
|
|
|
all_required: vec![],
|
|
|
|
any_required: vec![],
|
|
|
|
none_required: vec![BuffCategory::PersistOnDeath],
|
2020-10-13 00:48:25 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
2020-10-24 20:12:37 +00:00
|
|
|
buffs.set_event_emission(true);
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
}
|