veloren/common/src/sys/controller.rs

113 lines
2.9 KiB
Rust
Raw Normal View History

2019-06-09 14:20:20 +00:00
use crate::{
comp::{
phys::{ForceUpdate, Ori, Pos, Vel},
Attacking, Controller, Gliding, Jumping, MoveDir, OnGround, Respawning, Stats,
2019-06-09 14:20:20 +00:00
},
state::DeltaTime,
2019-06-09 14:20:20 +00:00
};
use specs::{Entities, Join, Read, 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>,
Read<'a, DeltaTime>,
ReadStorage<'a, Controller>,
ReadStorage<'a, Stats>,
ReadStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Ori>,
2019-06-09 19:33:20 +00:00
WriteStorage<'a, MoveDir>,
WriteStorage<'a, OnGround>,
2019-06-09 14:20:20 +00:00
WriteStorage<'a, Jumping>,
WriteStorage<'a, Attacking>,
2019-06-09 19:33:20 +00:00
WriteStorage<'a, Respawning>,
2019-06-09 14:20:20 +00:00
WriteStorage<'a, Gliding>,
WriteStorage<'a, ForceUpdate>,
);
fn run(
&mut self,
(
entities,
dt,
controllers,
stats,
positions,
mut velocities,
mut orientations,
2019-06-09 19:33:20 +00:00
mut move_dirs,
mut on_grounds,
mut jumpings,
mut attackings,
mut respawns,
mut glidings,
force_updates,
2019-06-09 14:20:20 +00:00
): Self::SystemData,
) {
for (
entity,
controller,
stats,
pos,
mut vel,
mut ori,
on_ground,
mut attacking,
mut jumping,
mut gliding,
) in (
2019-06-09 14:20:20 +00:00
&entities,
&controllers,
&stats,
&positions,
&mut velocities,
&mut orientations,
2019-06-09 19:33:20 +00:00
on_grounds.maybe(),
attackings.maybe(),
jumpings.maybe(),
glidings.maybe(),
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 {
respawns.insert(entity, Respawning);
}
2019-06-09 14:20:20 +00:00
continue;
}
2019-06-09 19:33:20 +00:00
// Glide
if controller.glide && on_ground.is_none() && attacking.is_none() {
gliding = Some(&Gliding);
2019-06-09 14:20:20 +00:00
} else {
gliding = None
2019-06-09 14:20:20 +00:00
}
2019-06-09 19:33:20 +00:00
// Move dir
move_dirs.insert(
entity,
MoveDir(if controller.move_dir.magnitude() > 1.0 {
controller.move_dir.normalized()
} else {
controller.move_dir
}),
);
2019-06-09 14:20:20 +00:00
// Attack
if controller.attack && attacking.is_none() && gliding.is_none() {
attacking = Some(&Attacking::start());
2019-06-09 19:33:20 +00:00
}
// Jump
if on_ground.is_some() && controller.jump && vel.0.z <= 0.0 {
jumping = Some(&Jumping);
2019-06-09 19:33:20 +00:00
} else {
jumping = None;
2019-06-09 14:20:20 +00:00
}
}
}
}