2019-06-11 19:28:25 +00:00
|
|
|
use crate::{
|
2019-10-17 20:59:36 +00:00
|
|
|
comp::{
|
2020-10-01 01:35:57 +00:00
|
|
|
buff, group, Attacking, Body, CharacterState, Damage, DamageSource, HealthChange,
|
|
|
|
HealthSource, Loadout, Ori, Pos, Scale, Stats,
|
2019-10-17 20:59:36 +00:00
|
|
|
},
|
2020-03-25 14:24:55 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2020-09-14 12:56:05 +00:00
|
|
|
metrics::SysMetrics,
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2019-11-24 20:12:03 +00:00
|
|
|
sync::Uid,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2019-06-11 19:28:25 +00:00
|
|
|
};
|
2020-09-14 12:56:05 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
|
2020-10-01 00:40:46 +00:00
|
|
|
use std::time::Duration;
|
2019-08-24 20:41:34 +00:00
|
|
|
use vek::*;
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2020-04-01 14:07:10 +00:00
|
|
|
pub const BLOCK_EFFICIENCY: f32 = 0.9;
|
|
|
|
pub const BLOCK_ANGLE: f32 = 180.0;
|
2019-08-25 19:09:10 +00:00
|
|
|
|
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;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-06-11 19:28:25 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-09-25 21:31:25 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2020-03-25 14:24:55 +00:00
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2020-09-14 12:56:05 +00:00
|
|
|
ReadExpect<'a, SysMetrics>,
|
2019-09-25 21:31:25 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-06-11 19:28:25 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Ori>,
|
2019-12-01 23:40:05 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
2019-12-20 03:31:41 +00:00
|
|
|
ReadStorage<'a, Body>,
|
|
|
|
ReadStorage<'a, Stats>,
|
2020-07-18 00:05:28 +00:00
|
|
|
ReadStorage<'a, Loadout>,
|
2020-04-26 17:03:19 +00:00
|
|
|
ReadStorage<'a, group::Group>,
|
2020-08-08 22:22:21 +00:00
|
|
|
ReadStorage<'a, CharacterState>,
|
2020-02-11 15:42:17 +00:00
|
|
|
WriteStorage<'a, Attacking>,
|
2019-06-11 19:28:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-09-25 21:31:25 +00:00
|
|
|
server_bus,
|
2020-03-25 14:24:55 +00:00
|
|
|
local_bus,
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics,
|
2019-09-25 21:31:25 +00:00
|
|
|
uids,
|
2019-06-11 19:28:25 +00:00
|
|
|
positions,
|
|
|
|
orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales,
|
2019-12-20 03:31:41 +00:00
|
|
|
bodies,
|
2019-09-26 16:48:37 +00:00
|
|
|
stats,
|
2020-07-18 00:05:28 +00:00
|
|
|
loadouts,
|
2020-04-26 17:03:19 +00:00
|
|
|
groups,
|
2020-03-07 21:03:10 +00:00
|
|
|
character_states,
|
2020-08-08 22:22:21 +00:00
|
|
|
mut attacking_storage,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "combat::Sys::run");
|
2020-03-22 04:49:32 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
2020-08-23 20:53:43 +00:00
|
|
|
let mut _local_emitter = local_bus.emitter();
|
2019-06-11 19:28:25 +00:00
|
|
|
// Attacks
|
2020-03-26 21:52:44 +00:00
|
|
|
for (entity, uid, pos, ori, scale_maybe, attack) in (
|
2019-09-29 11:41:04 +00:00
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales.maybe(),
|
2020-02-11 15:42:17 +00:00
|
|
|
&mut attacking_storage,
|
2019-09-29 11:41:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-08-24 19:12:54 +00:00
|
|
|
{
|
2020-03-10 17:54:59 +00:00
|
|
|
if attack.applied {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attack.applied = true;
|
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Go through all other entities
|
2020-04-26 17:03:19 +00:00
|
|
|
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()
|
2020-09-05 23:45:36 +00:00
|
|
|
{
|
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);
|
|
|
|
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
|
2020-09-05 16:41:11 +00:00
|
|
|
// 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
|
|
|
{
|
2020-04-26 17:03:19 +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);
|
|
|
|
// Don't heal if outside group
|
|
|
|
// Don't damage in the same group
|
2020-09-30 00:47:11 +00:00
|
|
|
let is_damage = !same_group && (attack.base_damage > 0);
|
|
|
|
let is_heal = same_group && (attack.base_heal > 0);
|
2020-08-28 02:13:57 +00:00
|
|
|
if !is_heal && !is_damage {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-26 17:03:19 +00:00
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Weapon gives base damage
|
2020-09-25 21:33:47 +00:00
|
|
|
let (source, healthchange) = if is_heal {
|
|
|
|
(DamageSource::Healing, attack.base_heal as f32)
|
2020-08-27 00:08:29 +00:00
|
|
|
} else {
|
2020-09-25 21:33:47 +00:00
|
|
|
(DamageSource::Melee, -(attack.base_damage as f32))
|
2020-08-28 02:13:57 +00:00
|
|
|
};
|
2020-07-25 23:57:04 +00:00
|
|
|
let mut damage = Damage {
|
2020-08-28 02:13:57 +00:00
|
|
|
healthchange,
|
2020-07-25 23:57:04 +00:00
|
|
|
source,
|
|
|
|
};
|
2020-03-22 15:25:47 +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;
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2020-07-18 00:05:28 +00:00
|
|
|
if let Some(loadout) = loadouts.get(b) {
|
2020-07-25 23:57:04 +00:00
|
|
|
damage.modify_damage(block, loadout);
|
2020-07-18 00:05:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 00:08:29 +00:00
|
|
|
if damage.healthchange != 0.0 {
|
2020-09-13 05:32:50 +00:00
|
|
|
let cause = if is_heal {
|
|
|
|
HealthSource::Healing { by: Some(*uid) }
|
|
|
|
} else {
|
|
|
|
HealthSource::Attack { by: *uid }
|
|
|
|
};
|
2020-09-07 21:46:25 +00:00
|
|
|
server_emitter.emit(ServerEvent::Damage {
|
|
|
|
uid: *uid_b,
|
|
|
|
change: HealthChange {
|
|
|
|
amount: damage.healthchange as i32,
|
|
|
|
cause,
|
|
|
|
},
|
|
|
|
});
|
2020-10-01 00:40:46 +00:00
|
|
|
// Test for server event of buff, remove before merging
|
|
|
|
server_emitter.emit(ServerEvent::Buff {
|
|
|
|
uid: *uid_b,
|
2020-10-01 17:33:35 +00:00
|
|
|
buff_change: buff::BuffChange::Add(buff::Buff::new(
|
2020-10-02 17:15:10 +00:00
|
|
|
buff::BuffId::Bleeding(-damage.healthchange),
|
|
|
|
Some(Duration::from_millis(10000)),
|
2020-10-01 17:33:35 +00:00
|
|
|
vec![buff::BuffCategoryId::Physical],
|
|
|
|
)),
|
2020-10-01 00:40:46 +00:00
|
|
|
});
|
2020-09-07 21:46:25 +00:00
|
|
|
attack.hit_count += 1;
|
2020-07-07 01:21:14 +00:00
|
|
|
}
|
2020-08-23 20:53:43 +00:00
|
|
|
if attack.knockback != 0.0 && damage.healthchange != 0.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));
|
2020-08-23 20:10:58 +00:00
|
|
|
server_emitter.emit(ServerEvent::Knockback {
|
2020-03-25 14:24:55 +00:00
|
|
|
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-03-25 14:24:55 +00:00
|
|
|
});
|
|
|
|
}
|
2020-02-11 15:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics.combat_ns.store(
|
|
|
|
start_time.elapsed().as_nanos() as i64,
|
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
2020-09-05 23:45:36 +00:00
|
|
|
}
|