mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 11:03:06 +00:00
513be52505
# Objective Needing to derive `AnimationEvent` for `Event` is unnecessary, and the trigger logic coupled to it feels like we're coupling "event producer" logic with the event itself, which feels wrong. It also comes with a bunch of complexity, which is again unnecessary. We can have the flexibility of "custom animation event trigger logic" without this coupling and complexity. The current `animation_events` example is also needlessly complicated, due to it needing to work around system ordering issues. The docs describing it are also slightly wrong. We can make this all a non-issue by solving the underlying ordering problem. Related to this, we use the `bevy_animation::Animation` system set to solve PostUpdate animation order-of-operations issues. If we move this to bevy_app as part of our "core schedule", we can cut out needless `bevy_animation` crate dependencies in these instances. ## Solution - Remove `AnimationEvent`, the derive, and all other infrastructure associated with it (such as the `bevy_animation/derive` crate) - Replace all instances of `AnimationEvent` traits with `Event + Clone` - Store and use functions for custom animation trigger logic (ex: `clip.add_event_fn()`). For "normal" cases users dont need to think about this and should use the simpler `clip.add_event()` - Run the `Animation` system set _before_ updating text - Move `bevy_animation::Animation` to `bevy_app::Animation`. Remove unnecessary `bevy_animation` dependency from `bevy_ui` - Adjust `animation_events` example to use the simpler `clip.add_event` API, as the workarounds are no longer necessary This is polishing work that will land in 0.15, and I think it is simple enough and valuable enough to land in 0.15 with it, in the interest of making the feature as compelling as possible.
104 lines
2.5 KiB
Rust
104 lines
2.5 KiB
Rust
//! Demonstrate how to use animation events.
|
|
|
|
use bevy::{
|
|
color::palettes::css::{ALICE_BLUE, BLACK, CRIMSON},
|
|
core_pipeline::bloom::Bloom,
|
|
prelude::*,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_event::<MessageEvent>()
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, animate_text_opacity)
|
|
.add_observer(edit_message)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct MessageText;
|
|
|
|
#[derive(Event, Clone)]
|
|
struct MessageEvent {
|
|
value: String,
|
|
color: Color,
|
|
}
|
|
|
|
fn edit_message(
|
|
trigger: Trigger<MessageEvent>,
|
|
text: Single<(&mut Text2d, &mut TextColor), With<MessageText>>,
|
|
) {
|
|
let (mut text, mut color) = text.into_inner();
|
|
text.0 = trigger.event().value.clone();
|
|
color.0 = trigger.event().color;
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut animations: ResMut<Assets<AnimationClip>>,
|
|
mut graphs: ResMut<Assets<AnimationGraph>>,
|
|
) {
|
|
// Camera
|
|
commands.spawn((
|
|
Camera2d,
|
|
Camera {
|
|
clear_color: ClearColorConfig::Custom(BLACK.into()),
|
|
hdr: true,
|
|
..Default::default()
|
|
},
|
|
Bloom {
|
|
intensity: 0.4,
|
|
..Bloom::NATURAL
|
|
},
|
|
));
|
|
|
|
// The text that will be changed by animation events.
|
|
commands.spawn((
|
|
MessageText,
|
|
Text2d::default(),
|
|
TextFont {
|
|
font_size: 119.0,
|
|
..default()
|
|
},
|
|
TextColor(Color::NONE),
|
|
));
|
|
|
|
// Create a new animation clip.
|
|
let mut animation = AnimationClip::default();
|
|
|
|
// This is only necessary if you want the duration of the
|
|
// animation to be longer than the last event in the clip.
|
|
animation.set_duration(2.0);
|
|
|
|
// Add events at the specified time.
|
|
animation.add_event(
|
|
0.0,
|
|
MessageEvent {
|
|
value: "HELLO".into(),
|
|
color: ALICE_BLUE.into(),
|
|
},
|
|
);
|
|
animation.add_event(
|
|
1.0,
|
|
MessageEvent {
|
|
value: "BYE".into(),
|
|
color: CRIMSON.into(),
|
|
},
|
|
);
|
|
|
|
// Create the animation graph.
|
|
let (graph, animation_index) = AnimationGraph::from_clip(animations.add(animation));
|
|
let mut player = AnimationPlayer::default();
|
|
player.play(animation_index).repeat();
|
|
|
|
commands.spawn((AnimationGraphHandle(graphs.add(graph)), player));
|
|
}
|
|
|
|
// Slowly fade out the text opacity.
|
|
fn animate_text_opacity(mut colors: Query<&mut TextColor>, time: Res<Time>) {
|
|
for mut color in &mut colors {
|
|
let a = color.0.alpha();
|
|
color.0.set_alpha(a - time.delta_secs());
|
|
}
|
|
}
|