#Animations management

16 messages · Page 1 of 1 (latest)

sweet edge
#

u could use an enum to decide which set of frames to use

#

just requires a bit of code to change the enum whenever something should change it & something to decide which to play

ember ether
sweet edge
#

ive never used events before, im quite used to rust on its own so im looking into it now ·::P

viscid kestrel
#

for example you can attach a component to your player that contains the index of the current animation and track its change
it is convenient if there is more than one animated entity

#

in case of single animated entity of course it is easier to call the trigger and intercept it in the observer

viscid kestrel
#

You can also create a resource with the index of the current animation and track its change

ember ether
viscid kestrel
#

if you have several animated characters you can go the following way
You can create a resource to store all animations as a hashmap

struct AnimationSet {
    animations: Vec<AnimationNodeIndex>,
    graph: Handle<AnimationGraph>,
}
#[derive(Resource)]
pub struct AllAnimations(HashMap<&'static str, AnimationSet>);

in this case key of hashmap will be "Player" or "Monster" or "Etc"
Then insert component for each animated entity


#[derive(Component)]
pub struct AniData {
    pub animation_index: usize,
    /// current animation index 
    pub player_entity: Entity,
    /// entity of AnimatiionPlayer component (for convenience)
    pub animation_key: &'static str
    // key for link with animation set
}
viscid kestrel
#

then depends on situation you can update AniData on desired entity
and track it changes

pub fn switch(
    mut animation_players: Query<(&mut AnimationPlayer, &mut AnimationTransitions)>,
    objects_q: Query<&AniData, Changed<AniData>>,
    all_animations: Res<AllAnimations>,
) {
    for adata in objects_q.iter() {
        if let Ok((mut player, mut transitions)) = animation_players.get_mut(adata.player_entity) {
            let ani_set = all_animations.0.get(adata.animation_key).unwrap();
            transitions
            .play(
                &mut player,
                ani_set.animations[adata.animation_index],
                Duration::from_millis(250),
            )
            .repeat();            
        }
    }
}
ember ether
#

@viscid kestrel thanks, I thought exactly about this approach
Also I think I need observer (trigger) to catch movement changes (stay/run etc)

viscid kestrel
#

You can use a trigger or not, whichever is more convenient

ember ether
ember ether