2019-07-01 20:42:43 +00:00
|
|
|
use crate::comp::{
|
|
|
|
ActionState, Attacking, Controller, Gliding, Jumping, MoveDir, Respawning, Rolling, Stats, Vel,
|
2019-07-06 19:00:05 +00:00
|
|
|
Wielding,
|
2019-06-09 14:20:20 +00:00
|
|
|
};
|
2019-07-01 20:42:43 +00:00
|
|
|
use specs::{Entities, Join, ReadStorage, System, WriteStorage};
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
/// This system is responsible for validating controller inputs
|
2019-06-09 14:20:20 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
ReadStorage<'a, Controller>,
|
|
|
|
ReadStorage<'a, Stats>,
|
2019-06-13 18:09:50 +00:00
|
|
|
ReadStorage<'a, Vel>,
|
2019-06-30 17:48:38 +00:00
|
|
|
WriteStorage<'a, ActionState>,
|
2019-06-09 19:33:20 +00:00
|
|
|
WriteStorage<'a, MoveDir>,
|
2019-06-09 14:20:20 +00:00
|
|
|
WriteStorage<'a, Jumping>,
|
|
|
|
WriteStorage<'a, Attacking>,
|
2019-07-06 19:00:05 +00:00
|
|
|
WriteStorage<'a, Wielding>,
|
2019-06-11 04:08:55 +00:00
|
|
|
WriteStorage<'a, Rolling>,
|
2019-06-09 19:33:20 +00:00
|
|
|
WriteStorage<'a, Respawning>,
|
2019-06-09 14:20:20 +00:00
|
|
|
WriteStorage<'a, Gliding>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
|
|
|
controllers,
|
|
|
|
stats,
|
2019-06-13 18:09:50 +00:00
|
|
|
velocities,
|
2019-06-30 17:48:38 +00:00
|
|
|
mut action_states,
|
2019-06-09 19:33:20 +00:00
|
|
|
mut move_dirs,
|
|
|
|
mut jumpings,
|
|
|
|
mut attackings,
|
2019-07-06 19:00:05 +00:00
|
|
|
mut wieldings,
|
2019-06-11 04:08:55 +00:00
|
|
|
mut rollings,
|
2019-06-09 19:33:20 +00:00
|
|
|
mut respawns,
|
|
|
|
mut glidings,
|
2019-06-09 14:20:20 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2019-07-01 20:42:43 +00:00
|
|
|
for (entity, controller, stats, vel, mut a) in (
|
2019-06-09 14:20:20 +00:00
|
|
|
&entities,
|
|
|
|
&controllers,
|
|
|
|
&stats,
|
2019-06-13 18:09:50 +00:00
|
|
|
&velocities,
|
2019-06-30 17:48:38 +00:00
|
|
|
// Although this is changed, it is only kept for this system
|
|
|
|
// as it will be replaced in the action state system
|
|
|
|
&mut action_states,
|
2019-06-09 14:20:20 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
if stats.is_dead {
|
2019-06-09 19:33:20 +00:00
|
|
|
// Respawn
|
|
|
|
if controller.respawn {
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = respawns.insert(entity, Respawning);
|
2019-06-09 19:33:20 +00:00
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:40:47 +00:00
|
|
|
// Move dir
|
2019-06-29 20:11:21 +00:00
|
|
|
if !a.rolling {
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = move_dirs.insert(
|
2019-06-16 15:40:47 +00:00
|
|
|
entity,
|
2019-06-29 20:11:21 +00:00
|
|
|
MoveDir(if controller.move_dir.magnitude_squared() > 1.0 {
|
2019-06-16 15:40:47 +00:00
|
|
|
controller.move_dir.normalized()
|
|
|
|
} else {
|
|
|
|
controller.move_dir
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
// Glide
|
2019-06-29 20:11:21 +00:00
|
|
|
if controller.glide && !a.on_ground && !a.attacking && !a.rolling {
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = glidings.insert(entity, Gliding);
|
2019-06-30 17:48:38 +00:00
|
|
|
a.gliding = true;
|
2019-06-09 14:20:20 +00:00
|
|
|
} else {
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = glidings.remove(entity);
|
2019-06-30 17:48:38 +00:00
|
|
|
a.gliding = false;
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 19:00:05 +00:00
|
|
|
// Wield
|
|
|
|
if controller.attack && !a.wielding && !a.gliding && !a.rolling {
|
|
|
|
let _ = wieldings.insert(entity, Wielding::start());
|
|
|
|
a.wielding = true;
|
|
|
|
}
|
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
// Attack
|
2019-07-06 19:00:05 +00:00
|
|
|
if controller.attack
|
|
|
|
&& !a.attacking
|
|
|
|
&& wieldings.get(entity).map(|w| w.applied).unwrap_or(false)
|
|
|
|
&& !a.gliding
|
|
|
|
&& !a.rolling
|
|
|
|
{
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = attackings.insert(entity, Attacking::start());
|
2019-06-30 17:48:38 +00:00
|
|
|
a.attacking = true;
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
2019-06-11 04:08:55 +00:00
|
|
|
|
|
|
|
// Roll
|
2019-06-29 20:40:40 +00:00
|
|
|
if controller.roll
|
|
|
|
&& !a.rolling
|
|
|
|
&& a.on_ground
|
|
|
|
&& a.moving
|
|
|
|
&& !a.attacking
|
|
|
|
&& !a.gliding
|
|
|
|
{
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = rollings.insert(entity, Rolling::start());
|
2019-06-30 17:48:38 +00:00
|
|
|
a.rolling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump
|
|
|
|
if controller.jump && a.on_ground && vel.0.z <= 0.0 {
|
2019-07-01 20:42:43 +00:00
|
|
|
let _ = jumpings.insert(entity, Jumping);
|
2019-06-30 17:48:38 +00:00
|
|
|
a.on_ground = false;
|
2019-06-11 04:08:55 +00:00
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|