2019-06-11 19:28:25 +00:00
|
|
|
use crate::{
|
2019-08-24 19:12:54 +00:00
|
|
|
comp::{
|
|
|
|
ActionState::*, CharacterState, Controller, ForceUpdate, HealthSource, Ori, Pos, Stats, Vel,
|
|
|
|
},
|
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 BASE_DMG: i32 = 10;
|
|
|
|
const BLOCK_EFFICIENCY: f32 = 0.9;
|
|
|
|
|
|
|
|
const ATTACK_RANGE: f32 = 4.0;
|
|
|
|
const BLOCK_ANGLE: f32 = 180.0;
|
|
|
|
|
|
|
|
const KNOCKBACK_XY: f32 = 2.0;
|
|
|
|
const KNOCKBACK_Z: f32 = 2.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>,
|
|
|
|
ReadStorage<'a, Uid>,
|
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Ori>,
|
2019-08-24 19:12:54 +00:00
|
|
|
ReadStorage<'a, Controller>,
|
2019-06-13 18:09:50 +00:00
|
|
|
WriteStorage<'a, Vel>,
|
2019-08-23 10:11:37 +00:00
|
|
|
WriteStorage<'a, CharacterState>,
|
2019-06-11 19:28:25 +00:00
|
|
|
WriteStorage<'a, Stats>,
|
|
|
|
WriteStorage<'a, ForceUpdate>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
|
|
|
uids,
|
|
|
|
dt,
|
|
|
|
positions,
|
|
|
|
orientations,
|
2019-08-24 19:12:54 +00:00
|
|
|
controllers,
|
2019-06-13 18:09:50 +00:00
|
|
|
mut velocities,
|
2019-08-23 10:11:37 +00:00
|
|
|
mut character_states,
|
2019-06-11 19:28:25 +00:00
|
|
|
mut stats,
|
|
|
|
mut force_updates,
|
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
|
|
|
// Attacks
|
2019-08-24 19:12:54 +00:00
|
|
|
for (entity, uid, pos, ori, controller) in
|
|
|
|
(&entities, &uids, &positions, &orientations, &controllers).join()
|
|
|
|
{
|
2019-08-23 10:11:37 +00:00
|
|
|
let mut todo_end = false;
|
|
|
|
|
|
|
|
// Go through all other entities
|
2019-08-24 16:38:59 +00:00
|
|
|
if let Some(Attack { time_left, applied }) =
|
|
|
|
&mut character_states.get(entity).map(|c| c.action)
|
|
|
|
{
|
2019-08-23 10:11:37 +00:00
|
|
|
if !*applied {
|
2019-08-24 16:38:59 +00:00
|
|
|
for (b, pos_b, ori_b, character_b, mut vel_b, stat_b) in (
|
|
|
|
&entities,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
|
|
|
&character_states,
|
|
|
|
&mut velocities,
|
|
|
|
&mut stats,
|
|
|
|
)
|
|
|
|
.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-08-24 20:41:34 +00:00
|
|
|
&& ori2.angle_between(pos_b2 - pos2) < (1.0 / pos2.distance(pos_b2)).atan()
|
2019-06-11 19:28:25 +00:00
|
|
|
{
|
2019-08-24 16:38:59 +00:00
|
|
|
let dmg = 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-08-25 19:09:10 +00:00
|
|
|
(BASE_DMG as f32 * (1.0 - BLOCK_EFFICIENCY)) as i32
|
2019-08-24 16:38:59 +00:00
|
|
|
} else {
|
2019-08-25 19:09:10 +00:00
|
|
|
BASE_DMG
|
2019-08-24 16:38:59 +00:00
|
|
|
};
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Deal damage
|
2019-06-30 11:48:28 +00:00
|
|
|
stat_b
|
|
|
|
.health
|
2019-08-24 16:38:59 +00:00
|
|
|
.change_by(-dmg, HealthSource::Attack { by: *uid }); // TODO: variable damage and weapon
|
2019-08-25 19:09:10 +00:00
|
|
|
vel_b.0 += (pos_b.0 - pos.0).normalized() * KNOCKBACK_XY;
|
|
|
|
vel_b.0.z = KNOCKBACK_Z;
|
2019-06-16 19:53:10 +00:00
|
|
|
let _ = force_updates.insert(b, ForceUpdate);
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:38:59 +00:00
|
|
|
if let Some(Attack { time_left, applied }) =
|
|
|
|
&mut character_states.get_mut(entity).map(|c| &mut c.action)
|
|
|
|
{
|
|
|
|
// Only attack once
|
|
|
|
*applied = true;
|
|
|
|
|
|
|
|
if *time_left == Duration::default() {
|
|
|
|
todo_end = true;
|
|
|
|
} else {
|
|
|
|
*time_left = time_left
|
|
|
|
.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
.unwrap_or_default();
|
|
|
|
}
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
if todo_end {
|
2019-08-24 16:38:59 +00:00
|
|
|
if let Some(character) = &mut character_states.get_mut(entity) {
|
|
|
|
character.action = Wield {
|
|
|
|
time_left: Duration::default(),
|
|
|
|
};
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|