veloren/common/src/sys/combat.rs

138 lines
4.3 KiB
Rust
Raw Normal View History

use crate::{
comp::{
Agent, Attacking, Body, CharacterState, Controller, HealthChange, HealthSource, Ori, Pos,
Scale, Stats,
},
2020-03-21 22:55:20 +00:00
event::{EventBus, ServerEvent},
2019-11-24 20:12:03 +00:00
sync::Uid,
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
2019-08-24 20:41:34 +00:00
use vek::*;
const BLOCK_EFFICIENCY: f32 = 0.9;
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>>,
ReadStorage<'a, Uid>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Ori>,
ReadStorage<'a, Scale>,
ReadStorage<'a, Agent>,
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,
uids,
positions,
orientations,
scales,
agents,
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,
) {
2020-03-22 04:49:32 +00:00
let mut server_emitter = server_bus.emitter();
// Attacks
for (entity, uid, pos, ori, scale_maybe, agent_maybe, _, _attacker_stats, attack) in (
&entities,
&uids,
&positions,
&orientations,
scales.maybe(),
agents.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);
2020-03-22 04:49:32 +00:00
let pos_b2 = Vec2::<f32>::from(pos_b.0);
2020-02-24 13:32:12 +00:00
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.max_angle + (rad_b / pos2.distance(pos_b2)).atan()
2020-02-24 13:32:12 +00:00
{
// Weapon gives base damage
2020-03-24 21:03:11 +00:00
let mut healthchange = attack.base_healthchange;
// NPCs do less damage:
if agent_maybe.is_some() {
2020-03-24 21:03:11 +00:00
if healthchange > 0 {
healthchange = (healthchange / 2).max(1);
}
if healthchange < 0 {
healthchange = (healthchange / 2).min(-1);
}
}
if rand::random() {
2020-03-24 21:03:11 +00:00
healthchange = (healthchange as f32 * 1.2) as i32;
}
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-24 21:03:11 +00:00
healthchange = (healthchange as f32 * (1.0 - BLOCK_EFFICIENCY)) as i32
2020-02-11 15:42:17 +00:00
}
2020-03-22 04:49:32 +00:00
server_emitter.emit(ServerEvent::Damage {
2020-02-24 13:32:12 +00:00
uid: *uid_b,
change: HealthChange {
2020-03-24 21:03:11 +00:00
amount: healthchange,
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
}
}
}
}
}