Skip to content

Commit

Permalink
feat: add opt-out support for bevy_eventlistener (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
musjj authored Apr 17, 2024
1 parent eac77aa commit 4351cc5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ resolver = "2"

[dependencies]
bevy = { version = "0.13.0", default-features = false }
bevy_eventlistener = { version = "0.7.0", optional = true }

[dev-dependencies]
bevy-inspector-egui = "0.23"
Expand All @@ -30,7 +31,8 @@ default = [
"span_tween",
"bevy_asset",
"bevy_render",
"bevy_sprite"
"bevy_sprite",
"bevy_eventlistener"
]

# Tweener implementation by defining a tween in range of time.
Expand All @@ -42,6 +44,8 @@ bevy_asset = [ "bevy/bevy_asset" ]
bevy_render = [ "bevy/bevy_render"]
# Add some built-in interpolator related to sprite
bevy_sprite = [ "bevy/bevy_sprite", "bevy_render" ]
# Add entity-targeted events with bevy_eventlistener
bevy_eventlistener = [ "dep:bevy_eventlistener" ]

[package.metadata.docs.rs]
all-features = true
Expand Down Expand Up @@ -108,6 +112,14 @@ required-features = [
"span_tween",
]

[[example]]
name = "entity_event"
path = "examples/demo/entity_event.rs"
required-features = [
"span_tween",
"bevy_eventlistener"
]

[[example]]
name = "sprite_sheet"
path = "examples/demo/sprite_sheet.rs"
Expand Down
24 changes: 24 additions & 0 deletions examples/demo/entity_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use bevy::prelude::*;
use bevy_tween::prelude::*;
use bevy_eventlistener::prelude::*;

fn main() {
App::new()
.add_plugins((MinimalPlugins, DefaultTweenPlugins))
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn((
SpanTweenerBundle::new(Duration::from_secs_f32(0.5))
.with_repeat(Repeat::times(5)),
On::<SpanTweenerEnded>::run(|listener: Listener<SpanTweenerEnded>| {
if listener.is_completed() {
println!("done!");
} else {
println!("repeating");
}
})
));
}
8 changes: 8 additions & 0 deletions src/span_tween.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ use std::{cmp::Ordering, ops, time::Duration};

use crate::utils;
use bevy::{ecs::system::EntityCommands, prelude::*};
#[cfg(feature = "bevy_eventlistener")]
use bevy_eventlistener::prelude::*;
use tween_timer::{Repeat, RepeatStyle};

use crate::{
Expand Down Expand Up @@ -152,6 +154,9 @@ impl Plugin for SpanTweenPlugin {
.register_type::<TimeBound>()
.register_type::<TweenTimeSpan>()
.add_event::<SpanTweenerEnded>();

#[cfg(feature = "bevy_eventlistener")]
app.add_plugins(EventListenerPlugin::<SpanTweenerEnded>::default());
}
}

Expand Down Expand Up @@ -642,9 +647,12 @@ pub fn span_tween(duration: Duration) -> QuickSpanTweenBundle {
}

/// Fired when a span tweener repeated or completed
#[cfg_attr(feature = "bevy_eventlistener", derive(EntityEvent))]
#[cfg_attr(feature = "bevy_eventlistener", can_bubble)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Event, Reflect)]
pub struct SpanTweenerEnded {
/// Tween timer that just ended
#[cfg_attr(feature = "bevy_eventlistener", target)]
pub tweener: Entity,
/// Currently timer direction. If is [`RepeatStyle::PingPong`], the current
/// direction will be its already changed direction.
Expand Down

0 comments on commit 4351cc5

Please sign in to comment.