use bevy_app::{App, Plugin, PostStartup, PostUpdate}; use bevy_ecs::schedule::{IntoSystemConfigs, IntoSystemSetConfigs, SystemSet}; use bevy_hierarchy::ValidParentCheckPlugin; use crate::{ components::GlobalTransform, systems::{propagate_transforms, sync_simple_transforms}, }; #[cfg(feature = "bevy_reflect")] use crate::components::Transform; /// Set enum for the systems relating to transform propagation #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)] pub enum TransformSystem { /// Propagates changes in transform to children's [`GlobalTransform`] TransformPropagate, } /// The base plugin for handling [`Transform`] components #[derive(Default)] pub struct TransformPlugin; impl Plugin for TransformPlugin { fn build(&self, app: &mut App) { // A set for `propagate_transforms` to mark it as ambiguous with `sync_simple_transforms`. // Used instead of the `SystemTypeSet` as that would not allow multiple instances of the system. #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)] struct PropagateTransformsSet; #[cfg(feature = "bevy_reflect")] app.register_type::() .register_type::(); app.add_plugins(ValidParentCheckPlugin::::default()) .configure_sets( PostStartup, PropagateTransformsSet.in_set(TransformSystem::TransformPropagate), ) // add transform systems to startup so the first update is "correct" .add_systems( PostStartup, ( sync_simple_transforms .in_set(TransformSystem::TransformPropagate) // FIXME: https://github.com/bevyengine/bevy/issues/4381 // These systems cannot access the same entities, // due to subtle query filtering that is not yet correctly computed in the ambiguity detector .ambiguous_with(PropagateTransformsSet), propagate_transforms.in_set(PropagateTransformsSet), ), ) .configure_sets( PostUpdate, PropagateTransformsSet.in_set(TransformSystem::TransformPropagate), ) .add_systems( PostUpdate, ( sync_simple_transforms .in_set(TransformSystem::TransformPropagate) .ambiguous_with(PropagateTransformsSet), propagate_transforms.in_set(PropagateTransformsSet), ), ); } }