bevy_transform/
plugins.rs

1use bevy_app::{App, Plugin, PostStartup, PostUpdate};
2use bevy_ecs::schedule::{IntoSystemConfigs, IntoSystemSetConfigs, SystemSet};
3use bevy_hierarchy::ValidParentCheckPlugin;
4
5use crate::{
6    prelude::{GlobalTransform, Transform},
7    systems::{propagate_transforms, sync_simple_transforms},
8};
9
10/// Set enum for the systems relating to transform propagation
11#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
12pub enum TransformSystem {
13    /// Propagates changes in transform to children's [`GlobalTransform`]
14    TransformPropagate,
15}
16
17/// The base plugin for handling [`Transform`] components
18#[derive(Default)]
19pub struct TransformPlugin;
20
21impl Plugin for TransformPlugin {
22    fn build(&self, app: &mut App) {
23        // A set for `propagate_transforms` to mark it as ambiguous with `sync_simple_transforms`.
24        // Used instead of the `SystemTypeSet` as that would not allow multiple instances of the system.
25        #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
26        struct PropagateTransformsSet;
27
28        app.register_type::<Transform>()
29            .register_type::<GlobalTransform>()
30            .add_plugins(ValidParentCheckPlugin::<GlobalTransform>::default())
31            .configure_sets(
32                PostStartup,
33                PropagateTransformsSet.in_set(TransformSystem::TransformPropagate),
34            )
35            // add transform systems to startup so the first update is "correct"
36            .add_systems(
37                PostStartup,
38                (
39                    sync_simple_transforms
40                        .in_set(TransformSystem::TransformPropagate)
41                        // FIXME: https://github.com/bevyengine/bevy/issues/4381
42                        // These systems cannot access the same entities,
43                        // due to subtle query filtering that is not yet correctly computed in the ambiguity detector
44                        .ambiguous_with(PropagateTransformsSet),
45                    propagate_transforms.in_set(PropagateTransformsSet),
46                ),
47            )
48            .configure_sets(
49                PostUpdate,
50                PropagateTransformsSet.in_set(TransformSystem::TransformPropagate),
51            )
52            .add_systems(
53                PostUpdate,
54                (
55                    sync_simple_transforms
56                        .in_set(TransformSystem::TransformPropagate)
57                        .ambiguous_with(PropagateTransformsSet),
58                    propagate_transforms.in_set(PropagateTransformsSet),
59                ),
60            );
61    }
62}