@@ -2,20 +2,11 @@ use crate::{Children, HierarchyEvent, Parent};
22use bevy_ecs:: {
33 bundle:: Bundle ,
44 entity:: Entity ,
5- event:: Events ,
65 system:: { Command , Commands , EntityCommands } ,
76 world:: { EntityMut , World } ,
87} ;
98use smallvec:: SmallVec ;
109
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-
1910fn push_child_unchecked ( world : & mut World , parent : Entity , child : Entity ) {
2011 let mut parent = world. entity_mut ( parent) ;
2112 if let Some ( mut children) = parent. get_mut :: < Children > ( ) {
@@ -64,7 +55,7 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
6455 } ) ;
6556 }
6657 }
67- push_events ( world, moved) ;
58+ world. send_event_batch ( moved) ;
6859}
6960
7061fn remove_children ( parent : Entity , children : & [ Entity ] , world : & mut World ) {
@@ -83,7 +74,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
8374 world. entity_mut ( child) . remove :: < Parent > ( ) ;
8475 }
8576 }
86- push_events ( world, events) ;
77+ world. send_event_batch ( events) ;
8778
8879 let mut parent = world. entity_mut ( parent) ;
8980 if let Some ( mut parent_children) = parent. get_mut :: < Children > ( ) {
@@ -114,19 +105,16 @@ impl Command for AddChild {
114105 return ;
115106 }
116107 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 {
126109 child : self . child ,
127- parent : self . parent ,
110+ previous_parent : previous,
111+ new_parent : self . parent ,
128112 } ) ;
129113 }
114+ world. send_event ( HierarchyEvent :: ChildAdded {
115+ child : self . child ,
116+ parent : self . parent ,
117+ } ) ;
130118 let mut parent = world. entity_mut ( self . parent ) ;
131119 if let Some ( mut children) = parent. get_mut :: < Children > ( ) {
132120 if !children. contains ( & self . child ) {
@@ -202,12 +190,10 @@ impl Command for RemoveParent {
202190 let parent_entity = parent. get ( ) ;
203191 remove_from_children ( world, parent_entity, self . child ) ;
204192 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+ } ) ;
211197 }
212198 }
213199}
@@ -354,25 +340,21 @@ impl<'w> WorldChildBuilder<'w> {
354340 pub fn spawn ( & mut self , bundle : impl Bundle + Send + Sync + ' static ) -> EntityMut < ' _ > {
355341 let entity = self . world . spawn ( ( bundle, Parent ( self . parent ) ) ) . id ( ) ;
356342 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+ } ) ;
363347 self . world . entity_mut ( entity)
364348 }
365349
366350 /// Spawns an [`Entity`] with no components and inserts it into the children defined by the [`WorldChildBuilder`] which adds the [`Parent`] component to it.
367351 pub fn spawn_empty ( & mut self ) -> EntityMut < ' _ > {
368352 let entity = self . world . spawn ( Parent ( self . parent ) ) . id ( ) ;
369353 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+ } ) ;
376358 self . world . entity_mut ( entity)
377359 }
378360
0 commit comments