1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
use specs::{Component, Entity, FlaggedStorage, VecStorage, World}; use specs_hierarchy::Hierarchy; use std::borrow::Cow; pub fn hierarchy_find(mut root: Entity, path: &str, world: &World) -> Option<Entity> { let hierarchy = world.read_resource::<HierarchyRes>(); let names = world.read_storage::<Name>(); for part in path.split('/') { match part { "" | "." => {} ".." => { if let Some(parent) = hierarchy.parent(root) { root = parent; } else { return None; } } part => { let found = hierarchy.children(root).iter().find_map(|child| { if let Some(name) = names.get(*child) { if name.0 == part { Some(child) } else { None } } else { None } }); if let Some(child) = found { root = *child; } else { return None; } } } } Some(root) } pub type HierarchyRes = Hierarchy<Parent>; #[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)] pub struct Parent(pub Entity); impl Component for Parent { type Storage = FlaggedStorage<Self, VecStorage<Self>>; } impl specs_hierarchy::Parent for Parent { fn parent_entity(&self) -> Entity { self.0 } } #[derive(Debug, Clone)] pub struct Tag(pub Cow<'static, str>); impl Component for Tag { type Storage = VecStorage<Self>; } #[derive(Debug, Clone)] pub struct Name(pub Cow<'static, str>); impl Component for Name { type Storage = VecStorage<Self>; }