@@ -6,15 +6,15 @@ use std::ops::Deref;
6
6
7
7
use bevy_app:: { App , CoreStage , Plugin } ;
8
8
use bevy_asset:: { AddAsset , Assets , Handle } ;
9
- use bevy_core:: Name ;
9
+ use bevy_core:: { EntityPath , Name , NameLookup } ;
10
10
use bevy_ecs:: {
11
11
change_detection:: DetectChanges ,
12
12
entity:: Entity ,
13
13
prelude:: Component ,
14
+ query:: QueryEntityError ,
14
15
reflect:: ReflectComponent ,
15
16
schedule:: ParallelSystemDescriptorCoercion ,
16
17
system:: { Query , Res , SystemParam } ,
17
- query:: QueryEntityError ,
18
18
} ;
19
19
use bevy_hierarchy:: Children ;
20
20
use bevy_math:: { Quat , Vec3 } ;
@@ -26,94 +26,9 @@ use bevy_utils::{tracing::warn, HashMap};
26
26
#[ allow( missing_docs) ]
27
27
pub mod prelude {
28
28
#[ doc( hidden) ]
29
- pub use crate :: {
30
- AnimationClip , AnimationPlayer , AnimationPlugin , EntityPath , Keyframes , VariableCurve ,
31
- } ;
32
- }
33
-
34
- /// System param to enable entity lookup of an entity via EntityPath
35
- #[ derive( SystemParam ) ]
36
- pub struct NameLookup < ' w , ' s > {
37
- named : Query < ' w , ' s , ( Entity , & ' static Name ) > ,
38
- children : Query < ' w , ' s , & ' static Children > ,
39
- }
40
-
41
- /// Errors when looking up an entity by name
42
- pub enum LookupError {
43
- /// An entity could not be found, this either means the entity has been
44
- /// despawned, or the entity doesn't have the required components
45
- Query ( QueryEntityError ) ,
46
- /// The root node does not have the corrent name
47
- // TODO: add expected / found name
48
- RootNotFound ,
49
- /// A child was not found
50
- // TODO: add expected name
51
- ChildNotFound ,
52
- /// The name does not uniquely identify an entity
53
- // TODO: add name
54
- NameNotUnique ,
55
- }
56
-
57
- impl From < QueryEntityError > for LookupError {
58
- fn from ( q : QueryEntityError ) -> Self {
59
- Self :: Query ( q)
60
- }
61
- }
62
-
63
- impl < ' w , ' s > NameLookup < ' w , ' s > {
64
- /// Find an entity by entity path, may return an error if the root name isn't unique
65
- pub fn lookup_any ( & self , path : & EntityPath ) -> Result < Entity , LookupError > {
66
- let mut path = path. parts . iter ( ) ;
67
- let root_name = path. next ( ) . unwrap ( ) ;
68
- let mut root = None ;
69
- for ( entity, name) in self . named . iter ( ) {
70
- if root_name == name {
71
- if root. is_some ( ) {
72
- return Err ( LookupError :: NameNotUnique ) ;
73
- }
74
- root = Some ( entity) ;
75
- }
76
- }
77
- let mut current_node = root. ok_or ( LookupError :: RootNotFound ) ?;
78
- for part in path {
79
- current_node = self . find_child ( current_node, part) ?;
80
- }
81
- Ok ( current_node)
82
- }
83
-
84
- /// Find an entity by the root & entity path
85
- pub fn lookup ( & self , root : Entity , path : & EntityPath ) -> Result < Entity , LookupError > {
86
- let mut path = path. parts . iter ( ) ;
87
- let ( _, root_name) = self . named . get ( root) ?;
88
- if root_name != path. next ( ) . unwrap ( ) {
89
- return Err ( LookupError :: RootNotFound ) ;
90
- }
91
- let mut current_node = root;
92
- for part in path {
93
- current_node = self . find_child ( current_node, part) ?;
94
- }
95
- Ok ( current_node)
96
- }
97
-
98
- /// Internal function to get the child of `current_node` that has the name `part`
99
- fn find_child ( & self , current_node : Entity , part : & Name ) -> Result < Entity , LookupError > {
100
- let children = self . children . get ( current_node) ?;
101
- let mut ret = Err ( LookupError :: ChildNotFound ) ;
102
- for child in children {
103
- if let Ok ( ( _, name) ) = self . named . get ( * child) {
104
- if name == part {
105
- if ret. is_ok ( ) {
106
- return Err ( LookupError :: NameNotUnique ) ;
107
- }
108
- ret = Ok ( * child) ;
109
- }
110
- }
111
- }
112
- ret
113
- }
29
+ pub use crate :: { AnimationClip , AnimationPlayer , AnimationPlugin , Keyframes , VariableCurve } ;
114
30
}
115
31
116
-
117
32
/// List of keyframes for one of the attribute of a [`Transform`].
118
33
#[ derive( Clone , Debug ) ]
119
34
pub enum Keyframes {
@@ -136,13 +51,6 @@ pub struct VariableCurve {
136
51
pub keyframes : Keyframes ,
137
52
}
138
53
139
- /// Path to an entity, with [`Name`]s. Each entity in a path must have a name.
140
- #[ derive( Clone , Debug , Hash , PartialEq , Eq , Default ) ]
141
- pub struct EntityPath {
142
- /// Parts of the path
143
- pub parts : Vec < Name > ,
144
- }
145
-
146
54
/// A list of [`VariableCurve`], and the [`EntityPath`] to which they apply.
147
55
#[ derive( Clone , TypeUuid , Debug , Default ) ]
148
56
#[ uuid = "d81b7179-0448-4eb0-89fe-c067222725bf" ]
@@ -263,9 +171,8 @@ pub fn animation_player(
263
171
time : Res < Time > ,
264
172
animations : Res < Assets < AnimationClip > > ,
265
173
mut animation_players : Query < ( Entity , & mut AnimationPlayer ) > ,
266
- names : Query < & Name > ,
267
174
mut transforms : Query < & mut Transform > ,
268
- children : Query < & Children > ,
175
+ lookup : NameLookup ,
269
176
) {
270
177
for ( entity, mut player) in & mut animation_players {
271
178
if let Some ( animation_clip) = animations. get ( & player. animation_clip ) {
@@ -284,29 +191,15 @@ pub fn animation_player(
284
191
if elapsed < 0.0 {
285
192
elapsed += animation_clip. duration ;
286
193
}
287
- ' entity : for ( path, curves) in & animation_clip. curves {
194
+ for ( path, curves) in & animation_clip. curves {
288
195
// PERF: finding the target entity can be optimised
289
- let mut current_entity = entity;
290
- // Ignore the first name, it is the root node which we already have
291
- for part in path. parts . iter ( ) . skip ( 1 ) {
292
- let mut found = false ;
293
- if let Ok ( children) = children. get ( current_entity) {
294
- for child in children. deref ( ) {
295
- if let Ok ( name) = names. get ( * child) {
296
- if name == part {
297
- // Found a children with the right name, continue to the next part
298
- current_entity = * child;
299
- found = true ;
300
- break ;
301
- }
302
- }
303
- }
196
+ let current_entity = match lookup. lookup ( entity, path) {
197
+ Ok ( e) => e,
198
+ Err ( e) => {
199
+ warn ! ( "Entity for path {path:?} was not found" ) ;
200
+ continue ;
304
201
}
305
- if !found {
306
- warn ! ( "Entity not found for path {:?} on part {:?}" , path, part) ;
307
- continue ' entity;
308
- }
309
- }
202
+ } ;
310
203
if let Ok ( mut transform) = transforms. get_mut ( current_entity) {
311
204
for curve in curves {
312
205
// Some curves have only one keyframe used to set a transform
0 commit comments