bevy_tween
bevy_tween copied to clipboard
impl AnimationBuilderExt for EntityWorldMut / RelatedSpawner<ChildOf>
hello !
this would allow easily adding animations when using the Children component (and so children![] macro) use case:
let bundle = (..., Children::spawn_with(|mut p| {
p.animation() // impl on RelatedSpawner
p.spawn_empty() // animator as child, using EntityWorldMut impl that this spawn_empty return
});
and what about directly a component that insert the animation ? instead of using entity commands
did that:
use bevy::{
ecs::{component::HookContext, world::DeferredWorld},
prelude::*,
};
use bevy_tween::prelude::*;
pub trait AnimFn = Fn(bevy_tween::combinator::AnimationBuilder) + Send + Sync + 'static;
#[derive(Component)]
#[component(storage = "SparseSet", on_add = Self::on_add)]
pub struct AnimationBuilder<T: AnimFn>(pub T);
impl<T: AnimFn> AnimationBuilder<T> {
fn on_add(mut world: DeferredWorld, context: HookContext) {
let (entities, mut commands) = world.entities_and_commands();
let inner = entities
.get(context.entity)
.unwrap()
.get::<AnimationBuilder<T>>()
.unwrap();
let mut ec = commands.entity(context.entity);
inner.0(ec.animation());
}
}