Skip to content

Commit

Permalink
feat!: make the integration with bevy_app optional and disabled by …
Browse files Browse the repository at this point in the history
…default (#73)

This change makes the integration with `bevy_app` optional and disabled by default.

The bevy plugin implementation is available only if this optional dependency is enabled.

To enable this integration (and make the bevy plugin available) one must enable the `bevy-app-07` feature flag.

This marks the beginning of the development of the next major version of benimator (version 4.0.0), for which I will try to make all bevy dependencies entirely optional so that I untie benimator's API stability from bevy's.

See the [related decision](https://github.com/jcornaz/benimator/blob/main/doc/adr/0002-optional-bevy-dependency.md) for more information.
  • Loading branch information
jcornaz committed Jul 13, 2022
1 parent cb54d07 commit 68ec6d9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 39 deletions.
38 changes: 33 additions & 5 deletions .github/workflows/build.yml
Expand Up @@ -17,7 +17,22 @@ env:
RUSTFLAGS: "-D warnings"

jobs:
test:
test-no-feature:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v3
- name: Install rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ env.RUST_VERSION }}
override: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- run: cargo check --all-targets --no-default-features
- run: cargo test --no-default-features

test-default-features:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
Expand All @@ -44,11 +59,20 @@ jobs:
override: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- run: cargo check --all-targets --all-features
- run: cargo test --all-features

test-unstable-load-from-file:
test-feature:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
feature:
- "unstable-load-from-file, yaml"
- "unstable-load-from-file, ron"
- "bevy-07"
- "bevy-app-07"

steps:
- uses: actions/checkout@v3
- name: Install rust toolchain
Expand All @@ -58,8 +82,8 @@ jobs:
override: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- run: cargo test --features "unstable-load-from-file, yaml"
- run: cargo test --features "unstable-load-from-file, ron"
- run: cargo check --all-targets --no-default-features --features "${{ matrix.feature }}"
- run: cargo test --no-default-features --features "${{ matrix.feature }}"

code-style:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -117,7 +141,11 @@ jobs:
release:
if: ${{ github.event_name != 'pull_request' }}
needs:
[test, test-all-features, test-unstable-load-from-file, documentation]
- test-no-feature
- test-default-features
- test-all-features
- test-feature
- documentation
environment: release
runs-on: ubuntu-latest
steps:
Expand Down
22 changes: 20 additions & 2 deletions Cargo.toml
Expand Up @@ -16,11 +16,12 @@ all-features = true
[features]
default = []
unstable-load-from-file = ["serde", "anyhow", "bevy_utils"]
bevy-07 = ["bevy-app-07"]

[dependencies]
bevy_core = { version = "0.7.0", default-features = false }
bevy_ecs = { version = "0.7.0", default-features = false }
bevy_app = { version = "0.7.0", default-features = false }
bevy-app-07 = { package = "bevy_app",version = "0.7.0", default-features = false, optional = true }
bevy_reflect = { version = "0.7.0", default-features = false }
bevy_sprite = { version = "0.7.0", default-features = false }
bevy_asset = { version = "0.7.0", default-features = false }
Expand All @@ -41,7 +42,24 @@ rustc_version = "0.4.0"
[[bench]]
name = "play_component"
harness = false
required-features = ["bevy-07"]

[[example]]
name = "change_animation"
required-features = ["bevy-07"]

[[example]]
name = "end_of_animation_detection"
required-features = ["bevy-07"]

[[example]]
name = "ping_pong"
required-features = ["bevy-07"]

[[example]]
name = "readme"
required-features = ["bevy-07"]

[[example]]
name = "using_animation_file"
required-features = ["unstable-load-from-file", "yaml"]
required-features = ["unstable-load-from-file", "yaml", "bevy-07"]
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -82,6 +82,8 @@ cargo add benimator

* `yaml` deserialization from yaml asset files (also requires `unstable-load-from-file`)
* `ron` deserialization from ron asset files (also requires `unstable-load-from-file`)
* `bevy-07` all integrations with bevy 0.7
* `bevy-app-07` integration with `bevy_app` 0.7 (incl. bevy plugin)

### Unstable features

Expand Down
61 changes: 40 additions & 21 deletions src/lib.rs
Expand Up @@ -14,22 +14,27 @@
//!
//! 1. Add the [`AnimationPlugin`] plugin
//!
//! ```no_run
//! use std::time::Duration;
//! use bevy::prelude::*;
//! use benimator::*;
//!
//! fn main() {
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_plugin(AnimationPlugin::default()) // <-- Enable sprite-sheet animations
//! .add_startup_system(spawn.system())
//! // ...
//! .run()
//! }
#![cfg_attr(
feature = "bevy-app-07",
doc = "
```no_run
# use bevy::prelude::*;
use benimator::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(AnimationPlugin::default()) // <-- Enable sprite-sheet animations
.add_startup_system(spawn)
// ...
.run()
}
fn spawn() { /* ... */ }
```
"
)]
//!
//! fn spawn() { /* ... */ }
//! ```
//!
//! 2. Create a [`SpriteSheetAnimation`] and insert the asset handle to the sprite sheet entity you want to animate
//!
Expand Down Expand Up @@ -148,8 +153,6 @@
#[macro_use]
extern crate rstest;

use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::component::SparseStorage;
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
Expand All @@ -169,6 +172,7 @@ mod state;
///
/// See crate level documentation for usage
#[non_exhaustive]
#[cfg(feature = "bevy-app-07")]
#[derive(Default)]
pub struct AnimationPlugin;

Expand Down Expand Up @@ -230,17 +234,32 @@ impl From<f32> for PlaySpeedMultiplier {
}
}

impl Plugin for AnimationPlugin {
fn build(&self, app: &mut App) {
#[cfg(feature = "bevy-app-07")]
impl bevy_app_07::Plugin for AnimationPlugin {
fn build(&self, app: &mut bevy_app_07::App) {
use bevy_asset::AddAsset;

app.add_asset::<SpriteSheetAnimation>()
.add_system_set_to_stage(CoreStage::PreUpdate, state::maintenance_systems())
.add_system_set_to_stage(CoreStage::Update, state::post_update_systems());
.add_system_set_to_stage(bevy_app_07::CoreStage::PreUpdate, maintenance_systems())
.add_system_set_to_stage(bevy_app_07::CoreStage::Update, animation_systems());

#[cfg(feature = "unstable-load-from-file")]
app.init_asset_loader::<animation::load::SpriteSheetAnimationLoader>();
}
}

/// System set that automatically insert and remove components (i.e. [`SpriteSheetAnimationState`])
pub fn maintenance_systems() -> SystemSet {
SystemSet::new()
.with_system(state::insert)
.with_system(state::remove)
}

/// System set that animate the sprite-sheets
pub fn animation_systems() -> SystemSet {
SystemSet::new().with_system(state::animate)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 3 additions & 11 deletions src/state.rs
Expand Up @@ -8,14 +8,6 @@ use bevy_sprite::prelude::*;

use crate::{animation::Mode, Play, PlaySpeedMultiplier, SpriteSheetAnimation};

pub(crate) fn maintenance_systems() -> SystemSet {
SystemSet::new().with_system(insert).with_system(remove)
}

pub(crate) fn post_update_systems() -> SystemSet {
SystemSet::new().with_system(animate)
}

/// Animation state component which is automatically inserted/removed
///
/// It can be used to reset the animation state.
Expand Down Expand Up @@ -114,7 +106,7 @@ impl SpriteSheetAnimationState {
}
}

fn insert(
pub(crate) fn insert(
mut commands: Commands<'_, '_>,
query: Query<
'_,
Expand All @@ -133,7 +125,7 @@ fn insert(
}
}

fn remove(
pub(crate) fn remove(
mut commands: Commands<'_, '_>,
removed: RemovedComponents<'_, Handle<SpriteSheetAnimation>>,
) {
Expand All @@ -152,7 +144,7 @@ type AnimationSystemQuery<'a> = (
Option<&'a PlaySpeedMultiplier>,
);

fn animate(
pub(crate) fn animate(
mut commands: Commands<'_, '_>,
time: Res<'_, Time>,
animation_defs: Res<'_, Assets<SpriteSheetAnimation>>,
Expand Down
2 changes: 2 additions & 0 deletions tests/spec.rs → tests/bevy_spec.rs
@@ -1,3 +1,5 @@
#![cfg(feature = "bevy-07")]

use std::time::Duration;

use bevy::asset::AssetPlugin;
Expand Down

0 comments on commit 68ec6d9

Please sign in to comment.