-
-
Couldn't load subscription status.
- Fork 4.2k
[Merged by Bors] - add a SceneBundle to spawn a scene
#2424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9866682
add a bundle to spawn a scene
mockersf a13d089
remove instance from list on despawn
mockersf d00bff3
replace in option
mockersf 5d2a219
fix Parent and PreviousParent entity mapping
mockersf 3a556fd
add `DynamicSceneBundle`
mockersf 9e9d553
remove `SpawnScene` command
mockersf 971a61a
update for mandatory component derive
mockersf e423fac
new dependency for transforms
mockersf 9d420db
use new default
mockersf 45f3f26
Deref and DerefMut for HasSceneInstance
mockersf c4604d7
docs
mockersf 0405ed8
only delete once
mockersf 4eebccc
remove the option
mockersf 756d7fb
update example
mockersf dd855e0
more use of the SceneSpawner
mockersf 60828c5
other example to `SceneSpawner`
mockersf 9d2beb9
typo
mockersf f2c6cb7
rename systems as they don't really need a bundle anymore
mockersf 9a8121c
add basic doc on how to spawn a scene
mockersf 2acb18a
Use SceneBundle everywhere
cart e32238d
improve doc comment
mockersf d27c206
update comment
mockersf 0a28d05
more use of the bundle
mockersf 96d4ba7
merge the two systems
mockersf 2a1f993
update example
mockersf c3d4889
derive derefs
mockersf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| use bevy_asset::Handle; | ||
| use bevy_derive::{Deref, DerefMut}; | ||
| use bevy_ecs::{ | ||
| bundle::Bundle, | ||
| change_detection::ResMut, | ||
| entity::Entity, | ||
| prelude::{Changed, Component, Without}, | ||
| system::{Commands, Query}, | ||
| }; | ||
| use bevy_transform::components::{GlobalTransform, Transform}; | ||
|
|
||
| use crate::{DynamicScene, InstanceId, Scene, SceneSpawner}; | ||
|
|
||
| /// [`InstanceId`] of a spawned scene. It can be used with the [`SceneSpawner`] to | ||
| /// interact with the spawned scene. | ||
| #[derive(Component, Deref, DerefMut)] | ||
| pub struct SceneInstance(InstanceId); | ||
|
|
||
| /// A component bundle for a [`Scene`] root. | ||
| /// | ||
| /// The scene from `scene` will be spawn as a child of the entity with this component. | ||
| /// Once it's spawned, the entity will have a [`SceneInstance`] component. | ||
| #[derive(Default, Bundle)] | ||
| pub struct SceneBundle { | ||
mockersf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Handle to the scene to spawn | ||
| pub scene: Handle<Scene>, | ||
| pub transform: Transform, | ||
mockersf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub global_transform: GlobalTransform, | ||
| } | ||
|
|
||
| /// A component bundle for a [`DynamicScene`] root. | ||
| /// | ||
| /// The dynamic scene from `scene` will be spawn as a child of the entity with this component. | ||
| /// Once it's spawned, the entity will have a [`SceneInstance`] component. | ||
| #[derive(Default, Bundle)] | ||
| pub struct DynamicSceneBundle { | ||
| /// Handle to the scene to spawn | ||
| pub scene: Handle<DynamicScene>, | ||
| pub transform: Transform, | ||
| pub global_transform: GlobalTransform, | ||
| } | ||
|
|
||
| /// System that will spawn scenes from [`SceneBundle`]. | ||
| pub fn scene_spawner( | ||
mockersf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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>>, 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 { | ||
| scene_spawner.despawn_instance(**old_instance); | ||
| *old_instance = SceneInstance(new_instance); | ||
| } else { | ||
| commands.entity(entity).insert(SceneInstance(new_instance)); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.