2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-01-30 05:17:40 +00:00
|
|
|
comp::{
|
|
|
|
projectile, Energy, Group, HealthSource, Inventory, Ori, PhysicsState, Pos, Projectile, Vel,
|
|
|
|
},
|
2020-11-07 18:00:07 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-09-14 12:56:05 +00:00
|
|
|
metrics::SysMetrics,
|
2020-12-01 00:28:00 +00:00
|
|
|
resources::DeltaTime,
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::UidAllocator,
|
2020-11-02 00:26:01 +00:00
|
|
|
GroupTarget,
|
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;
|
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>,
|
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>,
|
2021-01-08 19:12:09 +00:00
|
|
|
ReadStorage<'a, Inventory>,
|
2020-09-19 23:35:14 +00:00
|
|
|
ReadStorage<'a, Group>,
|
2021-01-30 05:17:40 +00:00
|
|
|
ReadStorage<'a, Energy>,
|
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,
|
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,
|
2021-01-08 19:12:09 +00:00
|
|
|
inventories,
|
2020-09-19 23:35:14 +00:00
|
|
|
groups,
|
2021-01-30 05:17:40 +00:00
|
|
|
energies,
|
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");
|
2019-09-17 12:43:19 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
|
|
|
|
// Attacks
|
2021-02-01 15:41:34 +00:00
|
|
|
'projectile_loop: for (entity, pos, physics, ori, mut 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()
|
|
|
|
{
|
2021-02-01 15:41:34 +00:00
|
|
|
let mut projectile_vanished: bool = false;
|
2020-08-24 23:04:04 +00:00
|
|
|
// Hit entity
|
|
|
|
for other in physics.touch_entities.iter().copied() {
|
2020-10-17 22:42:43 +00:00
|
|
|
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))
|
|
|
|
);
|
2020-11-02 00:26:01 +00:00
|
|
|
|
|
|
|
let target_group = if same_group {
|
|
|
|
GroupTarget::InGroup
|
|
|
|
} else {
|
|
|
|
GroupTarget::OutOfGroup
|
|
|
|
};
|
|
|
|
|
2020-09-19 23:35:14 +00:00
|
|
|
if projectile.ignore_group
|
|
|
|
// Skip if in the same group
|
2020-10-17 22:42:43 +00:00
|
|
|
&& same_group
|
2020-09-19 23:35:14 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:04:04 +00:00
|
|
|
if projectile.owner == Some(other) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:25:12 +00:00
|
|
|
let projectile = &mut *projectile;
|
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 {
|
2021-01-29 00:04:44 +00:00
|
|
|
projectile::Effect::Attack(attack) => {
|
|
|
|
if let Some(owner) = projectile.owner {
|
|
|
|
if let (Some(owner_entity), Some(target_entity)) = (
|
|
|
|
uid_allocator.retrieve_entity_internal(owner.into()),
|
|
|
|
uid_allocator.retrieve_entity_internal(other.into()),
|
|
|
|
) {
|
|
|
|
let server_events = attack.apply_attack(
|
|
|
|
target_group,
|
|
|
|
owner_entity,
|
|
|
|
target_entity,
|
|
|
|
inventories.get(target_entity),
|
|
|
|
owner,
|
2021-01-30 05:17:40 +00:00
|
|
|
energies.get(owner_entity),
|
2021-01-29 00:04:44 +00:00
|
|
|
ori.0,
|
|
|
|
false,
|
2021-01-31 16:40:04 +00:00
|
|
|
1.0,
|
2021-01-29 00:04:44 +00:00
|
|
|
);
|
2020-11-04 02:01:12 +00:00
|
|
|
|
2021-01-29 00:04:44 +00:00
|
|
|
for event in server_events {
|
|
|
|
server_emitter.emit(event);
|
|
|
|
}
|
2020-10-27 22:16:17 +00:00
|
|
|
}
|
2020-08-24 23:04:04 +00:00
|
|
|
}
|
|
|
|
},
|
2021-01-29 00:04:44 +00:00
|
|
|
// projectile::Effect::Knockback(knockback) => {
|
|
|
|
// if let Some(other_entity) =
|
|
|
|
// uid_allocator.retrieve_entity_internal(other.into())
|
|
|
|
// {
|
|
|
|
// let impulse = knockback.calculate_impulse(ori.0);
|
|
|
|
// if !impulse.is_approx_zero() {
|
|
|
|
// server_emitter.emit(ServerEvent::Knockback {
|
|
|
|
// entity: other_entity,
|
|
|
|
// impulse,
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// projectile::Effect::RewardEnergy(energy) => {
|
|
|
|
// if let Some(entity_owner) = projectile
|
|
|
|
// .owner
|
|
|
|
// .and_then(|u| uid_allocator.retrieve_entity_internal(u.into()))
|
|
|
|
// {
|
|
|
|
// server_emitter.emit(ServerEvent::EnergyChange {
|
|
|
|
// entity: entity_owner,
|
|
|
|
// change: EnergyChange {
|
|
|
|
// amount: energy as i32,
|
|
|
|
// source: EnergySource::HitEnemy,
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// },
|
2020-10-06 02:19:41 +00:00
|
|
|
projectile::Effect::Explode(e) => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
explosion: e,
|
|
|
|
owner: projectile.owner,
|
|
|
|
reagent: None,
|
|
|
|
})
|
|
|
|
},
|
2021-02-01 15:41:34 +00:00
|
|
|
projectile::Effect::Vanish => {
|
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
});
|
|
|
|
projectile_vanished = true;
|
|
|
|
},
|
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-11-04 02:01:12 +00:00
|
|
|
// TODO: Change to effect after !1472 merges
|
2021-01-29 00:04:44 +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 source = if let Some(owner) = projectile.owner {
|
|
|
|
// BuffSource::Character { by: owner }
|
|
|
|
// } else {
|
|
|
|
// BuffSource::Unknown
|
|
|
|
// };
|
|
|
|
// let buff =
|
|
|
|
// Buff::new(buff.kind, buff.data, buff.cat_ids, source);
|
|
|
|
// server_emitter.emit(ServerEvent::Buff {
|
|
|
|
// entity,
|
|
|
|
// buff_change: BuffChange::Add(buff),
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// },
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-10-06 17:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 15:41:34 +00:00
|
|
|
|
|
|
|
if projectile_vanished {
|
|
|
|
continue 'projectile_loop;
|
|
|
|
}
|
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 {
|
2021-01-07 20:25:12 +00:00
|
|
|
let projectile = &mut *projectile;
|
2020-08-24 23:04:04 +00:00
|
|
|
for effect in projectile.hit_solid.drain(..) {
|
|
|
|
match effect {
|
2020-10-06 02:19:41 +00:00
|
|
|
projectile::Effect::Explode(e) => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
explosion: e,
|
|
|
|
owner: projectile.owner,
|
|
|
|
reagent: None,
|
|
|
|
})
|
|
|
|
},
|
2021-02-01 15:41:34 +00:00
|
|
|
projectile::Effect::Vanish => {
|
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
});
|
|
|
|
projectile_vanished = true;
|
|
|
|
},
|
2020-08-24 23:04:04 +00:00
|
|
|
_ => {},
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 15:41:34 +00:00
|
|
|
|
|
|
|
if projectile_vanished {
|
|
|
|
continue 'projectile_loop;
|
|
|
|
}
|
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(
|
2020-12-15 23:51:07 +00:00
|
|
|
start_time.elapsed().as_nanos() as u64,
|
2020-09-14 12:56:05 +00:00
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|