2019-09-17 12:43:19 +00:00
|
|
|
use crate::{
|
2020-03-27 16:07:19 +00:00
|
|
|
comp::{
|
2020-08-03 21:54:33 +00:00
|
|
|
projectile, Damage, DamageSource, Energy, EnergySource, HealthChange, HealthSource,
|
|
|
|
Loadout, Ori, PhysicsState, Pos, Projectile, Vel,
|
2020-03-27 16:07:19 +00:00
|
|
|
},
|
2020-03-26 21:52:44 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2020-09-14 12:56:05 +00:00
|
|
|
metrics::SysMetrics,
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2019-10-06 17:30:06 +00:00
|
|
|
state::DeltaTime,
|
2020-03-26 21:52:44 +00:00
|
|
|
sync::UidAllocator,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2019-09-17 12:43:19 +00:00
|
|
|
};
|
2020-09-14 12:56:05 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::MarkerAllocator, Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage,
|
|
|
|
};
|
2019-10-06 17:30:06 +00:00
|
|
|
use std::time::Duration;
|
2020-03-26 21:52:44 +00:00
|
|
|
use vek::*;
|
2019-09-17 12:43:19 +00:00
|
|
|
|
|
|
|
/// This system is responsible for handling projectile effect triggers
|
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-09-17 12:43:19 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-10-06 17:30:06 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2020-03-26 21:52:44 +00:00
|
|
|
Read<'a, UidAllocator>,
|
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2019-09-17 12:43:19 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2020-09-14 12:56:05 +00:00
|
|
|
ReadExpect<'a, SysMetrics>,
|
2020-03-22 19:39:50 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
2019-09-17 12:43:19 +00:00
|
|
|
ReadStorage<'a, PhysicsState>,
|
2019-10-02 19:28:35 +00:00
|
|
|
ReadStorage<'a, Vel>,
|
2019-09-21 12:43:24 +00:00
|
|
|
WriteStorage<'a, Ori>,
|
2019-09-17 12:43:19 +00:00
|
|
|
WriteStorage<'a, Projectile>,
|
2020-03-27 16:07:19 +00:00
|
|
|
WriteStorage<'a, Energy>,
|
2020-07-25 23:57:04 +00:00
|
|
|
ReadStorage<'a, Loadout>,
|
2019-09-17 12:43:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-10-06 17:30:06 +00:00
|
|
|
dt,
|
2020-03-26 21:52:44 +00:00
|
|
|
uid_allocator,
|
|
|
|
local_bus,
|
2019-09-17 12:43:19 +00:00
|
|
|
server_bus,
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics,
|
2020-03-22 19:39:50 +00:00
|
|
|
positions,
|
2019-09-17 12:43:19 +00:00
|
|
|
physics_states,
|
2019-10-02 19:28:35 +00:00
|
|
|
velocities,
|
2019-09-21 12:43:24 +00:00
|
|
|
mut orientations,
|
2019-09-17 12:43:19 +00:00
|
|
|
mut projectiles,
|
2020-03-27 16:07:19 +00:00
|
|
|
mut energies,
|
2020-07-25 23:57:04 +00:00
|
|
|
loadouts,
|
2019-09-17 12:43:19 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "projectile::Sys::run");
|
2020-03-26 21:52:44 +00:00
|
|
|
let mut local_emitter = local_bus.emitter();
|
2019-09-17 12:43:19 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
|
|
|
|
// Attacks
|
2020-03-22 19:39:50 +00:00
|
|
|
for (entity, pos, physics, ori, projectile) in (
|
2019-09-17 12:43:19 +00:00
|
|
|
&entities,
|
2020-03-22 19:39:50 +00:00
|
|
|
&positions,
|
2019-09-17 12:43:19 +00:00
|
|
|
&physics_states,
|
2019-09-21 12:43:24 +00:00
|
|
|
&mut orientations,
|
2019-09-17 12:43:19 +00:00
|
|
|
&mut projectiles,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2020-08-24 23:04:04 +00:00
|
|
|
// Hit entity
|
|
|
|
for other in physics.touch_entities.iter().copied() {
|
|
|
|
if projectile.owner == Some(other) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-16 01:06:45 +00:00
|
|
|
for effect in projectile.hit_entity.drain(..) {
|
2019-10-06 17:30:06 +00:00
|
|
|
match effect {
|
2020-08-24 23:04:04 +00:00
|
|
|
projectile::Effect::Damage(healthchange) => {
|
|
|
|
let owner_uid = projectile.owner.unwrap();
|
|
|
|
let mut damage = Damage {
|
|
|
|
healthchange: healthchange as f32,
|
|
|
|
source: DamageSource::Projectile,
|
|
|
|
};
|
|
|
|
|
|
|
|
let other_entity = uid_allocator.retrieve_entity_internal(other.into());
|
|
|
|
if let Some(loadout) = other_entity.and_then(|e| loadouts.get(e)) {
|
|
|
|
damage.modify_damage(false, loadout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if other != owner_uid {
|
2020-09-09 19:26:59 +00:00
|
|
|
if damage.healthchange < 0.0 {
|
|
|
|
server_emitter.emit(ServerEvent::Damage {
|
|
|
|
uid: other,
|
|
|
|
change: HealthChange {
|
|
|
|
amount: damage.healthchange as i32,
|
|
|
|
cause: HealthSource::Projectile {
|
|
|
|
owner: Some(owner_uid),
|
|
|
|
},
|
2020-09-06 19:42:32 +00:00
|
|
|
},
|
2020-09-09 19:26:59 +00:00
|
|
|
});
|
|
|
|
} else if damage.healthchange > 0.0 {
|
|
|
|
server_emitter.emit(ServerEvent::Damage {
|
|
|
|
uid: other,
|
|
|
|
change: HealthChange {
|
|
|
|
amount: damage.healthchange as i32,
|
|
|
|
cause: HealthSource::Healing {
|
|
|
|
by: Some(owner_uid),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-08-24 23:04:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
projectile::Effect::Knockback(knockback) => {
|
|
|
|
if let Some(entity) =
|
|
|
|
uid_allocator.retrieve_entity_internal(other.into())
|
|
|
|
{
|
2020-09-19 16:55:31 +00:00
|
|
|
local_emitter.emit(LocalEvent::ApplyImpulse {
|
2020-08-24 23:04:04 +00:00
|
|
|
entity,
|
2020-09-19 16:55:31 +00:00
|
|
|
impulse: knockback
|
2020-08-24 23:04:04 +00:00
|
|
|
* *Dir::slerp(ori.0, Dir::new(Vec3::unit_z()), 0.5),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
projectile::Effect::RewardEnergy(energy) => {
|
|
|
|
if let Some(energy_mut) = projectile
|
|
|
|
.owner
|
|
|
|
.and_then(|o| uid_allocator.retrieve_entity_internal(o.into()))
|
|
|
|
.and_then(|o| energies.get_mut(o))
|
|
|
|
{
|
|
|
|
energy_mut.change_by(energy as i32, EnergySource::HitEnemy);
|
|
|
|
}
|
|
|
|
},
|
2020-08-29 20:44:46 +00:00
|
|
|
projectile::Effect::Explode {
|
|
|
|
power,
|
|
|
|
percent_damage,
|
|
|
|
} => server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
power,
|
|
|
|
owner: projectile.owner,
|
|
|
|
friendly_damage: false,
|
|
|
|
reagent: None,
|
|
|
|
percent_damage,
|
|
|
|
}),
|
2019-10-11 04:30:34 +00:00
|
|
|
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}),
|
2020-08-24 23:04:04 +00:00
|
|
|
projectile::Effect::Possess => {
|
|
|
|
if other != projectile.owner.unwrap() {
|
|
|
|
if let Some(owner) = projectile.owner {
|
|
|
|
server_emitter.emit(ServerEvent::Possess(owner, other));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-10-06 17:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 23:04:04 +00:00
|
|
|
}
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-08-24 23:04:04 +00:00
|
|
|
// Hit something solid
|
|
|
|
if physics.on_wall.is_some() || physics.on_ground || physics.on_ceiling {
|
|
|
|
for effect in projectile.hit_solid.drain(..) {
|
|
|
|
match effect {
|
2020-08-29 20:44:46 +00:00
|
|
|
projectile::Effect::Explode {
|
|
|
|
power,
|
|
|
|
percent_damage,
|
|
|
|
} => server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
power,
|
|
|
|
owner: projectile.owner,
|
|
|
|
friendly_damage: false,
|
|
|
|
reagent: None,
|
|
|
|
percent_damage,
|
|
|
|
}),
|
2020-08-24 23:04:04 +00:00
|
|
|
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}),
|
|
|
|
_ => {},
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 09:31:24 +00:00
|
|
|
} else if let Some(dir) = velocities
|
2020-08-24 23:04:04 +00:00
|
|
|
.get(entity)
|
|
|
|
.and_then(|vel| vel.0.try_normalized())
|
|
|
|
{
|
|
|
|
ori.0 = dir.into();
|
2019-10-02 19:28:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 17:30:06 +00:00
|
|
|
if projectile.time_left == Duration::default() {
|
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
projectile.time_left = projectile
|
|
|
|
.time_left
|
|
|
|
.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
.unwrap_or_default();
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics.projectile_ns.store(
|
|
|
|
start_time.elapsed().as_nanos() as i64,
|
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|