@@ -336,14 +336,11 @@ impl<'a> EntityCommands<'a> {
336
336
///
337
337
/// See [`add_one_related`](Self::add_one_related) if you want relate only one entity.
338
338
pub fn add_related < R : Relationship > ( & mut self , related : & [ Entity ] ) -> & mut Self {
339
- let id = self . id ( ) ;
340
- let related = related. to_vec ( ) ;
341
- self . commands ( ) . queue ( move |world : & mut World | {
342
- for related in related {
343
- world. entity_mut ( related) . insert ( R :: from ( id) ) ;
344
- }
345
- } ) ;
346
- self
339
+ let related: Box < [ Entity ] > = related. into ( ) ;
340
+
341
+ self . queue ( move |mut entity : EntityWorldMut | {
342
+ entity. add_related :: < R > ( & related) ;
343
+ } )
347
344
}
348
345
349
346
/// Relates the given entity to this with the relation `R`.
@@ -355,14 +352,11 @@ impl<'a> EntityCommands<'a> {
355
352
356
353
/// Replaces all the related entities with the given set of new related entities.
357
354
pub fn replace_related < R : Relationship > ( & mut self , related : & [ Entity ] ) -> & mut Self {
358
- let id = self . id ( ) ;
359
355
let related: Box < [ Entity ] > = related. into ( ) ;
360
356
361
- self . commands ( ) . queue ( move |world : & mut World | {
362
- world. entity_mut ( id) . replace_related :: < R > ( & related) ;
363
- } ) ;
364
-
365
- self
357
+ self . queue ( move |mut entity : EntityWorldMut | {
358
+ entity. replace_related :: < R > ( & related) ;
359
+ } )
366
360
}
367
361
368
362
/// Replaces all the related entities with a new set of entities.
@@ -381,30 +375,25 @@ impl<'a> EntityCommands<'a> {
381
375
entities_to_relate : & [ Entity ] ,
382
376
newly_related_entities : & [ Entity ] ,
383
377
) -> & mut Self {
384
- let id = self . id ( ) ;
385
378
let entities_to_unrelate: Box < [ Entity ] > = entities_to_unrelate. into ( ) ;
386
379
let entities_to_relate: Box < [ Entity ] > = entities_to_relate. into ( ) ;
387
380
let newly_related_entities: Box < [ Entity ] > = newly_related_entities. into ( ) ;
388
381
389
- self . commands ( ) . queue ( move |world : & mut World | {
390
- world . entity_mut ( id ) . replace_children_with_difference (
382
+ self . queue ( move |mut entity : EntityWorldMut | {
383
+ entity . replace_children_with_difference (
391
384
& entities_to_unrelate,
392
385
& entities_to_relate,
393
386
& newly_related_entities,
394
387
) ;
395
- } ) ;
396
-
397
- self
388
+ } )
398
389
}
399
390
400
391
/// Despawns entities that relate to this one via the given [`RelationshipTarget`].
401
392
/// This entity will not be despawned.
402
393
pub fn despawn_related < S : RelationshipTarget > ( & mut self ) -> & mut Self {
403
- let id = self . id ( ) ;
404
- self . commands . queue ( move |world : & mut World | {
405
- world. entity_mut ( id) . despawn_related :: < S > ( ) ;
406
- } ) ;
407
- self
394
+ self . queue ( move |mut entity : EntityWorldMut | {
395
+ entity. despawn_related :: < S > ( ) ;
396
+ } )
408
397
}
409
398
410
399
/// Inserts a component or bundle of components into the entity and all related entities,
@@ -418,11 +407,9 @@ impl<'a> EntityCommands<'a> {
418
407
& mut self ,
419
408
bundle : impl Bundle + Clone ,
420
409
) -> & mut Self {
421
- let id = self . id ( ) ;
422
- self . commands . queue ( move |world : & mut World | {
423
- world. entity_mut ( id) . insert_recursive :: < S > ( bundle) ;
424
- } ) ;
425
- self
410
+ self . queue ( move |mut entity : EntityWorldMut | {
411
+ entity. insert_recursive :: < S > ( bundle) ;
412
+ } )
426
413
}
427
414
428
415
/// Removes a component or bundle of components of type `B` from the entity and all related entities,
@@ -433,11 +420,9 @@ impl<'a> EntityCommands<'a> {
433
420
/// This method should only be called on relationships that form a tree-like structure.
434
421
/// Any cycles will cause this method to loop infinitely.
435
422
pub fn remove_recursive < S : RelationshipTarget , B : Bundle > ( & mut self ) -> & mut Self {
436
- let id = self . id ( ) ;
437
- self . commands . queue ( move |world : & mut World | {
438
- world. entity_mut ( id) . remove_recursive :: < S , B > ( ) ;
439
- } ) ;
440
- self
423
+ self . queue ( move |mut entity : EntityWorldMut | {
424
+ entity. remove_recursive :: < S , B > ( ) ;
425
+ } )
441
426
}
442
427
}
443
428
0 commit comments