veloren/common/src/sys/combat.rs

161 lines
5.5 KiB
Rust
Raw Normal View History

use crate::{
comp::{
ActionState::*, CharacterState, Controller, HealthChange, HealthSource, ItemKind, Ori, Pos,
Stats,
},
2019-09-25 21:31:25 +00:00
event::{EventBus, LocalEvent, ServerEvent},
state::{DeltaTime, Uid},
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
use std::time::Duration;
2019-08-24 20:41:34 +00:00
use vek::*;
2019-08-26 18:05:30 +00:00
pub const WIELD_DURATION: Duration = Duration::from_millis(300);
pub const ATTACK_DURATION: Duration = Duration::from_millis(500);
// Delay before hit
const PREPARE_DURATION: Duration = Duration::from_millis(100);
2019-08-26 18:05:30 +00:00
const BLOCK_EFFICIENCY: f32 = 0.9;
const ATTACK_RANGE: f32 = 4.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>,
2019-08-24 19:12:54 +00:00
ReadStorage<'a, Controller>,
WriteStorage<'a, CharacterState>,
WriteStorage<'a, Stats>,
);
fn run(
&mut self,
(
entities,
2019-09-25 21:31:25 +00:00
server_bus,
local_bus,
dt,
2019-09-25 21:31:25 +00:00
uids,
positions,
orientations,
2019-08-24 19:12:54 +00:00
controllers,
mut character_states,
2019-09-26 16:48:37 +00:00
stats,
): Self::SystemData,
) {
2019-09-25 21:31:25 +00:00
let mut server_emitter = server_bus.emitter();
2019-09-26 16:48:37 +00:00
let mut _local_emitter = local_bus.emitter();
2019-09-25 21:31:25 +00:00
// Attacks
for (entity, uid, pos, ori, _, stat) in (
&entities,
&uids,
&positions,
&orientations,
&controllers,
&stats,
)
.join()
2019-08-24 19:12:54 +00:00
{
let (deal_damage, should_end) = if let Some(Attack { time_left, applied }) =
&mut character_states.get_mut(entity).map(|c| &mut c.action)
{
*time_left = time_left
.checked_sub(Duration::from_secs_f32(dt.0))
.unwrap_or_default();
if !*applied && ATTACK_DURATION - *time_left > PREPARE_DURATION {
*applied = true;
(true, false)
} else if *time_left == Duration::default() {
(false, true)
} else {
(false, false)
}
} else {
(false, false)
};
if deal_damage {
2019-09-04 23:03:49 +00:00
if let Some(Attack { .. }) = &character_states.get(entity).map(|c| c.action) {
// Go through all other entities
2019-09-26 16:48:37 +00:00
for (b, uid_b, pos_b, ori_b, character_b, stat_b) in (
&entities,
2019-09-25 21:31:25 +00:00
&uids,
&positions,
&orientations,
&character_states,
2019-09-26 16:48:37 +00:00
&stats,
)
.join()
{
// 2D versions
2019-08-24 20:41:34 +00:00
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
// Check if it is a hit
if entity != b
&& !stat_b.is_dead
&& pos.0.distance_squared(pos_b.0) < ATTACK_RANGE.powi(2)
2019-08-24 19:12:54 +00:00
// TODO: Use size instead of 1.0
2019-08-24 20:41:34 +00:00
&& ori2.angle_between(pos_b2 - pos2) < (1.0 / pos2.distance(pos_b2)).atan()
{
// Weapon gives base damage
let mut dmg = if let Some(ItemKind::Tool { power, .. }) =
stat.equipment.main.as_ref().map(|i| &i.kind)
{
*power as i32
} else {
1
};
// Block
if character_b.action.is_block()
&& ori_b.0.angle_between(pos.0 - pos_b.0).to_degrees()
< BLOCK_ANGLE / 2.0
{
dmg = (dmg as f32 * (1.0 - BLOCK_EFFICIENCY)) as i32
}
2019-09-25 21:31:25 +00:00
server_emitter.emit(ServerEvent::Damage {
uid: *uid_b,
change: HealthChange {
amount: -dmg,
cause: HealthSource::Attack { by: *uid },
},
});
}
}
}
}
if should_end {
if let Some(character) = &mut character_states.get_mut(entity) {
character.action = Wield {
time_left: Duration::default(),
};
}
}
if let Some(Wield { time_left }) =
&mut character_states.get_mut(entity).map(|c| &mut c.action)
{
if *time_left != Duration::default() {
*time_left = time_left
.checked_sub(Duration::from_secs_f32(dt.0))
.unwrap_or_default();
}
}
}
}
}