veloren/common/src/sys/controller.rs

118 lines
3.3 KiB
Rust
Raw Normal View History

2019-06-09 14:20:20 +00:00
use crate::{
comp::{
Animation, AnimationInfo, Attacking, Controller, Gliding, HealthSource, Jumping, MoveDir,
OnGround, Respawning, Rolling, Stats, {ForceUpdate, Ori, Pos, Vel},
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>,
2019-06-13 18:09:50 +00:00
ReadStorage<'a, Vel>,
ReadStorage<'a, Ori>,
ReadStorage<'a, OnGround>,
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-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,
dt,
controllers,
stats,
positions,
2019-06-13 18:09:50 +00:00
velocities,
orientations,
on_grounds,
2019-06-09 19:33:20 +00:00
mut move_dirs,
mut jumpings,
mut attackings,
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-06-13 18:09:50 +00:00
for (entity, controller, stats, pos, vel, ori, on_ground) in (
2019-06-09 14:20:20 +00:00
&entities,
&controllers,
&stats,
&positions,
2019-06-13 18:09:50 +00:00
&velocities,
&orientations,
2019-06-09 19:33:20 +00:00
on_grounds.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-16 15:40:47 +00:00
// Move dir
if rollings.get(entity).is_none() {
move_dirs.insert(
entity,
MoveDir(if controller.move_dir.magnitude() > 1.0 {
controller.move_dir.normalized()
} else {
controller.move_dir
}),
);
}
2019-06-09 19:33:20 +00:00
// Glide
2019-06-16 15:40:47 +00:00
if controller.glide
&& on_ground.is_none()
&& attackings.get(entity).is_none()
&& rollings.get(entity).is_none()
{
2019-06-13 18:09:50 +00:00
glidings.insert(entity, Gliding);
2019-06-09 14:20:20 +00:00
} else {
2019-06-13 18:09:50 +00:00
glidings.remove(entity);
2019-06-09 14:20:20 +00:00
}
// Attack
2019-06-13 18:09:50 +00:00
if controller.attack
&& attackings.get(entity).is_none()
&& glidings.get(entity).is_none()
2019-06-16 15:40:47 +00:00
&& rollings.get(entity).is_none()
2019-06-13 18:09:50 +00:00
{
attackings.insert(entity, Attacking::start());
2019-06-09 19:33:20 +00:00
}
// Jump
2019-06-16 17:56:55 +00:00
if controller.jump && on_ground.is_some() && vel.0.z <= 0.0 {
2019-06-13 18:09:50 +00:00
jumpings.insert(entity, Jumping);
2019-06-09 14:20:20 +00:00
}
2019-06-11 04:08:55 +00:00
// Roll
2019-06-16 15:40:47 +00:00
if controller.roll
&& rollings.get(entity).is_none()
&& attackings.get(entity).is_none()
&& glidings.get(entity).is_none()
&& on_ground.is_some()
2019-06-29 12:10:52 +00:00
&& vel.0.magnitude_squared() > 25.0
2019-06-16 15:40:47 +00:00
{
2019-06-11 04:08:55 +00:00
rollings.insert(entity, Rolling::start());
}
2019-06-09 14:20:20 +00:00
}
}
}