bevy/crates/bevy_transform/src/hierarchy/hierarchy.rs

47 lines
1.3 KiB
Rust
Raw Normal View History

2020-07-16 23:32:39 +00:00
use crate::components::Children;
2020-07-10 04:18:35 +00:00
use bevy_ecs::{Entity, Query};
2020-01-15 06:23:00 +00:00
2020-07-10 04:18:35 +00:00
pub fn run_on_hierarchy<T, S>(
children_query: &Query<&Children>,
state: &mut S,
entity: Entity,
parent_result: Option<&mut T>,
mut previous_result: Option<T>,
2020-07-10 04:18:35 +00:00
run: &mut dyn FnMut(&mut S, Entity, Option<&mut T>, Option<T>) -> Option<T>,
) -> Option<T>
where
T: Clone,
{
// TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocations?
// TODO: the problem above might be resolvable with world splitting
2020-07-10 04:18:35 +00:00
let children = match children_query.get::<Children>(entity) {
Ok(children) => Some(
2020-01-13 06:18:17 +00:00
children
.0
2020-01-13 06:18:17 +00:00
.iter()
.map(|entity| *entity)
.collect::<Vec<Entity>>(),
),
2020-07-10 04:18:35 +00:00
Err(_) => None,
2020-01-13 06:18:17 +00:00
};
2020-07-10 04:18:35 +00:00
let mut parent_result = run(state, entity, parent_result, previous_result);
previous_result = None;
if let Some(children) = children {
for child in children {
2020-07-10 04:18:35 +00:00
previous_result = run_on_hierarchy(
children_query,
state,
child,
parent_result.as_mut(),
previous_result,
run,
);
2020-01-13 06:18:17 +00:00
}
} else {
previous_result = parent_result;
2020-01-13 06:18:17 +00:00
}
previous_result
2020-02-08 07:17:51 +00:00
}