veloren/common/src/sys/melee.rs

170 lines
6.1 KiB
Rust
Raw Normal View History

use crate::{
comp::{buff, group, Attacking, Body, CharacterState, Loadout, Ori, Pos, Scale, Stats},
event::{EventBus, LocalEvent, ServerEvent},
metrics::SysMetrics,
span,
2019-11-24 20:12:03 +00:00
sync::Uid,
util::Dir,
};
use rand::{thread_rng, Rng};
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
use std::time::Duration;
2019-08-24 20:41:34 +00:00
use vek::*;
2020-04-01 14:07:10 +00:00
pub const BLOCK_EFFICIENCY: f32 = 0.9;
pub const BLOCK_ANGLE: f32 = 180.0;
/// 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, Stats>,
ReadStorage<'a, Loadout>,
ReadStorage<'a, group::Group>,
ReadStorage<'a, CharacterState>,
2020-02-11 15:42:17 +00:00
WriteStorage<'a, Attacking>,
);
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,
2019-09-26 16:48:37 +00:00
stats,
loadouts,
groups,
2020-03-07 21:03:10 +00:00
character_states,
mut attacking_storage,
): 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();
let mut _local_emitter = local_bus.emitter();
// Attacks
for (entity, uid, pos, ori, scale_maybe, attack) in (
&entities,
&uids,
&positions,
&orientations,
scales.maybe(),
2020-02-11 15:42:17 +00:00
&mut attacking_storage,
)
.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
for (b, uid_b, pos_b, ori_b, scale_b_maybe, character_b, stats_b, body_b) in (
2020-02-24 13:32:12 +00:00
&entities,
&uids,
&positions,
&orientations,
scales.maybe(),
2020-07-05 12:39:28 +00:00
character_states.maybe(),
2020-02-24 13:32:12 +00:00
&stats,
&bodies,
)
.join()
{
// 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);
let rad_b = body_b.radius() * scale_b;
2019-08-24 19:12:54 +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
&& !stats_b.is_dead
// Spherical wedge shaped attack field
&& pos.0.distance_squared(pos_b.0) < (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 damage = if !same_group && attack.damages.enemy.is_some() {
attack.damages.enemy.unwrap()
} else if same_group && attack.damages.group.is_some() {
attack.damages.group.unwrap()
} else {
continue;
2020-07-25 23:57:04 +00:00
};
2020-07-25 23:57:04 +00:00
let block = character_b.map(|c_b| c_b.is_block()).unwrap_or(false)
&& ori_b.0.angle_between(pos.0 - pos_b.0) < BLOCK_ANGLE.to_radians() / 2.0;
let change = damage.modify_damage(block, loadouts.get(b), Some(*uid));
if change.amount != 0 {
server_emitter.emit(ServerEvent::Damage {
uid: *uid_b,
change,
});
2020-10-24 23:07:38 +00:00
// Apply bleeding buff on melee hits with 10% chance
// TODO: Don't have buff uniformly applied on all melee attacks
if change.amount < 0 && thread_rng().gen::<f32>() < 0.1 {
use buff::*;
server_emitter.emit(ServerEvent::Buff {
entity: b,
2020-10-24 23:07:38 +00:00
buff_change: BuffChange::Add(Buff::new(
BuffKind::Bleeding,
BuffData {
strength: -change.amount as f32 / 10.0,
duration: Some(Duration::from_secs(10)),
},
2020-10-24 23:07:38 +00:00
vec![BuffCategory::Physical],
BuffSource::Character { by: *uid },
)),
});
}
attack.hit_count += 1;
2020-07-07 01:21:14 +00:00
}
if attack.knockback != 0.0 && change.amount != 0 {
2020-09-13 01:33:46 +00:00
let kb_dir = Dir::new((pos_b.0 - pos.0).try_normalized().unwrap_or(*ori.0));
server_emitter.emit(ServerEvent::Knockback {
entity: b,
2020-09-19 16:55:31 +00:00
impulse: attack.knockback
2020-09-13 01:33:46 +00:00
* *Dir::slerp(kb_dir, Dir::new(Vec3::new(0.0, 0.0, 1.0)), 0.5),
});
}
2020-02-11 15:42:17 +00:00
}
}
}
sys_metrics.melee_ns.store(
start_time.elapsed().as_nanos() as i64,
std::sync::atomic::Ordering::Relaxed,
);
}
}