@@ -7,6 +7,7 @@ use bevy::{
77 ecs:: {
88 entity:: Entity ,
99 entity_disabling:: Disabled ,
10+ error:: Result ,
1011 lifecycle:: { HookContext , Insert , Replace } ,
1112 observer:: On ,
1213 query:: { Changed , Has , Or , With , Without } ,
@@ -21,7 +22,6 @@ use bevy::{
2122 } ,
2223 world:: { DeferredWorld , Mut , Ref , World } ,
2324 } ,
24- log:: warn,
2525 prelude:: { Deref , DerefMut } ,
2626 time:: Time ,
2727} ;
@@ -100,7 +100,7 @@ fn sleep_on_add_sleeping(mut world: DeferredWorld, ctx: HookContext) {
100100 return ;
101101 }
102102
103- world. commands ( ) . queue ( SleepBody ( ctx. entity ) ) ;
103+ world. commands ( ) . queue_silenced ( SleepBody ( ctx. entity ) ) ;
104104}
105105
106106fn wake_on_remove_sleeping ( mut world : DeferredWorld , ctx : HookContext ) {
@@ -119,7 +119,7 @@ fn wake_on_remove_sleeping(mut world: DeferredWorld, ctx: HookContext) {
119119 return ;
120120 }
121121
122- world. commands ( ) . queue ( WakeBody ( ctx. entity ) ) ;
122+ world. commands ( ) . queue_silenced ( WakeBody ( ctx. entity ) ) ;
123123}
124124
125125fn wake_on_replace_rigid_body (
@@ -293,46 +293,52 @@ struct CachedBodySleepingSystemState(
293293/// A [`Command`] that forces a [`RigidBody`] and its [`PhysicsIsland`][super::PhysicsIsland] to be [`Sleeping`].
294294pub struct SleepBody ( pub Entity ) ;
295295
296- impl Command for SleepBody {
297- fn apply ( self , world : & mut World ) {
298- if let Some ( island_id) = world
299- . get :: < BodyIslandNode > ( self . 0 )
300- . map ( |node| node. island_id )
301- {
302- world. try_resource_scope ( |world, mut state : Mut < CachedBodySleepingSystemState > | {
303- let (
304- mut body_islands,
305- body_colliders,
306- mut islands,
307- mut contact_graph,
308- mut joint_graph,
309- ) = state. 0 . get_mut ( world) ;
310-
311- let Some ( island) = islands. get_mut ( island_id) else {
312- return ;
313- } ;
314-
315- // The island must be split before it can be woken up.
316- // Note that this is expensive.
317- if island. constraints_removed > 0 {
318- islands. split_island (
319- island_id,
320- & mut body_islands,
321- & body_colliders,
322- & mut contact_graph,
323- & mut joint_graph,
324- ) ;
325- }
326-
327- // The ID of the body's island might have changed due to the split,
328- // so we need to retrieve it again.
329- let island_id = body_islands. get ( self . 0 ) . map ( |node| node. island_id ) . unwrap ( ) ;
296+ impl Command < Result > for SleepBody {
297+ fn apply ( self , world : & mut World ) -> Result {
298+ if let Ok ( entity) = world. get_entity ( self . 0 ) {
299+ if let Some ( island_id) = entity. get :: < BodyIslandNode > ( ) . map ( |node| node. island_id ) {
300+ world. try_resource_scope ( |world, mut state : Mut < CachedBodySleepingSystemState > | {
301+ let (
302+ mut body_islands,
303+ body_colliders,
304+ mut islands,
305+ mut contact_graph,
306+ mut joint_graph,
307+ ) = state. 0 . get_mut ( world) ;
308+
309+ let Some ( island) = islands. get_mut ( island_id) else {
310+ return ;
311+ } ;
312+
313+ // The island must be split before it can be woken up.
314+ // Note that this is expensive.
315+ if island. constraints_removed > 0 {
316+ islands. split_island (
317+ island_id,
318+ & mut body_islands,
319+ & body_colliders,
320+ & mut contact_graph,
321+ & mut joint_graph,
322+ ) ;
323+ }
330324
331- // Sleep the island.
332- SleepIslands ( vec ! [ island_id] ) . apply ( world) ;
333- } ) ;
325+ // The ID of the body's island might have changed due to the split,
326+ // so we need to retrieve it again.
327+ let island_id = body_islands. get ( self . 0 ) . map ( |node| node. island_id ) . unwrap ( ) ;
328+
329+ // Sleep the island.
330+ SleepIslands ( vec ! [ island_id] ) . apply ( world) ;
331+ } ) ;
332+ Ok ( ( ) )
333+ } else {
334+ Err ( format ! (
335+ "Tried to sleep entity {:?} that is not a body or does not belong to an island" ,
336+ self . 0
337+ )
338+ . into ( ) )
339+ }
334340 } else {
335- warn ! ( "Tried to sleep body {:?} that does not exist" , self . 0 ) ;
341+ Err ( format ! ( "Tried to sleep entity {:?} that does not exist" , self . 0 ) . into ( ) )
336342 }
337343 }
338344}
@@ -442,27 +448,25 @@ struct CachedIslandWakingSystemState(
442448/// A [`Command`] that wakes up a [`RigidBody`] and its [`PhysicsIsland`](super::PhysicsIsland) if it is [`Sleeping`].
443449pub struct WakeBody ( pub Entity ) ;
444450
445- impl Command for WakeBody {
446- fn apply ( self , world : & mut World ) {
447- if let Some ( body_island) = world. get :: < BodyIslandNode > ( self . 0 ) {
448- WakeIslands ( vec ! [ body_island. island_id] ) . apply ( world) ;
451+ impl Command < Result > for WakeBody {
452+ fn apply ( self , world : & mut World ) -> Result {
453+ if let Ok ( entity) = world. get_entity ( self . 0 ) {
454+ if let Some ( body_island) = entity. get :: < BodyIslandNode > ( ) {
455+ WakeIslands ( vec ! [ body_island. island_id] ) . apply ( world) ;
456+ Ok ( ( ) )
457+ } else {
458+ Err ( format ! (
459+ "Tried to wake entity {:?} that is not a body or does not belong to an island" ,
460+ self . 0
461+ )
462+ . into ( ) )
463+ }
449464 } else {
450- warn ! ( "Tried to wake body {:?} that does not exist" , self . 0 ) ;
465+ Err ( format ! ( "Tried to wake entity {:?} that does not exist" , self . 0 ) . into ( ) )
451466 }
452467 }
453468}
454469
455- /// A deprecated alias for [`WakeBody`].
456- #[ deprecated( since = "0.4.0" , note = "Renamed to `WakeBody`." ) ]
457- pub struct WakeUpBody ( pub Entity ) ;
458-
459- #[ expect( deprecated) ]
460- impl Command for WakeUpBody {
461- fn apply ( self , world : & mut World ) {
462- WakeBody ( self . 0 ) . apply ( world) ;
463- }
464- }
465-
466470/// A [`Command`] that wakes up the [`PhysicsIsland`](super::PhysicsIsland)s with the given IDs if they are sleeping.
467471pub struct WakeIslands ( pub Vec < IslandId > ) ;
468472
0 commit comments