2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-02-02 18:02:40 +00:00
|
|
|
combat::AttackerInfo,
|
2021-02-22 18:25:21 +00:00
|
|
|
comp::{Body, CharacterState, Energy, Group, Health, Inventory, Melee, Ori, Pos, Scale},
|
|
|
|
event::{EventBus, 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
|
|
|
};
|
2021-02-22 18:25:21 +00:00
|
|
|
use specs::{
|
|
|
|
shred::ResourceId, Entities, Join, Read, ReadExpect, ReadStorage, System, SystemData, World,
|
|
|
|
WriteStorage,
|
|
|
|
};
|
2019-08-24 20:41:34 +00:00
|
|
|
use vek::*;
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2021-02-22 18:25:21 +00:00
|
|
|
#[derive(SystemData)]
|
2021-02-22 21:02:37 +00:00
|
|
|
pub struct ReadData<'a> {
|
2021-02-22 18:25:21 +00:00
|
|
|
entities: Entities<'a>,
|
|
|
|
uids: ReadStorage<'a, Uid>,
|
|
|
|
positions: ReadStorage<'a, Pos>,
|
|
|
|
orientations: ReadStorage<'a, Ori>,
|
|
|
|
scales: ReadStorage<'a, Scale>,
|
|
|
|
bodies: ReadStorage<'a, Body>,
|
|
|
|
healths: ReadStorage<'a, Health>,
|
|
|
|
energies: ReadStorage<'a, Energy>,
|
|
|
|
inventories: ReadStorage<'a, Inventory>,
|
|
|
|
groups: ReadStorage<'a, Group>,
|
|
|
|
char_states: ReadStorage<'a, CharacterState>,
|
|
|
|
server_bus: Read<'a, EventBus<ServerEvent>>,
|
|
|
|
metrics: ReadExpect<'a, SysMetrics>,
|
|
|
|
}
|
|
|
|
|
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;
|
2021-02-22 18:25:21 +00:00
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2021-02-22 21:02:37 +00:00
|
|
|
type SystemData = (ReadData<'a>, WriteStorage<'a, Melee>);
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2021-02-22 21:02:37 +00:00
|
|
|
fn run(&mut self, (read_data, mut melee_attacks): 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");
|
2021-02-22 21:02:37 +00:00
|
|
|
let mut server_emitter = read_data.server_bus.emitter();
|
2019-06-11 19:28:25 +00:00
|
|
|
// Attacks
|
2021-02-22 18:25:21 +00:00
|
|
|
for (attacker, uid, pos, ori, melee_attack, body) in (
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.entities,
|
|
|
|
&read_data.uids,
|
|
|
|
&read_data.positions,
|
|
|
|
&read_data.orientations,
|
2021-02-22 18:25:21 +00:00
|
|
|
&mut melee_attacks,
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.bodies,
|
2019-09-29 11:41:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-08-24 19:12:54 +00:00
|
|
|
{
|
2021-02-22 18:25:21 +00:00
|
|
|
if melee_attack.applied {
|
2020-03-10 17:54:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-22 18:25:21 +00:00
|
|
|
melee_attack.applied = true;
|
2020-03-10 17:54:59 +00:00
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Go through all other entities
|
2021-02-22 18:25:21 +00:00
|
|
|
for (target, pos_b, health_b, body_b) in (
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.entities,
|
|
|
|
&read_data.positions,
|
|
|
|
&read_data.healths,
|
|
|
|
&read_data.bodies,
|
2020-11-05 22:48:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2020-09-05 23:45:36 +00:00
|
|
|
{
|
2021-02-04 09:17:38 +00:00
|
|
|
let look_dir = *ori.look_dir();
|
|
|
|
|
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);
|
2021-02-04 09:17:38 +00:00
|
|
|
let ori2 = Vec2::from(look_dir);
|
2020-09-05 16:41:11 +00:00
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Scales
|
2021-02-22 21:02:37 +00:00
|
|
|
let scale = read_data.scales.get(attacker).map_or(1.0, |s| s.0);
|
|
|
|
let scale_b = read_data.scales.get(target).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
|
2021-02-22 21:02:37 +00:00
|
|
|
let is_dodge = read_data
|
2021-02-22 18:25:21 +00:00
|
|
|
.char_states
|
|
|
|
.get(target)
|
|
|
|
.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
|
2021-02-22 18:25:21 +00:00
|
|
|
if attacker != target
|
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-02-22 18:25:21 +00:00
|
|
|
&& pos.0.distance_squared(pos_b.0) < (rad + rad_b + scale * melee_attack.range).powi(2)
|
|
|
|
&& ori2.angle_between(pos_b2 - pos2) < melee_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
|
2021-02-22 21:02:37 +00:00
|
|
|
let same_group = read_data
|
2021-02-22 18:25:21 +00:00
|
|
|
.groups
|
|
|
|
.get(attacker)
|
2021-02-22 21:02:37 +00:00
|
|
|
.map(|group_a| Some(group_a) == read_data.groups.get(target))
|
2020-04-26 17:03:19 +00:00
|
|
|
.unwrap_or(false);
|
|
|
|
|
2020-11-02 00:26:01 +00:00
|
|
|
let target_group = if same_group {
|
|
|
|
GroupTarget::InGroup
|
|
|
|
} else {
|
|
|
|
GroupTarget::OutOfGroup
|
|
|
|
};
|
|
|
|
|
2021-02-04 09:17:38 +00:00
|
|
|
let dir = Dir::new((pos_b.0 - pos.0).try_normalized().unwrap_or(look_dir));
|
2021-01-26 02:50:16 +00:00
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
let attacker_info = Some(AttackerInfo {
|
2021-02-22 18:25:21 +00:00
|
|
|
entity: attacker,
|
2021-02-02 18:02:40 +00:00
|
|
|
uid: *uid,
|
2021-02-22 21:02:37 +00:00
|
|
|
energy: read_data.energies.get(attacker),
|
2021-02-02 18:02:40 +00:00
|
|
|
});
|
|
|
|
|
2021-02-22 18:25:21 +00:00
|
|
|
melee_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-02-22 18:25:21 +00:00
|
|
|
target,
|
2021-02-22 21:02:37 +00:00
|
|
|
read_data.inventories.get(target),
|
2021-01-26 17:58:52 +00:00
|
|
|
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-22 18:25:21 +00:00
|
|
|
melee_attack.hit_count += 1;
|
2020-02-11 15:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 21:02:37 +00:00
|
|
|
read_data.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
|
|
|
}
|