veloren/common/sys/src/melee.rs

162 lines
4.9 KiB
Rust
Raw Normal View History

use common::{
2021-02-02 18:02:40 +00:00
combat::AttackerInfo,
comp::{group, Body, CharacterState, Energy, Health, Inventory, Melee, Ori, Pos, Scale},
event::{EventBus, LocalEvent, ServerEvent},
metrics::SysMetrics,
span,
uid::Uid,
util::Dir,
GroupTarget,
};
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
2019-08-24 20:41:34 +00:00
use vek::*;
/// This system is responsible for handling accepted inputs like moving or
/// attacking
pub struct Sys;
impl<'a> System<'a> for Sys {
#[allow(clippy::type_complexity)]
type SystemData = (
Entities<'a>,
2019-09-25 21:31:25 +00:00
Read<'a, EventBus<ServerEvent>>,
Read<'a, EventBus<LocalEvent>>,
ReadExpect<'a, SysMetrics>,
2019-09-25 21:31:25 +00:00
ReadStorage<'a, Uid>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Ori>,
ReadStorage<'a, Scale>,
ReadStorage<'a, Body>,
ReadStorage<'a, Health>,
ReadStorage<'a, Energy>,
ReadStorage<'a, Inventory>,
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>,
);
fn run(
&mut self,
(
entities,
2019-09-25 21:31:25 +00:00
server_bus,
local_bus,
sys_metrics,
2019-09-25 21:31:25 +00:00
uids,
positions,
orientations,
scales,
bodies,
healths,
energies,
inventories,
groups,
mut attacking_storage,
2020-11-05 22:48:04 +00:00
char_states,
): Self::SystemData,
) {
let start_time = std::time::Instant::now();
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();
// Attacks
2021-01-24 04:00:57 +00:00
for (entity, uid, pos, ori, scale_maybe, attack, body) in (
&entities,
&uids,
&positions,
&orientations,
scales.maybe(),
2020-02-11 15:42:17 +00:00
&mut attacking_storage,
2021-01-24 04:00:57 +00:00
&bodies,
)
.join()
2019-08-24 19:12:54 +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(),
inventories.maybe(),
2020-11-05 22:48:04 +00:00
)
.join()
{
2021-02-04 09:17:38 +00:00
let look_dir = *ori.look_dir();
// 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-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
// Check if entity is dodging
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
&& !health_b.is_dead
// 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)
&& ori2.angle_between(pos_b2 - pos2) < attack.max_angle + (rad_b / pos2.distance(pos_b2)).atan()
2020-02-24 13:32:12 +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);
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-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,
is_dodge,
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
}
}
}
sys_metrics.melee_ns.store(
start_time.elapsed().as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed,
);
}
}