2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-02-02 18:02:40 +00:00
|
|
|
combat::AttackerInfo,
|
|
|
|
comp::{group, Body, CharacterState, Energy, Health, Inventory, Melee, Ori, Pos, Scale},
|
2020-03-25 14:24:55 +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,
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::Uid,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2020-11-02 00:26:01 +00:00
|
|
|
GroupTarget,
|
2019-06-11 19:28:25 +00:00
|
|
|
};
|
2020-09-14 12:56:05 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
|
2019-08-24 20:41:34 +00:00
|
|
|
use vek::*;
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// This system is responsible for handling accepted inputs like moving or
|
|
|
|
/// attacking
|
2019-06-11 19:28:25 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-06-11 19:28:25 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-09-25 21:31:25 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2020-03-25 14:24:55 +00:00
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2020-09-14 12:56:05 +00:00
|
|
|
ReadExpect<'a, SysMetrics>,
|
2019-09-25 21:31:25 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-06-11 19:28:25 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Ori>,
|
2019-12-01 23:40:05 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
2019-12-20 03:31:41 +00:00
|
|
|
ReadStorage<'a, Body>,
|
2020-10-31 22:34:08 +00:00
|
|
|
ReadStorage<'a, Health>,
|
2021-01-30 05:17:40 +00:00
|
|
|
ReadStorage<'a, Energy>,
|
2021-01-08 19:12:09 +00:00
|
|
|
ReadStorage<'a, Inventory>,
|
2020-04-26 17:03:19 +00:00
|
|
|
ReadStorage<'a, group::Group>,
|
2021-02-02 18:02:40 +00:00
|
|
|
WriteStorage<'a, Melee>,
|
2020-11-05 22:48:04 +00:00
|
|
|
ReadStorage<'a, CharacterState>,
|
2019-06-11 19:28:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-09-25 21:31:25 +00:00
|
|
|
server_bus,
|
2020-03-25 14:24:55 +00:00
|
|
|
local_bus,
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics,
|
2019-09-25 21:31:25 +00:00
|
|
|
uids,
|
2019-06-11 19:28:25 +00:00
|
|
|
positions,
|
|
|
|
orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales,
|
2019-12-20 03:31:41 +00:00
|
|
|
bodies,
|
2020-10-31 22:34:08 +00:00
|
|
|
healths,
|
2021-01-30 05:17:40 +00:00
|
|
|
energies,
|
2021-01-08 19:12:09 +00:00
|
|
|
inventories,
|
2020-04-26 17:03:19 +00:00
|
|
|
groups,
|
2020-08-08 22:22:21 +00:00
|
|
|
mut attacking_storage,
|
2020-11-05 22:48:04 +00:00
|
|
|
char_states,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-10-17 22:42:43 +00:00
|
|
|
span!(_guard, "run", "melee::Sys::run");
|
2020-03-22 04:49:32 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
2020-11-16 23:32:23 +00:00
|
|
|
let _local_emitter = local_bus.emitter();
|
2019-06-11 19:28:25 +00:00
|
|
|
// Attacks
|
2021-01-24 04:00:57 +00:00
|
|
|
for (entity, uid, pos, ori, scale_maybe, attack, body) in (
|
2019-09-29 11:41:04 +00:00
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales.maybe(),
|
2020-02-11 15:42:17 +00:00
|
|
|
&mut attacking_storage,
|
2021-01-24 04:00:57 +00:00
|
|
|
&bodies,
|
2019-09-29 11:41:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-08-24 19:12:54 +00:00
|
|
|
{
|
2020-03-10 17:54:59 +00:00
|
|
|
if attack.applied {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attack.applied = true;
|
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Go through all other entities
|
2021-01-26 03:53:52 +00:00
|
|
|
for (
|
|
|
|
b,
|
|
|
|
pos_b,
|
|
|
|
scale_b_maybe,
|
|
|
|
health_b,
|
|
|
|
body_b,
|
|
|
|
char_state_b_maybe,
|
|
|
|
inventory_b_maybe,
|
|
|
|
) in (
|
2020-11-05 22:48:04 +00:00
|
|
|
&entities,
|
|
|
|
&positions,
|
|
|
|
scales.maybe(),
|
|
|
|
&healths,
|
|
|
|
&bodies,
|
|
|
|
char_states.maybe(),
|
2021-01-26 02:50:16 +00:00
|
|
|
inventories.maybe(),
|
2020-11-05 22:48:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2020-09-05 23:45:36 +00:00
|
|
|
{
|
2020-09-05 16:41:11 +00:00
|
|
|
// 2D versions
|
|
|
|
let pos2 = Vec2::from(pos.0);
|
|
|
|
let pos_b2 = Vec2::<f32>::from(pos_b.0);
|
|
|
|
let ori2 = Vec2::from(*ori.0);
|
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Scales
|
|
|
|
let scale = scale_maybe.map_or(1.0, |s| s.0);
|
|
|
|
let scale_b = scale_b_maybe.map_or(1.0, |s| s.0);
|
2021-01-24 04:00:57 +00:00
|
|
|
let rad = body.radius() * scale;
|
2020-02-24 13:32:12 +00:00
|
|
|
let rad_b = body_b.radius() * scale_b;
|
2019-08-24 19:12:54 +00:00
|
|
|
|
2020-11-06 01:22:26 +00:00
|
|
|
// Check if entity is dodging
|
2020-12-31 18:37:25 +00:00
|
|
|
let is_dodge = char_state_b_maybe.map_or(false, |c_s| c_s.is_melee_dodge());
|
2020-11-05 22:48:04 +00:00
|
|
|
|
2020-09-30 00:47:11 +00:00
|
|
|
// Check if it is a hit
|
2020-02-24 13:32:12 +00:00
|
|
|
if entity != b
|
2020-10-31 22:34:08 +00:00
|
|
|
&& !health_b.is_dead
|
2020-09-05 16:41:11 +00:00
|
|
|
// Spherical wedge shaped attack field
|
2021-01-24 04:00:57 +00:00
|
|
|
&& pos.0.distance_squared(pos_b.0) < (rad + rad_b + scale * attack.range).powi(2)
|
2020-09-05 16:41:11 +00:00
|
|
|
&& ori2.angle_between(pos_b2 - pos2) < attack.max_angle + (rad_b / pos2.distance(pos_b2)).atan()
|
2020-02-24 13:32:12 +00:00
|
|
|
{
|
2020-04-26 17:03:19 +00:00
|
|
|
// See if entities are in the same group
|
|
|
|
let same_group = groups
|
|
|
|
.get(entity)
|
|
|
|
.map(|group_a| Some(group_a) == groups.get(b))
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
2020-11-02 00:26:01 +00:00
|
|
|
let target_group = if same_group {
|
|
|
|
GroupTarget::InGroup
|
|
|
|
} else {
|
|
|
|
GroupTarget::OutOfGroup
|
|
|
|
};
|
|
|
|
|
2021-01-26 02:50:16 +00:00
|
|
|
let dir = Dir::new((pos_b.0 - pos.0).try_normalized().unwrap_or(*ori.0));
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
let attacker_info = Some(AttackerInfo {
|
|
|
|
entity,
|
|
|
|
uid: *uid,
|
|
|
|
energy: energies.get(entity),
|
|
|
|
});
|
|
|
|
|
|
|
|
attack.attack.apply_attack(
|
2021-01-26 17:58:52 +00:00
|
|
|
target_group,
|
2021-02-02 18:02:40 +00:00
|
|
|
attacker_info,
|
2021-01-26 17:58:52 +00:00
|
|
|
b,
|
|
|
|
inventory_b_maybe,
|
|
|
|
dir,
|
2021-01-28 01:54:45 +00:00
|
|
|
is_dodge,
|
2021-01-31 16:40:04 +00:00
|
|
|
1.0,
|
2021-02-02 18:02:40 +00:00
|
|
|
|e| server_emitter.emit(e),
|
2021-01-26 17:58:52 +00:00
|
|
|
);
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
attack.hit_count += 1;
|
2020-02-11 15:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
sys_metrics.melee_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-06-11 19:28:25 +00:00
|
|
|
}
|
2020-09-05 23:45:36 +00:00
|
|
|
}
|