Skip to content

Commit

Permalink
merge the two systems
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jun 6, 2022
1 parent 0a28d05 commit 96d4ba7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
41 changes: 17 additions & 24 deletions crates/bevy_scene/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_ecs::{
bundle::Bundle,
change_detection::ResMut,
entity::Entity,
prelude::{Changed, Component},
prelude::{Changed, Component, Without},
system::{Commands, Query},
};
use bevy_transform::components::{GlobalTransform, Transform};
Expand Down Expand Up @@ -43,26 +43,6 @@ pub struct SceneBundle {
pub global_transform: GlobalTransform,
}

/// System that will spawn scenes from [`SceneBundle`].
pub fn scene_spawner(
mut commands: Commands,
mut scene_to_spawn: Query<
(Entity, &Handle<Scene>, Option<&mut SceneInstance>),
Changed<Handle<Scene>>,
>,
mut scene_spawner: ResMut<SceneSpawner>,
) {
for (entity, scene, instance) in scene_to_spawn.iter_mut() {
let new_instance = scene_spawner.spawn_as_child(scene.clone(), entity);
if let Some(mut old_instance) = instance {
scene_spawner.despawn_instance(**old_instance);
*old_instance = SceneInstance(new_instance);
} else {
commands.entity(entity).insert(SceneInstance(new_instance));
}
}
}

/// A component bundle for a [`DynamicScene`] root.
///
/// The dynamic scene from `scene` will be spawn as a child of the entity with this component.
Expand All @@ -75,15 +55,28 @@ pub struct DynamicSceneBundle {
pub global_transform: GlobalTransform,
}

/// System that will spawn scenes from [`DynamicSceneBundle`].
pub fn dynamic_scene_spawner(
/// System that will spawn scenes from [`SceneBundle`].
pub fn scene_spawner(
mut commands: Commands,
mut scene_to_spawn: Query<
(Entity, &Handle<Scene>, Option<&mut SceneInstance>),
(Changed<Handle<Scene>>, Without<Handle<DynamicScene>>),
>,
mut dynamic_scene_to_spawn: Query<
(Entity, &Handle<DynamicScene>, Option<&mut SceneInstance>),
Changed<Handle<DynamicScene>>,
(Changed<Handle<DynamicScene>>, Without<Handle<Scene>>),
>,
mut scene_spawner: ResMut<SceneSpawner>,
) {
for (entity, scene, instance) in scene_to_spawn.iter_mut() {
let new_instance = scene_spawner.spawn_as_child(scene.clone(), entity);
if let Some(mut old_instance) = instance {
scene_spawner.despawn_instance(**old_instance);
*old_instance = SceneInstance(new_instance);
} else {
commands.entity(entity).insert(SceneInstance(new_instance));
}
}
for (entity, dynamic_scene, instance) in dynamic_scene_to_spawn.iter_mut() {
let new_instance = scene_spawner.spawn_dynamic_as_child(dynamic_scene.clone(), entity);
if let Some(mut old_instance) = instance {
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_scene/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl Plugin for ScenePlugin {
scene_spawner_system.exclusive_system().at_end(),
)
// Systems `*_bundle_spawner` must run before `scene_spawner_system`
.add_system_to_stage(CoreStage::PreUpdate, scene_spawner)
.add_system_to_stage(CoreStage::PreUpdate, dynamic_scene_spawner);
.add_system_to_stage(CoreStage::PreUpdate, scene_spawner);
}
}

0 comments on commit 96d4ba7

Please sign in to comment.