diff --git a/CHANGELOG.md b/CHANGELOG.md index b861df1..56af404 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is inspired from [Keep a Changelog], and this project adheres to [Sem ### Breaking changes * The minimum supported rust version is now `1.57` +* The animation is now updated during the `CoreStage::Update` ## [0.3.1] - 2021-08-02 diff --git a/examples/end_of_animation_detection.rs b/examples/end_of_animation_detection.rs new file mode 100644 index 0000000..6d733af --- /dev/null +++ b/examples/end_of_animation_detection.rs @@ -0,0 +1,49 @@ +use std::time::Duration; + +use bevy::prelude::*; + +use benimator::*; + +fn main() { + App::build() + .add_plugins(DefaultPlugins) + .add_plugin(AnimationPlugin) + .add_startup_system(spawn_animated_coin.system()) + .add_startup_system(spawn_camera.system()) + .add_system_to_stage(CoreStage::PostUpdate, removal_detection.system()) + .run(); +} + +fn spawn_animated_coin( + mut commands: Commands, + asset_server: Res, + mut textures: ResMut>, + mut animations: ResMut>, +) { + let animation = + animations.add(SpriteSheetAnimation::from_range(0..=4, Duration::from_millis(100)).once()); + + commands + .spawn_bundle(SpriteSheetBundle { + texture_atlas: textures.add(TextureAtlas::from_grid( + asset_server.load("coin.png"), + Vec2::new(16.0, 16.0), + 5, + 1, + )), + transform: Transform::from_scale(Vec3::splat(10.0)), + ..Default::default() + }) + .insert(animation) + .insert(Play); +} + +fn removal_detection(removals: RemovedComponents) { + for entity in removals.iter() { + println!("Animation stopped for: {:?}", entity); + } +} + +fn spawn_camera(mut commands: Commands) { + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); +} diff --git a/src/lib.rs b/src/lib.rs index 9eaf1f0..097d08e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,8 +150,8 @@ pub struct Play; impl Plugin for AnimationPlugin { fn build(&self, app: &mut AppBuilder) { app.add_asset::() - .add_system_set(state::update_systems()) - .add_system_to_stage(CoreStage::PostUpdate, state::post_update_system()); + .add_system_set_to_stage(CoreStage::PreUpdate, state::maintenance_systems()) + .add_system_to_stage(CoreStage::Update, state::post_update_system()); #[cfg(feature = "warnings")] app.add_system_set(warnings::systems()); diff --git a/src/state.rs b/src/state.rs index 0f0a450..9aab968 100644 --- a/src/state.rs +++ b/src/state.rs @@ -8,7 +8,7 @@ use bevy_sprite::prelude::*; use crate::{AnimationMode, Play, SpriteSheetAnimation}; -pub(crate) fn update_systems() -> SystemSet { +pub(crate) fn maintenance_systems() -> SystemSet { SystemSet::new() .with_system(insert.system()) .with_system(remove.system())