@@ -2,20 +2,11 @@ use crate::{Children, HierarchyEvent, Parent};
2
2
use bevy_ecs:: {
3
3
bundle:: Bundle ,
4
4
entity:: Entity ,
5
- event:: Events ,
6
5
system:: { Command , Commands , EntityCommands } ,
7
6
world:: { EntityMut , World } ,
8
7
} ;
9
8
use smallvec:: SmallVec ;
10
9
11
- fn push_events ( world : & mut World , events : SmallVec < [ HierarchyEvent ; 8 ] > ) {
12
- if let Some ( mut moved) = world. get_resource_mut :: < Events < HierarchyEvent > > ( ) {
13
- for evt in events {
14
- moved. send ( evt) ;
15
- }
16
- }
17
- }
18
-
19
10
fn push_child_unchecked ( world : & mut World , parent : Entity , child : Entity ) {
20
11
let mut parent = world. entity_mut ( parent) ;
21
12
if let Some ( mut children) = parent. get_mut :: < Children > ( ) {
@@ -64,7 +55,7 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
64
55
} ) ;
65
56
}
66
57
}
67
- push_events ( world, moved) ;
58
+ world. send_event_batch ( moved) ;
68
59
}
69
60
70
61
fn remove_children ( parent : Entity , children : & [ Entity ] , world : & mut World ) {
@@ -83,7 +74,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
83
74
world. entity_mut ( child) . remove :: < Parent > ( ) ;
84
75
}
85
76
}
86
- push_events ( world, events) ;
77
+ world. send_event_batch ( events) ;
87
78
88
79
let mut parent = world. entity_mut ( parent) ;
89
80
if let Some ( mut parent_children) = parent. get_mut :: < Children > ( ) {
@@ -114,19 +105,16 @@ impl Command for AddChild {
114
105
return ;
115
106
}
116
107
remove_from_children ( world, previous, self . child ) ;
117
- if let Some ( mut events) = world. get_resource_mut :: < Events < HierarchyEvent > > ( ) {
118
- events. send ( HierarchyEvent :: ChildMoved {
119
- child : self . child ,
120
- previous_parent : previous,
121
- new_parent : self . parent ,
122
- } ) ;
123
- }
124
- } else if let Some ( mut events) = world. get_resource_mut :: < Events < HierarchyEvent > > ( ) {
125
- events. send ( HierarchyEvent :: ChildAdded {
108
+ world. send_event ( HierarchyEvent :: ChildMoved {
126
109
child : self . child ,
127
- parent : self . parent ,
110
+ previous_parent : previous,
111
+ new_parent : self . parent ,
128
112
} ) ;
129
113
}
114
+ world. send_event ( HierarchyEvent :: ChildAdded {
115
+ child : self . child ,
116
+ parent : self . parent ,
117
+ } ) ;
130
118
let mut parent = world. entity_mut ( self . parent ) ;
131
119
if let Some ( mut children) = parent. get_mut :: < Children > ( ) {
132
120
if !children. contains ( & self . child ) {
@@ -202,12 +190,10 @@ impl Command for RemoveParent {
202
190
let parent_entity = parent. get ( ) ;
203
191
remove_from_children ( world, parent_entity, self . child ) ;
204
192
world. entity_mut ( self . child ) . remove :: < Parent > ( ) ;
205
- if let Some ( mut events) = world. get_resource_mut :: < Events < _ > > ( ) {
206
- events. send ( HierarchyEvent :: ChildRemoved {
207
- child : self . child ,
208
- parent : parent_entity,
209
- } ) ;
210
- }
193
+ world. send_event ( HierarchyEvent :: ChildRemoved {
194
+ child : self . child ,
195
+ parent : parent_entity,
196
+ } ) ;
211
197
}
212
198
}
213
199
}
@@ -354,25 +340,21 @@ impl<'w> WorldChildBuilder<'w> {
354
340
pub fn spawn ( & mut self , bundle : impl Bundle + Send + Sync + ' static ) -> EntityMut < ' _ > {
355
341
let entity = self . world . spawn ( ( bundle, Parent ( self . parent ) ) ) . id ( ) ;
356
342
push_child_unchecked ( self . world , self . parent , entity) ;
357
- if let Some ( mut added) = self . world . get_resource_mut :: < Events < HierarchyEvent > > ( ) {
358
- added. send ( HierarchyEvent :: ChildAdded {
359
- child : entity,
360
- parent : self . parent ,
361
- } ) ;
362
- }
343
+ self . world . send_event ( HierarchyEvent :: ChildAdded {
344
+ child : entity,
345
+ parent : self . parent ,
346
+ } ) ;
363
347
self . world . entity_mut ( entity)
364
348
}
365
349
366
350
/// Spawns an [`Entity`] with no components and inserts it into the children defined by the [`WorldChildBuilder`] which adds the [`Parent`] component to it.
367
351
pub fn spawn_empty ( & mut self ) -> EntityMut < ' _ > {
368
352
let entity = self . world . spawn ( Parent ( self . parent ) ) . id ( ) ;
369
353
push_child_unchecked ( self . world , self . parent , entity) ;
370
- if let Some ( mut added) = self . world . get_resource_mut :: < Events < HierarchyEvent > > ( ) {
371
- added. send ( HierarchyEvent :: ChildAdded {
372
- child : entity,
373
- parent : self . parent ,
374
- } ) ;
375
- }
354
+ self . world . send_event ( HierarchyEvent :: ChildAdded {
355
+ child : entity,
356
+ parent : self . parent ,
357
+ } ) ;
376
358
self . world . entity_mut ( entity)
377
359
}
378
360
0 commit comments