@@ -12,8 +12,7 @@ use bevy_ecs::{
12
12
use smallvec:: SmallVec ;
13
13
14
14
fn push_move_events ( world : & mut World , events : SmallVec < [ ChildMoved ; 8 ] > ) {
15
- {
16
- let mut removed = world. resource_mut :: < Events < ChildRemoved > > ( ) ;
15
+ if let Some ( mut removed) = world. get_resource_mut :: < Events < ChildRemoved > > ( ) {
17
16
for evt in events. iter ( ) {
18
17
removed. send ( ChildRemoved {
19
18
child : evt. child ,
@@ -22,19 +21,21 @@ fn push_move_events(world: &mut World, events: SmallVec<[ChildMoved; 8]>) {
22
21
}
23
22
}
24
23
25
- let mut moved_events = world. resource_mut :: < Events < ChildMoved > > ( ) ;
26
- for evt in events {
27
- moved_events. send ( evt) ;
24
+ if let Some ( mut moved) = world. get_resource_mut :: < Events < ChildMoved > > ( ) {
25
+ for evt in events {
26
+ moved. send ( evt) ;
27
+ }
28
28
}
29
29
}
30
30
31
31
fn push_add_events ( world : & mut World , parent : Entity , children : & [ Entity ] ) {
32
- let mut added = world. resource_mut :: < Events < ChildAdded > > ( ) ;
33
- for child in children. iter ( ) {
34
- added. send ( ChildAdded {
35
- child : * child,
36
- parent,
37
- } ) ;
32
+ if let Some ( mut added) = world. get_resource_mut :: < Events < ChildAdded > > ( ) {
33
+ for child in children. iter ( ) {
34
+ added. send ( ChildAdded {
35
+ child : * child,
36
+ parent,
37
+ } ) ;
38
+ }
38
39
}
39
40
}
40
41
@@ -106,17 +107,19 @@ impl Command for AddChild {
106
107
}
107
108
if let Some ( previous) = previous {
108
109
remove_from_children ( world, previous, self . child ) ;
109
- world
110
- . resource_mut :: < Events < ChildRemoved > > ( )
111
- . send ( ChildRemoved {
110
+ if let Some ( mut removed) = world. get_resource_mut :: < Events < ChildRemoved > > ( ) {
111
+ removed. send ( ChildRemoved {
112
112
child : self . child ,
113
113
parent : previous,
114
114
} ) ;
115
- world. resource_mut :: < Events < ChildMoved > > ( ) . send ( ChildMoved {
116
- child : self . child ,
117
- previous_parent : previous,
118
- new_parent : self . parent ,
119
- } ) ;
115
+ }
116
+ if let Some ( mut removed) = world. get_resource_mut :: < Events < ChildMoved > > ( ) {
117
+ removed. send ( ChildMoved {
118
+ child : self . child ,
119
+ previous_parent : previous,
120
+ new_parent : self . parent ,
121
+ } ) ;
122
+ }
120
123
}
121
124
let mut parent = world. entity_mut ( self . parent ) ;
122
125
if let Some ( mut children) = parent. get_mut :: < Children > ( ) {
@@ -126,10 +129,12 @@ impl Command for AddChild {
126
129
} else {
127
130
parent. insert ( Children ( smallvec:: smallvec![ self . child] ) ) ;
128
131
}
129
- world. resource_mut :: < Events < ChildAdded > > ( ) . send ( ChildAdded {
130
- child : self . child ,
131
- parent : self . parent ,
132
- } ) ;
132
+ if let Some ( mut added) = world. get_resource_mut :: < Events < ChildAdded > > ( ) {
133
+ added. send ( ChildAdded {
134
+ child : self . child ,
135
+ parent : self . parent ,
136
+ } ) ;
137
+ }
133
138
}
134
139
}
135
140
@@ -190,8 +195,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
190
195
} ) ;
191
196
}
192
197
193
- {
194
- let mut removed = world. resource_mut :: < Events < ChildRemoved > > ( ) ;
198
+ if let Some ( mut removed) = world. get_resource_mut :: < Events < ChildRemoved > > ( ) {
195
199
for evt in events {
196
200
removed. send ( evt) ;
197
201
}
@@ -331,12 +335,12 @@ impl<'w> WorldChildBuilder<'w> {
331
335
. id ( ) ;
332
336
push_child_unchecked ( self . world , parent_entity, entity) ;
333
337
self . current_entity = Some ( entity) ;
334
- self . world
335
- . resource_mut :: < Events < ChildAdded > > ( )
336
- . send ( ChildAdded {
338
+ if let Some ( mut added) = self . world . get_resource_mut :: < Events < ChildAdded > > ( ) {
339
+ added. send ( ChildAdded {
337
340
child : entity,
338
341
parent : parent_entity,
339
342
} ) ;
343
+ }
340
344
self . world . entity_mut ( entity)
341
345
}
342
346
@@ -346,12 +350,12 @@ impl<'w> WorldChildBuilder<'w> {
346
350
let entity = self . world . spawn ( ) . insert ( Parent ( parent_entity) ) . id ( ) ;
347
351
push_child_unchecked ( self . world , parent_entity, entity) ;
348
352
self . current_entity = Some ( entity) ;
349
- self . world
350
- . resource_mut :: < Events < ChildAdded > > ( )
351
- . send ( ChildAdded {
353
+ if let Some ( mut added) = self . world . get_resource_mut :: < Events < ChildAdded > > ( ) {
354
+ added. send ( ChildAdded {
352
355
child : entity,
353
356
parent : parent_entity,
354
357
} ) ;
358
+ }
355
359
self . world . entity_mut ( entity)
356
360
}
357
361
@@ -510,11 +514,10 @@ impl<'w> BuildWorldChildren for WorldChildBuilder<'w> {
510
514
#[ cfg( test) ]
511
515
mod tests {
512
516
use super :: { BuildChildren , BuildWorldChildren } ;
513
- use crate :: prelude:: { ChildAdded , ChildMoved , ChildRemoved , Children , Parent } ;
517
+ use crate :: prelude:: { Children , Parent } ;
514
518
use bevy_ecs:: {
515
519
component:: Component ,
516
520
entity:: Entity ,
517
- event:: Events ,
518
521
system:: { CommandQueue , Commands } ,
519
522
world:: World ,
520
523
} ;
@@ -526,11 +529,6 @@ mod tests {
526
529
#[ test]
527
530
fn build_children ( ) {
528
531
let mut world = World :: default ( ) ;
529
-
530
- world. insert_resource ( Events :: < ChildAdded > :: default ( ) ) ;
531
- world. insert_resource ( Events :: < ChildRemoved > :: default ( ) ) ;
532
- world. insert_resource ( Events :: < ChildMoved > :: default ( ) ) ;
533
-
534
532
let mut queue = CommandQueue :: default ( ) ;
535
533
let mut commands = Commands :: new ( & mut queue, & world) ;
536
534
@@ -557,11 +555,6 @@ mod tests {
557
555
#[ test]
558
556
fn push_and_insert_and_remove_children_commands ( ) {
559
557
let mut world = World :: default ( ) ;
560
-
561
- world. insert_resource ( Events :: < ChildAdded > :: default ( ) ) ;
562
- world. insert_resource ( Events :: < ChildRemoved > :: default ( ) ) ;
563
- world. insert_resource ( Events :: < ChildMoved > :: default ( ) ) ;
564
-
565
558
let entities = world
566
559
. spawn_batch ( vec ! [ ( C ( 1 ) , ) , ( C ( 2 ) , ) , ( C ( 3 ) , ) , ( C ( 4 ) , ) , ( C ( 5 ) , ) ] )
567
560
. collect :: < Vec < Entity > > ( ) ;
@@ -625,11 +618,6 @@ mod tests {
625
618
#[ test]
626
619
fn push_and_insert_and_remove_children_world ( ) {
627
620
let mut world = World :: default ( ) ;
628
-
629
- world. insert_resource ( Events :: < ChildAdded > :: default ( ) ) ;
630
- world. insert_resource ( Events :: < ChildRemoved > :: default ( ) ) ;
631
- world. insert_resource ( Events :: < ChildMoved > :: default ( ) ) ;
632
-
633
621
let entities = world
634
622
. spawn_batch ( vec ! [ ( C ( 1 ) , ) , ( C ( 2 ) , ) , ( C ( 3 ) , ) , ( C ( 4 ) , ) , ( C ( 5 ) , ) ] )
635
623
. collect :: < Vec < Entity > > ( ) ;
@@ -678,11 +666,6 @@ mod tests {
678
666
#[ test]
679
667
fn regression_push_children_same_archetype ( ) {
680
668
let mut world = World :: new ( ) ;
681
-
682
- world. insert_resource ( Events :: < ChildAdded > :: default ( ) ) ;
683
- world. insert_resource ( Events :: < ChildRemoved > :: default ( ) ) ;
684
- world. insert_resource ( Events :: < ChildMoved > :: default ( ) ) ;
685
-
686
669
let child = world. spawn ( ) . id ( ) ;
687
670
world. spawn ( ) . push_children ( & [ child] ) ;
688
671
}
0 commit comments