veloren/common/src/sys/combat.rs

129 lines
3.9 KiB
Rust
Raw Normal View History

use crate::{
comp::{
2020-03-07 21:03:10 +00:00
Attacking, Body, CharacterState, Controller, HealthChange, HealthSource, Ori, Pos, Scale,
Stats,
},
2019-09-25 21:31:25 +00:00
event::{EventBus, LocalEvent, ServerEvent},
2019-11-24 20:12:03 +00:00
state::DeltaTime,
sync::Uid,
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
2020-03-07 21:03:10 +00:00
// use std::time::Duration;
2019-08-24 20:41:34 +00:00
use vek::*;
const BLOCK_EFFICIENCY: f32 = 0.9;
const ATTACK_RANGE: f32 = 3.5;
const ATTACK_ANGLE: f32 = 45.0;
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 {
type SystemData = (
Entities<'a>,
2019-09-25 21:31:25 +00:00
Read<'a, EventBus<ServerEvent>>,
Read<'a, EventBus<LocalEvent>>,
Read<'a, DeltaTime>,
2019-09-25 21:31:25 +00:00
ReadStorage<'a, Uid>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Ori>,
ReadStorage<'a, Scale>,
2019-08-24 19:12:54 +00:00
ReadStorage<'a, Controller>,
ReadStorage<'a, Body>,
ReadStorage<'a, Stats>,
2020-02-11 15:42:17 +00:00
WriteStorage<'a, Attacking>,
WriteStorage<'a, CharacterState>,
);
fn run(
&mut self,
(
entities,
2019-09-25 21:31:25 +00:00
server_bus,
2020-03-07 21:03:10 +00:00
_local_bus,
_dt,
2019-09-25 21:31:25 +00:00
uids,
positions,
orientations,
scales,
2019-08-24 19:12:54 +00:00
controllers,
bodies,
2019-09-26 16:48:37 +00:00
stats,
2020-02-11 15:42:17 +00:00
mut attacking_storage,
2020-03-07 21:03:10 +00:00
character_states,
): Self::SystemData,
) {
// Attacks
2020-03-07 21:03:10 +00:00
for (entity, uid, pos, ori, scale_maybe, _, _attacker_stats, attack) in (
&entities,
&uids,
&positions,
&orientations,
scales.maybe(),
&controllers,
&stats,
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 (
&entities,
&uids,
&positions,
&orientations,
scales.maybe(),
&character_states,
&stats,
&bodies,
)
.join()
{
// 2D versions
let pos2 = Vec2::from(pos.0);
let pos_b2: Vec2<f32> = Vec2::from(pos_b.0);
let ori2 = Vec2::from(ori.0);
2019-08-24 19:12:54 +00:00
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-02-24 13:32:12 +00:00
// Check if it is a hit
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_ANGLE.to_radians() / 2.0 + (rad_b / pos2.distance(pos_b2)).atan()
{
// Weapon gives base damage
let mut dmg = attack.base_damage;
2020-02-24 13:32:12 +00:00
// Block
if character_b.is_block()
&& ori_b.0.angle_between(pos.0 - pos_b.0) < BLOCK_ANGLE.to_radians() / 2.0
2020-02-11 15:42:17 +00:00
{
dmg = (dmg as f32 * (1.0 - BLOCK_EFFICIENCY)) as u32
2020-02-11 15:42:17 +00:00
}
2020-02-24 13:32:12 +00:00
server_bus.emitter().emit(ServerEvent::Damage {
uid: *uid_b,
change: HealthChange {
amount: -(dmg as i32),
2020-02-24 13:32:12 +00:00
cause: HealthSource::Attack { by: *uid },
},
});
attack.hit_count += 1;
2020-02-11 15:42:17 +00:00
}
}
}
}
}