Skip to content
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

Hierarchy #713

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Sync previous parent in parent_update_system
  • Loading branch information
svents committed Oct 21, 2020
commit b30876f4df64a0387fa6aef795b199dce232e61f
60 changes: 25 additions & 35 deletions crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ use bevy_ecs::{Commands, Entity, IntoQuerySystem, Query, System, Without};
use bevy_utils::HashMap;
use smallvec::SmallVec;

pub fn missing_previous_parent_system(
mut commands: Commands,
mut query: Query<Without<PreviousParent, (Entity, &Parent)>>,
) {
// Add missing `PreviousParent` components
for (entity, _parent) in &mut query.iter() {
log::trace!("Adding missing PreviousParent to {:?}", entity);
commands.insert_one(entity, PreviousParent(None));
}
}

pub fn parent_update_system(
mut commands: Commands,
mut removed_parent_query: Query<Without<Parent, (Entity, &PreviousParent)>>,
// TODO: ideally this only runs when the Parent component has changed
mut changed_parent_query: Query<(Entity, &Parent, &mut PreviousParent)>,
mut changed_parent_query: Query<(Entity, &Parent, Option<&mut PreviousParent>)>,
children_query: Query<&mut Children>,
) {
// Entities with a missing `Parent` (ie. ones that have a `PreviousParent`), remove
Expand All @@ -39,28 +28,32 @@ pub fn parent_update_system(
let mut children_additions = HashMap::<Entity, SmallVec<[Entity; 8]>>::default();

// Entities with a changed Parent (that also have a PreviousParent, even if None)
for (entity, parent, mut previous_parent) in &mut changed_parent_query.iter() {
for (entity, parent, possible_previous_parent) in &mut changed_parent_query.iter() {
log::trace!("Parent changed for {:?}", entity);

// If the `PreviousParent` is not None.
if let Some(previous_parent_entity) = previous_parent.0 {
// New and previous point to the same Entity, carry on, nothing to see here.
if previous_parent_entity == parent.0 {
log::trace!(" > But the previous parent is the same, ignoring...");
continue;
if let Some(mut previous_parent) = possible_previous_parent {
// If the `PreviousParent` is not None.
if let Some(previous_parent_entity) = previous_parent.0 {
// New and previous point to the same Entity, carry on, nothing to see here.
if previous_parent_entity == parent.0 {
log::trace!(" > But the previous parent is the same, ignoring...");
continue;
}

// Remove from `PreviousParent.Children`.
if let Ok(mut previous_parent_children) =
children_query.get_mut::<Children>(previous_parent_entity)
{
log::trace!(" > Removing {:?} from prev parent's children", entity);
(*previous_parent_children).0.retain(|e| *e != entity);
}
}

// Remove from `PreviousParent.Children`.
if let Ok(mut previous_parent_children) =
children_query.get_mut::<Children>(previous_parent_entity)
{
log::trace!(" > Removing {:?} from prev parent's children", entity);
(*previous_parent_children).0.retain(|e| *e != entity);
}
}

// Set `PreviousParent = Parent`.
*previous_parent = PreviousParent(Some(parent.0));
// Set `PreviousParent = Parent`.
*previous_parent = PreviousParent(Some(parent.0));
} else {
log::trace!("Adding missing PreviousParent to {:?}", entity);
commands.insert_one(entity, PreviousParent(Some(parent.0)));
};

// Add to the parent's `Children` (either the real component, or
// `children_additions`).
Expand Down Expand Up @@ -99,10 +92,7 @@ pub fn parent_update_system(
}

pub fn hierarchy_maintenance_systems() -> Vec<Box<dyn System>> {
vec![
missing_previous_parent_system.system(),
parent_update_system.system(),
]
vec![parent_update_system.system()]
}

#[cfg(test)]
Expand Down