use crate::component::{Children, Parent};
use crate::entity::Entity;
use crate::world::World;
pub trait HierarchyExt {
fn despawn_recursive(&mut self, entity: Entity);
fn add_child(&mut self, parent: Entity, child: Entity);
fn remove_child(&mut self, parent: Entity, child: Entity);
}
impl HierarchyExt for World {
fn despawn_recursive(&mut self, entity: Entity) {
let mut children_to_despawn = Vec::new();
if let Some(children_ptr) = self.get_component_ptr(entity, std::any::TypeId::of::<Children>()) {
let children = unsafe { &*(children_ptr as *const Children) };
for &child_id in &children.0 {
if let Some(child_entity) = self.reconstruct_entity(child_id) {
children_to_despawn.push(child_entity);
}
}
}
if let Some(parent_ptr) = self.get_component_ptr(entity, std::any::TypeId::of::<Parent>()) {
let parent_id = unsafe { (*(parent_ptr as *const Parent)).0 };
if let Some(parent_entity) = self.reconstruct_entity(parent_id) {
self.remove_child(parent_entity, entity);
}
}
for child in children_to_despawn {
self.despawn_recursive(child);
}
self.despawn(entity);
}
fn add_child(&mut self, parent: Entity, child: Entity) {
if let Some(parent_ptr) = self.get_component_ptr(child, std::any::TypeId::of::<Parent>()) {
let old_parent_id = unsafe { (*(parent_ptr as *const Parent)).0 };
if old_parent_id != parent.id() {
if let Some(old_parent) = self.reconstruct_entity(old_parent_id) {
self.remove_child(old_parent, child);
}
}
}
self.add_component(child, Parent(parent.id()));
if let Some(children_ptr) = self.get_component_mut_ptr(parent, std::any::TypeId::of::<Children>()) {
let children = unsafe { &mut *(children_ptr as *mut Children) };
if !children.0.contains(&child.id()) {
children.0.push(child.id());
}
} else {
self.add_component(parent, Children(vec![child.id()]));
}
}
fn remove_child(&mut self, parent: Entity, child: Entity) {
self.remove_component::<Parent>(child);
if let Some(children_ptr) = self.get_component_mut_ptr(parent, std::any::TypeId::of::<Children>()) {
let children = unsafe { &mut *(children_ptr as *mut Children) };
children.0.retain(|&id| id != child.id());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::world::World;
#[test]
fn test_hierarchy_add_remove() {
let mut world = World::new();
let parent = world.spawn();
let child = world.spawn();
world.add_child(parent, child);
if let Some(parent_ptr) = world.get_component_ptr(child, std::any::TypeId::of::<Parent>()) {
let parent_id = unsafe { (*(parent_ptr as *const Parent)).0 };
assert_eq!(parent_id, parent.id());
} else {
panic!("Child missing Parent component");
}
if let Some(children_ptr) = world.get_component_ptr(parent, std::any::TypeId::of::<Children>()) {
let children = unsafe { &*(children_ptr as *const Children) };
assert_eq!(children.0.len(), 1);
assert_eq!(children.0[0], child.id());
} else {
panic!("Parent missing Children component");
}
world.remove_child(parent, child);
assert!(world.get_component_ptr(child, std::any::TypeId::of::<Parent>()).is_none());
if let Some(children_ptr) = world.get_component_ptr(parent, std::any::TypeId::of::<Children>()) {
let children = unsafe { &*(children_ptr as *const Children) };
assert_eq!(children.0.len(), 0);
}
}
#[test]
fn test_despawn_recursive() {
let mut world = World::new();
let p1 = world.spawn();
let c1 = world.spawn();
let c2 = world.spawn();
let gc1 = world.spawn();
world.add_child(p1, c1);
world.add_child(p1, c2);
world.add_child(c1, gc1);
assert_eq!(world.entity_count(), 4);
world.despawn_recursive(p1);
assert_eq!(world.entity_count(), 0);
}
}