2019-06-11 19:28:25 +00:00
|
|
|
use crate::{
|
2019-10-17 20:59:36 +00:00
|
|
|
comp::{
|
2020-03-07 21:03:10 +00:00
|
|
|
Attacking, Body, CharacterState, Controller, HealthChange, HealthSource, Ori, Pos, Scale,
|
|
|
|
Stats,
|
2019-10-17 20:59:36 +00:00
|
|
|
},
|
2019-09-25 21:31:25 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2019-11-24 20:12:03 +00:00
|
|
|
state::DeltaTime,
|
|
|
|
sync::Uid,
|
2019-06-11 19:28:25 +00:00
|
|
|
};
|
|
|
|
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::*;
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2019-08-25 19:09:10 +00:00
|
|
|
const BLOCK_EFFICIENCY: f32 = 0.9;
|
|
|
|
|
2019-12-01 23:40:05 +00:00
|
|
|
const ATTACK_RANGE: f32 = 3.5;
|
2020-03-16 12:19:51 +00:00
|
|
|
const ATTACK_ANGLE: f32 = 70.0;
|
2019-08-25 19:09:10 +00:00
|
|
|
const BLOCK_ANGLE: f32 = 180.0;
|
|
|
|
|
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 {
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-09-25 21:31:25 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2019-06-11 19:28:25 +00:00
|
|
|
Read<'a, DeltaTime>,
|
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-08-24 19:12:54 +00:00
|
|
|
ReadStorage<'a, Controller>,
|
2019-12-20 03:31:41 +00:00
|
|
|
ReadStorage<'a, Body>,
|
|
|
|
ReadStorage<'a, Stats>,
|
2020-02-11 15:42:17 +00:00
|
|
|
WriteStorage<'a, Attacking>,
|
2019-08-23 10:11:37 +00:00
|
|
|
WriteStorage<'a, CharacterState>,
|
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-07 21:03:10 +00:00
|
|
|
_local_bus,
|
|
|
|
_dt,
|
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-08-24 19:12:54 +00:00
|
|
|
controllers,
|
2019-12-20 03:31:41 +00:00
|
|
|
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,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
|
|
|
// Attacks
|
2020-03-07 21:03:10 +00:00
|
|
|
for (entity, uid, pos, ori, scale_maybe, _, _attacker_stats, attack) in (
|
2019-09-29 11:41:04 +00:00
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales.maybe(),
|
2019-09-29 11:41:04 +00:00
|
|
|
&controllers,
|
|
|
|
&stats,
|
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
|
|
|
|
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
|
2020-03-14 21:33:20 +00:00
|
|
|
let mut dmg = attack.base_damage;
|
2019-09-29 11:41:04 +00:00
|
|
|
|
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
|
|
|
{
|
2020-03-14 21:33:20 +00:00
|
|
|
dmg = (dmg as f32 * (1.0 - BLOCK_EFFICIENCY)) as u32
|
2020-02-11 15:42:17 +00:00
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
server_bus.emitter().emit(ServerEvent::Damage {
|
|
|
|
uid: *uid_b,
|
|
|
|
change: HealthChange {
|
2020-03-14 21:33:20 +00:00
|
|
|
amount: -(dmg as i32),
|
2020-02-24 13:32:12 +00:00
|
|
|
cause: HealthSource::Attack { by: *uid },
|
|
|
|
},
|
|
|
|
});
|
2020-03-10 17:54:59 +00:00
|
|
|
attack.hit_count += 1;
|
2020-02-11 15:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
}
|