veloren/common/src/sys/animation.rs

61 lines
2.4 KiB
Rust
Raw Normal View History

2019-06-09 19:33:20 +00:00
use crate::{
2019-06-29 18:52:20 +00:00
comp::{ActionState, Animation, AnimationInfo, ForceUpdate},
2019-06-09 19:33:20 +00:00
state::DeltaTime,
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
2019-06-09 19:33:20 +00:00
/// This system will apply the animation that fits best to the users actions
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-06-29 18:52:20 +00:00
fn run(&mut self, (entities, dt, action_states, mut animation_infos): Self::SystemData) {
for (entity, a) in (&entities, &action_states).join() {
fn impossible_animation(message: &str) -> Animation {
warn!("{}", message);
2019-06-09 19:33:20 +00:00
Animation::Idle
}
2019-06-30 22:52:25 +00:00
let animation = match (
a.on_ground,
a.moving,
a.attacking,
a.gliding,
a.rolling,
a.wielding,
) {
(_, _, 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,
(true, false, false, false, false, false) => Animation::Idle,
(true, true, false, false, false, false) => Animation::Run,
(false, _, false, false, false, false) => Animation::Jump,
(true, false, false, false, false, true) => Animation::Cidle,
(true, true, false, false, false, true) => Animation::Crun,
(false, _, false, false, false, true) => Animation::Cjump,
(_, _, false, true, false, _) => Animation::Gliding,
(_, _, true, false, false, _) => Animation::Attack,
2019-06-09 19:33:20 +00:00
};
2019-06-29 18:52:20 +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:52:20 +00:00
animation_infos.insert(
entity,
AnimationInfo {
animation,
time: new_time.unwrap_or(0.0),
},
);
}
}
}