diff --git a/crates/bevy_animation/src/graph.rs b/crates/bevy_animation/src/graph.rs index 6121ecb2ab8a2..aca3220855908 100644 --- a/crates/bevy_animation/src/graph.rs +++ b/crates/bevy_animation/src/graph.rs @@ -7,11 +7,14 @@ use std::io::{self, Write}; use bevy_asset::{ io::Reader, Asset, AssetEvent, AssetId, AssetLoader, AssetPath, Assets, Handle, LoadContext, }; +use bevy_derive::{Deref, DerefMut}; use bevy_ecs::{ + component::Component, event::EventReader, + reflect::ReflectComponent, system::{Res, ResMut, Resource}, }; -use bevy_reflect::{Reflect, ReflectSerialize}; +use bevy_reflect::{prelude::ReflectDefault, Reflect, ReflectSerialize}; use bevy_utils::HashMap; use petgraph::{ graph::{DiGraph, NodeIndex}, @@ -122,6 +125,29 @@ pub struct AnimationGraph { pub mask_groups: HashMap, } +/// A [`Handle`] to the [`AnimationGraph`] to be used by the [`AnimationPlayer`](crate::AnimationPlayer) on the same entity. +#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect, PartialEq, Eq)] +#[reflect(Component, Default)] +pub struct AnimationGraphHandle(pub Handle); + +impl From> for AnimationGraphHandle { + fn from(handle: Handle) -> Self { + Self(handle) + } +} + +impl From for AssetId { + fn from(handle: AnimationGraphHandle) -> Self { + handle.id() + } +} + +impl From<&AnimationGraphHandle> for AssetId { + fn from(handle: &AnimationGraphHandle) -> Self { + handle.id() + } +} + /// A type alias for the `petgraph` data structure that defines the animation /// graph. pub type AnimationDiGraph = DiGraph; diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index c4d54d7f5fa34..e608f57583d37 100755 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -28,10 +28,10 @@ use core::{ use graph::AnimationNodeType; use prelude::AnimationCurveEvaluator; -use crate::graph::ThreadedAnimationGraphs; +use crate::graph::{AnimationGraphHandle, ThreadedAnimationGraphs}; use bevy_app::{App, Plugin, PostUpdate}; -use bevy_asset::{Asset, AssetApp, Assets, Handle}; +use bevy_asset::{Asset, AssetApp, Assets}; use bevy_core::Name; use bevy_ecs::{ entity::{VisitEntities, VisitEntitiesMut}, @@ -942,7 +942,7 @@ fn trigger_untargeted_animation_events( mut commands: Commands, clips: Res>, graphs: Res>, - players: Query<(Entity, &AnimationPlayer, &Handle)>, + players: Query<(Entity, &AnimationPlayer, &AnimationGraphHandle)>, ) { for (entity, player, graph_id) in &players { // The graph might not have loaded yet. Safely bail. @@ -989,7 +989,7 @@ pub fn advance_animations( time: Res