2019-06-09 19:33:20 +00:00
|
|
|
use crate::{
|
2019-06-13 18:09:50 +00:00
|
|
|
comp::{
|
|
|
|
phys, Animation, AnimationInfo, Attacking, Cidling, Crunning, Gliding, Jumping, OnGround,
|
|
|
|
Rolling,
|
|
|
|
},
|
2019-06-09 19:33:20 +00:00
|
|
|
state::DeltaTime,
|
|
|
|
};
|
|
|
|
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
|
2019-05-17 20:47:58 +00:00
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
/// This system will apply the animation that fits best to the users actions
|
2019-05-17 20:47:58 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2019-06-09 19:33:20 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
ReadStorage<'a, phys::Vel>,
|
|
|
|
ReadStorage<'a, OnGround>,
|
|
|
|
ReadStorage<'a, Jumping>,
|
|
|
|
ReadStorage<'a, Gliding>,
|
|
|
|
ReadStorage<'a, Attacking>,
|
2019-06-11 04:08:55 +00:00
|
|
|
ReadStorage<'a, Rolling>,
|
|
|
|
ReadStorage<'a, Crunning>,
|
|
|
|
ReadStorage<'a, Cidling>,
|
2019-06-09 19:33:20 +00:00
|
|
|
WriteStorage<'a, AnimationInfo>,
|
|
|
|
);
|
2019-05-17 20:47:58 +00:00
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-06-13 18:09:50 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
dt,
|
|
|
|
velocities,
|
|
|
|
on_grounds,
|
|
|
|
jumpings,
|
|
|
|
glidings,
|
|
|
|
attackings,
|
|
|
|
rollings,
|
|
|
|
crunnings,
|
|
|
|
cidlings,
|
|
|
|
mut animation_infos,
|
|
|
|
): Self::SystemData,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-06-13 18:09:50 +00:00
|
|
|
for (
|
|
|
|
entity,
|
|
|
|
vel,
|
|
|
|
on_ground,
|
|
|
|
jumping,
|
|
|
|
gliding,
|
|
|
|
attacking,
|
|
|
|
rolling,
|
|
|
|
crunning,
|
|
|
|
cidling,
|
|
|
|
mut animation_info,
|
|
|
|
) in (
|
2019-06-09 19:33:20 +00:00
|
|
|
&entities,
|
|
|
|
&velocities,
|
|
|
|
on_grounds.maybe(),
|
|
|
|
jumpings.maybe(),
|
|
|
|
glidings.maybe(),
|
|
|
|
attackings.maybe(),
|
2019-06-11 04:08:55 +00:00
|
|
|
rollings.maybe(),
|
|
|
|
crunnings.maybe(),
|
|
|
|
cidlings.maybe(),
|
2019-06-09 19:33:20 +00:00
|
|
|
&mut animation_infos,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-05-19 14:53:07 +00:00
|
|
|
animation_info.time += dt.0 as f64;
|
2019-06-09 19:33:20 +00:00
|
|
|
let moving = vel.0.magnitude() > 3.0;
|
|
|
|
|
|
|
|
fn impossible_animation() -> Animation {
|
|
|
|
warn!("Impossible animation");
|
|
|
|
Animation::Idle
|
|
|
|
}
|
|
|
|
|
|
|
|
let animation = match (
|
|
|
|
on_ground.is_some(),
|
|
|
|
moving,
|
|
|
|
attacking.is_some(),
|
|
|
|
gliding.is_some(),
|
2019-06-13 18:09:50 +00:00
|
|
|
rolling.is_some(),
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-06-13 18:09:50 +00:00
|
|
|
(true, false, false, false, false) => Animation::Idle,
|
|
|
|
(true, true, false, false, false) => Animation::Run,
|
|
|
|
(false, _, false, false, false) => Animation::Jump,
|
|
|
|
(_, _, false, true, false) => Animation::Gliding,
|
|
|
|
(_, _, true, false, false) => Animation::Attack,
|
|
|
|
(_, true, false, false, true) => {
|
|
|
|
dbg!("roll");
|
|
|
|
Animation::Roll
|
|
|
|
}
|
|
|
|
//(_, true, false, false, false) => Animation::Crun,
|
|
|
|
//(true, false, false, false, false) => Animation::Cidle,
|
|
|
|
(_, _, true, true, _) => impossible_animation(), // Attack while gliding
|
|
|
|
(_, _, true, _, true) => impossible_animation(), // Roll while attacking
|
|
|
|
(_, _, _, true, true) => impossible_animation(), // Roll while gliding
|
|
|
|
(_, false, _, _, true) => impossible_animation(), // Roll without moving
|
2019-06-09 19:33:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let last = animation_info.clone();
|
|
|
|
let changed = last.animation != animation;
|
|
|
|
|
|
|
|
*animation_info = AnimationInfo {
|
|
|
|
animation,
|
|
|
|
time: if changed { 0.0 } else { last.time },
|
|
|
|
changed,
|
|
|
|
};
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|