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,
|
2020-01-17 09:29:01 +00:00
|
|
|
entity: Entity,
|
2020-07-25 06:04:45 +00:00
|
|
|
parent_result: Option<T>,
|
2020-06-25 17:13:00 +00:00
|
|
|
mut previous_result: Option<T>,
|
2020-07-25 06:04:45 +00:00
|
|
|
run: &mut dyn FnMut(&mut S, Entity, Option<T>, Option<T>) -> Option<T>,
|
2020-07-10 04:18:35 +00:00
|
|
|
) -> Option<T>
|
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
{
|
2020-05-06 20:49:07 +00:00
|
|
|
// TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocations?
|
2020-06-25 17:13:00 +00:00
|
|
|
// 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
|
2020-07-10 08:49:23 +00:00
|
|
|
.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-25 06:04:45 +00:00
|
|
|
let parent_result = run(state, entity, parent_result, previous_result);
|
2020-06-25 17:13:00 +00:00
|
|
|
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,
|
2020-06-25 17:13:00 +00:00
|
|
|
child,
|
2020-07-25 06:04:45 +00:00
|
|
|
parent_result.clone(),
|
2020-06-25 17:13:00 +00:00
|
|
|
previous_result,
|
|
|
|
run,
|
|
|
|
);
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
2020-05-06 20:49:07 +00:00
|
|
|
} else {
|
2020-06-25 17:13:00 +00:00
|
|
|
previous_result = parent_result;
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
2020-06-25 17:13:00 +00:00
|
|
|
|
|
|
|
previous_result
|
2020-02-08 07:17:51 +00:00
|
|
|
}
|