2019-06-11 19:28:25 +00:00
|
|
|
use crate::{
|
2019-10-17 20:59:36 +00:00
|
|
|
comp::{
|
2019-12-03 06:30:08 +00:00
|
|
|
ActionState::*, CharacterState, Controller, HealthChange, HealthSource, Item, ItemKind,
|
|
|
|
Ori, Pos, Stats,
|
2019-10-17 20:59:36 +00:00
|
|
|
},
|
2019-09-25 21:31:25 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2019-06-11 19:28:25 +00:00
|
|
|
state::{DeltaTime, Uid},
|
|
|
|
};
|
|
|
|
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
|
2019-08-23 10:11:37 +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;
|
|
|
|
|
|
|
|
const ATTACK_RANGE: f32 = 4.0;
|
|
|
|
const BLOCK_ANGLE: f32 = 180.0;
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
/// 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>>,
|
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-08-24 19:12:54 +00:00
|
|
|
ReadStorage<'a, Controller>,
|
2019-08-23 10:11:37 +00:00
|
|
|
WriteStorage<'a, CharacterState>,
|
2019-06-11 19:28:25 +00:00
|
|
|
WriteStorage<'a, Stats>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-09-25 21:31:25 +00:00
|
|
|
server_bus,
|
|
|
|
local_bus,
|
2019-06-11 19:28:25 +00:00
|
|
|
dt,
|
2019-09-25 21:31:25 +00:00
|
|
|
uids,
|
2019-06-11 19:28:25 +00:00
|
|
|
positions,
|
|
|
|
orientations,
|
2019-08-24 19:12:54 +00:00
|
|
|
controllers,
|
2019-08-23 10:11:37 +00:00
|
|
|
mut character_states,
|
2019-09-26 16:48:37 +00:00
|
|
|
stats,
|
2019-06-11 19:28:25 +00:00
|
|
|
): 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
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Attacks
|
2019-09-29 11:41:04 +00:00
|
|
|
for (entity, uid, pos, ori, _, stat) in (
|
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
|
|
|
&controllers,
|
|
|
|
&stats,
|
|
|
|
)
|
|
|
|
.join()
|
2019-08-24 19:12:54 +00:00
|
|
|
{
|
2019-12-03 06:30:08 +00:00
|
|
|
let recover_duration = if let Some(Item {
|
|
|
|
kind: ItemKind::Tool { kind, .. },
|
|
|
|
..
|
|
|
|
}) = stat.equipment.main
|
|
|
|
{
|
|
|
|
kind.attack_recover_duration()
|
|
|
|
} else {
|
|
|
|
Duration::from_secs(1)
|
|
|
|
};
|
|
|
|
|
2019-08-26 19:15:49 +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)
|
2019-08-24 16:38:59 +00:00
|
|
|
{
|
2019-08-26 19:15:49 +00:00
|
|
|
*time_left = time_left
|
|
|
|
.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
.unwrap_or_default();
|
2019-12-03 06:30:08 +00:00
|
|
|
if !*applied && recover_duration > *time_left {
|
2019-08-26 19:15:49 +00:00
|
|
|
*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) {
|
2019-08-26 19:15:49 +00:00
|
|
|
// 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 (
|
2019-08-24 16:38:59 +00:00
|
|
|
&entities,
|
2019-09-25 21:31:25 +00:00
|
|
|
&uids,
|
2019-08-24 16:38:59 +00:00
|
|
|
&positions,
|
|
|
|
&orientations,
|
|
|
|
&character_states,
|
2019-09-26 16:48:37 +00:00
|
|
|
&stats,
|
2019-08-24 16:38:59 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-06-11 19:28:25 +00:00
|
|
|
{
|
2019-08-25 19:09:10 +00:00
|
|
|
// 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
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Check if it is a hit
|
|
|
|
if entity != b
|
|
|
|
&& !stat_b.is_dead
|
2019-08-25 19:09:10 +00:00
|
|
|
&& 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-12-03 06:30:08 +00:00
|
|
|
&& ori2.angle_between(pos_b2 - pos2) < (2.0 / pos2.distance(pos_b2)).atan()
|
2019-06-11 19:28:25 +00:00
|
|
|
{
|
2019-09-29 11:41:04 +00:00
|
|
|
// Weapon gives base damage
|
2019-10-22 18:18:40 +00:00
|
|
|
let mut dmg = if let Some(ItemKind::Tool { power, .. }) =
|
|
|
|
stat.equipment.main.as_ref().map(|i| &i.kind)
|
|
|
|
{
|
|
|
|
*power as i32
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2019-09-29 11:41:04 +00:00
|
|
|
|
|
|
|
// Block
|
|
|
|
if character_b.action.is_block()
|
2019-08-25 19:09:10 +00:00
|
|
|
&& ori_b.0.angle_between(pos.0 - pos_b.0).to_degrees()
|
|
|
|
< BLOCK_ANGLE / 2.0
|
2019-08-24 16:38:59 +00:00
|
|
|
{
|
2019-10-17 20:59:36 +00:00
|
|
|
dmg = (dmg as f32 * (1.0 - BLOCK_EFFICIENCY)) as i32
|
2019-09-29 11:41:04 +00:00
|
|
|
}
|
2019-08-24 16:38:59 +00:00
|
|
|
|
2019-09-25 21:31:25 +00:00
|
|
|
server_emitter.emit(ServerEvent::Damage {
|
|
|
|
uid: *uid_b,
|
2019-10-17 20:59:36 +00:00
|
|
|
change: HealthChange {
|
|
|
|
amount: -dmg,
|
|
|
|
cause: HealthSource::Attack { by: *uid },
|
|
|
|
},
|
2019-09-29 11:41:04 +00:00
|
|
|
});
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 19:15:49 +00:00
|
|
|
}
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2019-08-26 19:15:49 +00:00
|
|
|
if should_end {
|
|
|
|
if let Some(character) = &mut character_states.get_mut(entity) {
|
|
|
|
character.action = Wield {
|
|
|
|
time_left: Duration::default(),
|
|
|
|
};
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 16:38:59 +00:00
|
|
|
if let Some(Wield { time_left }) =
|
|
|
|
&mut character_states.get_mut(entity).map(|c| &mut c.action)
|
|
|
|
{
|
2019-08-23 10:11:37 +00:00
|
|
|
if *time_left != Duration::default() {
|
|
|
|
*time_left = time_left
|
|
|
|
.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
.unwrap_or_default();
|
2019-07-06 19:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
}
|