bevy_tween icon indicating copy to clipboard operation
bevy_tween copied to clipboard

impl AnimationBuilderExt for EntityWorldMut / RelatedSpawner<ChildOf>

Open mirsella opened this issue 3 months ago • 1 comments

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

mirsella avatar Nov 03 '25 11:11 mirsella

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());
    }
}

mirsella avatar Nov 03 '25 17:11 mirsella