veloren/common/src/sys/projectile.rs

223 lines
8.8 KiB
Rust
Raw Normal View History

2019-09-17 12:43:19 +00:00
use crate::{
2020-03-27 16:07:19 +00:00
comp::{
2020-11-01 18:38:57 +00:00
buff::{BuffChange, BuffSource},
projectile, Energy, EnergySource, Group, HealthSource, Loadout, Ori, PhysicsState, Pos,
Projectile, Vel,
2020-03-27 16:07:19 +00:00
},
event::{EventBus, LocalEvent, ServerEvent},
metrics::SysMetrics,
span,
state::DeltaTime,
sync::UidAllocator,
2019-09-17 12:43:19 +00:00
};
2020-11-01 18:38:57 +00:00
use rand::{thread_rng, Rng};
use specs::{
saveload::MarkerAllocator, Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage,
};
use std::time::Duration;
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 {
#[allow(clippy::type_complexity)]
2019-09-17 12:43:19 +00:00
type SystemData = (
Entities<'a>,
Read<'a, DeltaTime>,
Read<'a, UidAllocator>,
Read<'a, EventBus<LocalEvent>>,
2019-09-17 12:43:19 +00:00
Read<'a, EventBus<ServerEvent>>,
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>,
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>,
ReadStorage<'a, Group>,
2019-09-17 12:43:19 +00:00
);
fn run(
&mut self,
(
entities,
dt,
uid_allocator,
local_bus,
2019-09-17 12:43:19 +00:00
server_bus,
sys_metrics,
2020-03-22 19:39:50 +00:00
positions,
2019-09-17 12:43:19 +00:00
physics_states,
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,
groups,
2019-09-17 12:43:19 +00:00
): Self::SystemData,
) {
let start_time = std::time::Instant::now();
span!(_guard, "run", "projectile::Sys::run");
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()
{
// Hit entity
for other in physics.touch_entities.iter().copied() {
let same_group = projectile
.owner
// Note: somewhat inefficient since we do the lookup for every touching
// entity, but if we pull this out of the loop we would want to do it only
// if there is at least one touching entity
.and_then(|uid| uid_allocator.retrieve_entity_internal(uid.into()))
.and_then(|e| groups.get(e))
.map_or(false, |owner_group|
Some(owner_group) == uid_allocator
.retrieve_entity_internal(other.into())
.and_then(|e| groups.get(e))
);
if projectile.ignore_group
// Skip if in the same group
&& same_group
{
continue;
}
if projectile.owner == Some(other) {
continue;
}
for effect in projectile.hit_entity.drain(..) {
match effect {
projectile::Effect::Damages(damages) => {
if Some(other) == projectile.owner {
continue;
}
2020-10-27 22:16:17 +00:00
let damage = if let Some(damage) = damages.get_damage(same_group) {
damage
} else {
continue;
};
let other_entity_loadout = uid_allocator
.retrieve_entity_internal(other.into())
.and_then(|e| loadouts.get(e));
let change =
damage.modify_damage(false, other_entity_loadout, projectile.owner);
if change.amount != 0 {
server_emitter.emit(ServerEvent::Damage { uid: other, change });
}
},
projectile::Effect::Knockback(knockback) => {
if let Some(entity) =
uid_allocator.retrieve_entity_internal(other.into())
{
2020-10-27 22:16:17 +00:00
let impulse = knockback.calculate_impulse(ori.0);
if !impulse.is_approx_zero() {
local_emitter
.emit(LocalEvent::ApplyImpulse { entity, impulse });
}
}
},
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);
}
},
projectile::Effect::Explode(e) => {
server_emitter.emit(ServerEvent::Explosion {
pos: pos.0,
explosion: e,
owner: projectile.owner,
reagent: None,
})
},
2019-10-11 04:30:34 +00:00
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
entity,
cause: HealthSource::World,
}),
projectile::Effect::Possess => {
if other != projectile.owner.unwrap() {
if let Some(owner) = projectile.owner {
server_emitter.emit(ServerEvent::Possess(owner, other));
}
}
},
2020-11-01 18:38:57 +00:00
projectile::Effect::Buff { buff, chance } => {
if let Some(entity) =
uid_allocator.retrieve_entity_internal(other.into())
{
if chance.map_or(true, |c| thread_rng().gen::<f32>() < c) {
let mut buff = buff.clone();
if let Some(uid) = projectile.owner {
buff.source = BuffSource::Character { by: uid };
}
server_emitter.emit(ServerEvent::Buff {
entity,
buff_change: BuffChange::Add(buff),
});
}
}
},
_ => {},
}
}
}
// Hit something solid
if physics.on_wall.is_some() || physics.on_ground || physics.on_ceiling {
for effect in projectile.hit_solid.drain(..) {
match effect {
projectile::Effect::Explode(e) => {
server_emitter.emit(ServerEvent::Explosion {
pos: pos.0,
explosion: e,
owner: projectile.owner,
reagent: None,
})
},
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
entity,
cause: HealthSource::World,
}),
_ => {},
2019-09-17 12:43:19 +00:00
}
}
} else if let Some(dir) = velocities
.get(entity)
.and_then(|vel| vel.0.try_normalized())
{
ori.0 = dir.into();
}
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
}
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
}
}