2019-06-11 19:28:25 +00:00
|
|
|
use crate::{
|
2019-08-23 10:11:37 +00:00
|
|
|
comp::{ActionState::*, CharacterState, 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-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-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-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-23 10:11:37 +00:00
|
|
|
for (entity, uid, pos, ori, mut character) in (
|
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
|
|
|
&mut character_states,
|
|
|
|
)
|
2019-06-11 19:28:25 +00:00
|
|
|
.join()
|
2019-08-23 10:11:37 +00:00
|
|
|
{
|
|
|
|
let mut todo_end = false;
|
|
|
|
|
|
|
|
// Go through all other entities
|
|
|
|
if let Attack { time_left, applied } = &mut character.action {
|
|
|
|
if !*applied {
|
2019-07-01 20:42:43 +00:00
|
|
|
for (b, pos_b, mut vel_b, stat_b) in
|
2019-06-11 19:28:25 +00:00
|
|
|
(&entities, &positions, &mut velocities, &mut stats).join()
|
|
|
|
{
|
|
|
|
// Check if it is a hit
|
|
|
|
if entity != b
|
|
|
|
&& !stat_b.is_dead
|
|
|
|
&& pos.0.distance_squared(pos_b.0) < 50.0
|
2019-08-02 19:27:21 +00:00
|
|
|
&& ori.0.angle_between(pos_b.0 - pos.0).to_degrees() < 90.0
|
2019-06-11 19:28:25 +00:00
|
|
|
{
|
|
|
|
// Deal damage
|
2019-06-30 11:48:28 +00:00
|
|
|
stat_b
|
|
|
|
.health
|
|
|
|
.change_by(-10, HealthSource::Attack { by: *uid }); // TODO: variable damage and weapon
|
2019-08-03 21:11:57 +00:00
|
|
|
vel_b.0 += (pos_b.0 - pos.0).normalized() * 2.0;
|
|
|
|
vel_b.0.z = 2.0;
|
2019-06-16 19:53:10 +00:00
|
|
|
let _ = force_updates.insert(b, ForceUpdate);
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
*applied = true;
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
if *time_left == Duration::default() {
|
|
|
|
todo_end = true;
|
2019-06-11 19:28:25 +00:00
|
|
|
} else {
|
2019-08-23 10:11:37 +00:00
|
|
|
*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 {
|
|
|
|
character.action = Wield {
|
|
|
|
time_left: Duration::default(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Wield { time_left } = &mut character.action {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|