@@ -160,6 +160,18 @@ impl Locals {
160160 pub fn translate ( & mut self , entity : Entity , delta : Vec3 ) {
161161 * self . translations . entry ( entity) . or_default ( ) += delta;
162162 }
163+
164+ fn clear ( & mut self , mut changed : impl FnMut ( Entity ) ) {
165+ let worlds = std:: mem:: take ( & mut self . worlds ) ;
166+ let translations = std:: mem:: take ( & mut self . translations ) ;
167+ for entity in std:: mem:: take ( & mut self . poses )
168+ . into_keys ( )
169+ . chain ( worlds. into_keys ( ) )
170+ . chain ( translations. into_keys ( ) )
171+ {
172+ changed ( entity) ;
173+ }
174+ }
163175}
164176
165177/// The same current local pose feeds attachment queries and final propagation.
@@ -284,7 +296,11 @@ impl Helper<'_, '_> {
284296
285297pub ( crate ) fn install ( app : & mut App ) {
286298 app. init_resource :: < Locals > ( )
287- . add_systems ( PreUpdate , clear)
299+ . add_systems (
300+ PreUpdate ,
301+ // Keep the outgoing pose until the battle transition hides the field.
302+ clear. run_if ( crate :: battle:: field_presenting. or_else ( field_camera_hidden) ) ,
303+ )
288304 . add_systems (
289305 PostUpdate ,
290306 propagate
@@ -295,19 +311,31 @@ pub(crate) fn install(app: &mut App) {
295311 ) ;
296312}
297313
314+ fn field_camera_hidden ( cameras : Query < & Camera , With < crate :: FieldCamera > > ) -> bool {
315+ !cameras. iter ( ) . any ( |camera| camera. is_active )
316+ }
317+
298318fn clear ( mut affine : ResMut < Locals > , mut transforms : Query < & mut Transform > ) {
299- let worlds = std:: mem:: take ( & mut affine. worlds ) ;
300- let translations = std:: mem:: take ( & mut affine. translations ) ;
301- for entity in std:: mem:: take ( & mut affine. poses )
302- . into_keys ( )
303- . chain ( worlds. into_keys ( ) )
304- . chain ( translations. into_keys ( ) )
305- {
319+ affine. clear ( |entity| {
306320 if let Ok ( mut transform) = transforms. get_mut ( entity) {
307321 // A dropped matrix clip must restore ordinary descendant globals too.
308322 transform. set_changed ( ) ;
309323 }
324+ } ) ;
325+ }
326+
327+ /// Battle disposal can resume field publication after this frame's PreUpdate.
328+ pub ( crate ) fn reset ( world : & mut World ) {
329+ if !world. contains_resource :: < Locals > ( ) {
330+ return ;
310331 }
332+ world. resource_scope ( |world, mut affine : Mut < Locals > | {
333+ affine. clear ( |entity| {
334+ if let Some ( mut transform) = world. get_mut :: < Transform > ( entity) {
335+ transform. set_changed ( ) ;
336+ }
337+ } ) ;
338+ } ) ;
311339}
312340
313341pub ( crate ) fn propagate (
@@ -349,6 +377,103 @@ mod tests {
349377 #[ derive( Resource ) ]
350378 struct Playback ( Option < f32 > ) ;
351379
380+ #[ test]
381+ fn battle_holds_the_outgoing_pose_until_the_field_camera_is_hidden ( ) {
382+ let mut app = App :: new ( ) ;
383+ app. add_plugins ( bevy:: transform:: TransformPlugin )
384+ . init_resource :: < crate :: loading:: Resident > ( ) ;
385+ install ( & mut app) ;
386+ let camera = app
387+ . world_mut ( )
388+ . spawn ( ( crate :: FieldCamera , Camera :: default ( ) ) )
389+ . id ( ) ;
390+ let bone = app. world_mut ( ) . spawn ( Transform :: IDENTITY ) . id ( ) ;
391+ app. update ( ) ;
392+
393+ let last_pose = GlobalTransform :: from_translation ( Vec3 :: Y ) ;
394+ app. world_mut ( )
395+ . resource_mut :: < Locals > ( )
396+ . set_world ( bone, last_pose) ;
397+ app. world_mut ( ) . run_schedule ( PostUpdate ) ;
398+ app. world ( )
399+ . resource :: < crate :: loading:: Resident > ( )
400+ . battle
401+ . store ( true , std:: sync:: atomic:: Ordering :: Release ) ;
402+ app. update ( ) ;
403+ assert_eq ! (
404+ * app. world( ) . get:: <GlobalTransform >( bone) . unwrap( ) ,
405+ last_pose
406+ ) ;
407+
408+ app. world_mut ( ) . get_mut :: < Camera > ( camera) . unwrap ( ) . is_active = false ;
409+ app. update ( ) ;
410+ assert_eq ! (
411+ * app. world( ) . get:: <GlobalTransform >( bone) . unwrap( ) ,
412+ GlobalTransform :: IDENTITY
413+ ) ;
414+ }
415+
416+ #[ test]
417+ fn reset_restores_all_overrides_before_same_frame_actor_retirement ( ) {
418+ let mut app = App :: new ( ) ;
419+ app. add_plugins ( bevy:: transform:: TransformPlugin ) ;
420+ install ( & mut app) ;
421+ let matrix_bone = app. world_mut ( ) . spawn ( Transform :: IDENTITY ) . id ( ) ;
422+ let world_bone = app
423+ . world_mut ( )
424+ . spawn ( Transform :: from_scale ( Vec3 :: ZERO ) )
425+ . id ( ) ;
426+ let translated_bone = app. world_mut ( ) . spawn ( Transform :: IDENTITY ) . id ( ) ;
427+ app. update ( ) ;
428+ let matrix = bevy:: math:: Affine3A :: from_mat4 ( Mat4 :: from_cols (
429+ Vec4 :: X ,
430+ Vec4 :: new ( 0.5 , 1. , 0. , 0. ) ,
431+ Vec4 :: Z ,
432+ Vec4 :: new ( 1. , 2. , 3. , 1. ) ,
433+ ) ) ;
434+ let held_world = GlobalTransform :: from_translation ( Vec3 :: new ( 10. , 20. , 30. ) ) ;
435+ let translation = Vec3 :: new ( 40. , 50. , 60. ) ;
436+ app. world_mut ( )
437+ . resource_scope ( |world, mut locals : Mut < Locals > | {
438+ locals. set (
439+ matrix_bone,
440+ & mut world. get_mut :: < Transform > ( matrix_bone) . unwrap ( ) ,
441+ Pose :: Affine ( matrix) ,
442+ ) ;
443+ locals. set_world ( world_bone, held_world) ;
444+ locals. translate ( translated_bone, translation) ;
445+ } ) ;
446+ app. world_mut ( ) . run_schedule ( PostUpdate ) ;
447+ for ( bone, expected) in [
448+ ( matrix_bone, matrix) ,
449+ ( world_bone, held_world. affine ( ) ) ,
450+ (
451+ translated_bone,
452+ Transform :: from_translation ( translation) . compute_affine ( ) ,
453+ ) ,
454+ ] {
455+ assert ! (
456+ app. world( )
457+ . get:: <GlobalTransform >( bone)
458+ . unwrap( )
459+ . affine( )
460+ . abs_diff_eq( expected, 0.0001 )
461+ ) ;
462+ }
463+ reset ( app. world_mut ( ) ) ;
464+ // Field return can retire actors before the next pose propagation.
465+ app. world_mut ( ) . despawn ( world_bone) ;
466+ // Disposal resumes this frame without another PreUpdate clear.
467+ app. world_mut ( ) . run_schedule ( PostUpdate ) ;
468+ for bone in [ matrix_bone, translated_bone] {
469+ assert_eq ! (
470+ * app. world( ) . get:: <GlobalTransform >( bone) . unwrap( ) ,
471+ GlobalTransform :: IDENTITY
472+ ) ;
473+ }
474+ reset ( & mut World :: new ( ) ) ;
475+ }
476+
352477 #[ test]
353478 fn platform_and_child_effect_receive_independent_world_translations ( ) {
354479 use bevy:: ecs:: system:: RunSystemOnce ;
0 commit comments