2020-01-18 23:36:24 +00:00
|
|
|
mod entity_builder;
|
|
|
|
mod archetypes;
|
|
|
|
|
|
|
|
pub use entity_builder::*;
|
|
|
|
pub use archetypes::*;
|
|
|
|
|
2020-01-15 06:23:00 +00:00
|
|
|
use crate::prelude::{Children, Entity, SubWorld, World};
|
|
|
|
|
2020-01-13 06:18:17 +00:00
|
|
|
pub fn run_on_hierarchy<T>(
|
2020-01-17 09:29:01 +00:00
|
|
|
world: &World,
|
|
|
|
entity: Entity,
|
|
|
|
input: T,
|
|
|
|
func: &mut dyn FnMut(&World, Entity, T) -> Option<T>,
|
|
|
|
) where
|
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
let result = func(world, entity, input);
|
|
|
|
|
|
|
|
if let Some(result) = result {
|
|
|
|
match world.get_component::<Children>(entity) {
|
|
|
|
Some(children) => Some(
|
|
|
|
for child in children.iter() {
|
|
|
|
run_on_hierarchy(world, *child, result, func);
|
|
|
|
}
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_on_hierarchy_mut<T>(
|
2020-01-13 06:18:17 +00:00
|
|
|
world: &mut World,
|
|
|
|
entity: Entity,
|
|
|
|
input: T,
|
|
|
|
func: &mut dyn FnMut(&mut World, Entity, T) -> Option<T>,
|
|
|
|
) where
|
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
// TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocactions?
|
|
|
|
let children = match world.get_component::<Children>(entity) {
|
|
|
|
Some(children) => Some(
|
|
|
|
children
|
|
|
|
.iter()
|
|
|
|
.map(|entity| *entity)
|
|
|
|
.collect::<Vec<Entity>>(),
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = func(world, entity, input);
|
|
|
|
|
|
|
|
if let Some(result) = result {
|
|
|
|
if let Some(children) = children {
|
|
|
|
for child in children {
|
2020-01-17 09:29:01 +00:00
|
|
|
run_on_hierarchy_mut(world, child, result, func);
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_on_hierarchy_subworld<T>(
|
2020-01-17 09:29:01 +00:00
|
|
|
world: &SubWorld,
|
2020-01-13 06:18:17 +00:00
|
|
|
entity: Entity,
|
|
|
|
input: T,
|
2020-01-17 09:29:01 +00:00
|
|
|
func: &mut dyn FnMut(&SubWorld, Entity, T) -> Option<T>,
|
|
|
|
) where
|
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
let result = func(world, entity, input);
|
|
|
|
|
|
|
|
if let Some(result) = result {
|
|
|
|
match world.get_component::<Children>(entity) {
|
|
|
|
Some(children) => Some(
|
|
|
|
for child in children.iter() {
|
|
|
|
run_on_hierarchy_subworld(world, *child, result, func);
|
|
|
|
}
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_on_hierarchy_subworld_mut<T>(
|
|
|
|
world: &mut SubWorld,
|
|
|
|
entity: Entity,
|
|
|
|
input: T,
|
|
|
|
func: &mut dyn FnMut(&mut SubWorld, Entity, T) -> Option<T>,
|
2020-01-13 06:18:17 +00:00
|
|
|
) where
|
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
// TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocactions?
|
|
|
|
let children = match world.get_component::<Children>(entity) {
|
|
|
|
Some(children) => Some(
|
|
|
|
children
|
|
|
|
.iter()
|
|
|
|
.map(|entity| *entity)
|
|
|
|
.collect::<Vec<Entity>>(),
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = func(world, entity, input);
|
|
|
|
|
|
|
|
if let Some(result) = result {
|
|
|
|
if let Some(children) = children {
|
|
|
|
for child in children {
|
2020-01-17 09:29:01 +00:00
|
|
|
run_on_hierarchy_subworld_mut(world, child, result, func);
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 09:29:01 +00:00
|
|
|
}
|