//! Player sprite animation. //! This is based on multiple examples and may be very different for your game. //! - [Sprite flipping](https://github.com/bevyengine/bevy/blob/latest/examples/2d/sprite_flipping.rs) //! - [Sprite animation](https://github.com/bevyengine/bevy/blob/latest/examples/2d/sprite_animation.rs) //! - [Timers](https://github.com/bevyengine/bevy/blob/latest/examples/time/timers.rs) use bevy::prelude::*; use rand::prelude::*; use std::time::Duration; use crate::{ audio::SoundEffect, demo::{movement::MovementController, player::PlayerAssets}, AppSet, }; pub(super) fn plugin(app: &mut App) { // Animate and play sound effects based on controls. app.register_type::(); app.add_systems( Update, ( update_animation_timer.in_set(AppSet::TickTimers), ( update_animation_movement, update_animation_atlas, trigger_step_sound_effect, ) .chain() .run_if(resource_exists::) .in_set(AppSet::Update), ), ); } /// Update the sprite direction and animation state (idling/walking). fn update_animation_movement( mut player_query: Query<(&MovementController, &mut Sprite, &mut PlayerAnimation)>, ) { for (controller, mut sprite, mut animation) in &mut player_query { let dx = controller.intent.x; if dx != 0.0 { sprite.flip_x = dx < 0.0; } let animation_state = if controller.intent == Vec2::ZERO { PlayerAnimationState::Idling } else { PlayerAnimationState::Walking }; animation.update_state(animation_state); } } /// Update the animation timer. fn update_animation_timer(time: Res