Skip to content

Commit

Permalink
feat!: Update animation during the CoreStage::Update stage (#14)
Browse files Browse the repository at this point in the history
So that we can detect end-of-animation during the post-update stage.

Co-authored-by: John Teske <john.teske@protonmail.com>
  • Loading branch information
jcornaz and johnteske committed Dec 4, 2021
1 parent dbed43c commit 2bcee87
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions 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<AssetServer>,
mut textures: ResMut<Assets<TextureAtlas>>,
mut animations: ResMut<Assets<SpriteSheetAnimation>>,
) {
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<Play>) {
for entity in removals.iter() {
println!("Animation stopped for: {:?}", entity);
}
}

fn spawn_camera(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
}
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -150,8 +150,8 @@ pub struct Play;
impl Plugin for AnimationPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_asset::<SpriteSheetAnimation>()
.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());
Expand Down
2 changes: 1 addition & 1 deletion src/state.rs
Expand Up @@ -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())
Expand Down

0 comments on commit 2bcee87

Please sign in to comment.