veloren/common/src/sys/animation.rs

59 lines
2.1 KiB
Rust
Raw Normal View History

2019-06-09 19:33:20 +00:00
use crate::{
comp::{
ActionState::*, Animation, AnimationInfo, CharacterState, MovementState::*, PhysicsState,
},
2019-06-09 19:33:20 +00:00
state::DeltaTime,
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
use std::fmt::Debug;
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>,
ReadStorage<'a, CharacterState>,
ReadStorage<'a, PhysicsState>,
2019-06-09 19:33:20 +00:00
WriteStorage<'a, AnimationInfo>,
);
fn run(
&mut self,
(entities, dt, character_states, physics_states, mut animation_infos): Self::SystemData,
) {
for (entity, character, physics) in (&entities, &character_states, &physics_states).join() {
fn impossible_animation(physics: PhysicsState, character: CharacterState) -> Animation {
warn!("Impossible animation: {:?} {:?}", physics, character);
Animation::Roll
2019-06-09 19:33:20 +00:00
}
let animation = match (physics.on_ground, &character.movement, &character.action) {
(_, Roll { .. }, Idle) => Animation::Roll,
(true, Stand, Idle) => Animation::Idle,
(true, Run, Idle) => Animation::Run,
(false, Jump, Idle) => Animation::Jump,
(true, Stand, Wield { .. }) => Animation::Cidle,
(true, Run, Wield { .. }) => Animation::Crun,
(false, Jump, Wield { .. }) => Animation::Cjump,
(_, Glide, Idle) => Animation::Gliding,
(_, _, Attack { .. }) => Animation::Attack,
_ => impossible_animation(physics.clone(), character.clone()),
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 + f64::from(dt.0));
2019-06-09 19:33:20 +00:00
let _ = animation_infos.insert(
2019-06-29 18:52:20 +00:00
entity,
AnimationInfo {
animation,
time: new_time.unwrap_or(0.0),
},
);
}
}
}