2019-06-09 19:33:20 +00:00
|
|
|
use crate::{
|
2019-06-14 15:27:05 +00:00
|
|
|
comp::{
|
2019-06-29 17:49:51 +00:00
|
|
|
Animation, AnimationInfo, ForceUpdate, ActionState
|
2019-06-14 15:27:05 +00:00
|
|
|
},
|
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>,
|
2019-06-29 17:49:51 +00:00
|
|
|
ReadStorage<'a, ActionState>,
|
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,
|
2019-06-29 17:49:51 +00:00
|
|
|
action_states,
|
2019-06-13 18:09:50 +00:00
|
|
|
mut animation_infos,
|
|
|
|
): Self::SystemData,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-06-29 18:23:18 +00:00
|
|
|
for (entity, a) in (
|
2019-06-09 19:33:20 +00:00
|
|
|
&entities,
|
2019-06-29 17:49:51 +00:00
|
|
|
&action_states,
|
2019-06-09 19:33:20 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-06-13 19:19:13 +00:00
|
|
|
fn impossible_animation(message: &str) -> Animation {
|
|
|
|
warn!("{}", message);
|
2019-06-09 19:33:20 +00:00
|
|
|
Animation::Idle
|
|
|
|
}
|
|
|
|
|
|
|
|
let animation = match (
|
2019-06-29 17:49:51 +00:00
|
|
|
a.on_ground,
|
|
|
|
a.moving,
|
|
|
|
a.attacking,
|
|
|
|
a.gliding,
|
|
|
|
a.rolling,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-06-13 19:19:13 +00:00
|
|
|
(_, _, 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"),
|
|
|
|
(_, true, false, false, true) => Animation::Roll,
|
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,
|
2019-06-09 19:33:20 +00:00
|
|
|
};
|
|
|
|
|
2019-06-29 18:23:18 +00:00
|
|
|
let new_time = animation_infos.get(entity).filter(|i| i.animation == animation).map(|i| i.time + dt.0 as f64);
|
2019-06-09 19:33:20 +00:00
|
|
|
|
2019-06-29 18:23:18 +00:00
|
|
|
animation_infos.insert(entity, AnimationInfo {
|
2019-06-09 19:33:20 +00:00
|
|
|
animation,
|
2019-06-29 18:23:18 +00:00
|
|
|
time: new_time.unwrap_or(0.0),
|
|
|
|
});
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|