veloren/common/systems/src/melee.rs

172 lines
5.9 KiB
Rust
Raw Normal View History

use common::{
2021-04-10 03:40:20 +00:00
combat::{AttackSource, AttackerInfo, TargetInfo},
comp::{
Body, CharacterState, Combo, Energy, Group, Health, Inventory, Melee, Ori, Pos, Scale,
Stats,
},
event::{EventBus, ServerEvent},
2021-04-04 03:04:02 +00:00
outcome::Outcome,
uid::Uid,
util::Dir,
GroupTarget,
};
use common_ecs::{Job, Origin, Phase, System};
use specs::{
2021-04-04 03:04:02 +00:00
shred::ResourceId, Entities, Join, Read, ReadStorage, SystemData, World, Write, WriteStorage,
};
2019-08-24 20:41:34 +00:00
use vek::*;
#[derive(SystemData)]
pub struct ReadData<'a> {
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>>,
stats: ReadStorage<'a, Stats>,
combos: ReadStorage<'a, Combo>,
}
/// This system is responsible for handling accepted inputs like moving or
/// attacking
#[derive(Default)]
pub struct Sys;
2021-03-08 11:13:59 +00:00
impl<'a> System<'a> for Sys {
2021-04-04 03:04:02 +00:00
type SystemData = (
ReadData<'a>,
WriteStorage<'a, Melee>,
Write<'a, Vec<Outcome>>,
);
const NAME: &'static str = "melee";
const ORIGIN: Origin = Origin::Common;
const PHASE: Phase = Phase::Create;
2021-04-04 03:04:02 +00:00
fn run(_job: &mut Job<Self>, (read_data, mut melee_attacks, mut outcomes): Self::SystemData) {
let mut server_emitter = read_data.server_bus.emitter();
// Attacks
for (attacker, uid, pos, ori, melee_attack, body) in (
&read_data.entities,
&read_data.uids,
&read_data.positions,
&read_data.orientations,
&mut melee_attacks,
&read_data.bodies,
)
.join()
2019-08-24 19:12:54 +00:00
{
if melee_attack.applied {
continue;
}
melee_attack.applied = true;
2021-03-21 16:09:16 +00:00
// Scales
let eye_pos = pos.0 + Vec3::unit_z() * body.eye_height();
let scale = read_data.scales.get(attacker).map_or(1.0, |s| s.0);
let rad = body.radius() * scale;
// Mine blocks broken by the attack
2021-03-21 17:45:01 +00:00
if let Some((block_pos, tool)) = melee_attack.break_block {
2021-03-21 16:09:16 +00:00
// Check distance to block
if eye_pos.distance_squared(block_pos.map(|e| e as f32 + 0.5))
< (rad + scale * melee_attack.range).powi(2)
{
2021-03-21 17:45:01 +00:00
server_emitter.emit(ServerEvent::MineBlock {
pos: block_pos,
tool,
});
2021-03-21 16:09:16 +00:00
}
}
2020-02-24 13:32:12 +00:00
// Go through all other entities
for (target, pos_b, health_b, body_b) in (
&read_data.entities,
&read_data.positions,
&read_data.healths,
&read_data.bodies,
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_b = read_data.scales.get(target).map_or(1.0, |s| s.0);
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 = read_data
.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
if attacker != target
&& !health_b.is_dead
// Spherical wedge shaped attack field
&& 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
{
// See if entities are in the same group
let same_group = read_data
.groups
.get(attacker)
.map(|group_a| Some(group_a) == read_data.groups.get(target))
.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: attacker,
2021-02-02 18:02:40 +00:00
uid: *uid,
energy: read_data.energies.get(attacker),
combo: read_data.combos.get(attacker),
2021-02-02 18:02:40 +00:00
});
let target_info = TargetInfo {
entity: target,
inventory: read_data.inventories.get(target),
stats: read_data.stats.get(target),
health: read_data.healths.get(target),
pos: pos_b.0,
2021-04-10 03:40:20 +00:00
ori: read_data.orientations.get(target),
char_state: read_data.char_states.get(target),
};
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,
target_info,
2021-01-26 17:58:52 +00:00
dir,
is_dodge,
1.0,
2021-04-10 03:40:20 +00:00
AttackSource::Melee,
2021-02-02 18:02:40 +00:00
|e| server_emitter.emit(e),
2021-04-04 03:04:02 +00:00
|o| outcomes.push(o),
2021-01-26 17:58:52 +00:00
);
melee_attack.hit_count += 1;
2020-02-11 15:42:17 +00:00
}
}
}
}
}