31 个版本
0.12.0 | 2024年1月3日 |
---|---|
0.11.7 | 2022年7月3日 |
0.11.6 | 2022年3月8日 |
0.8.1 | 2021年12月13日 |
0.1.8 | 2021年6月20日 |
#413 在 游戏开发 中
182 每月下载量
用于 7 个工具箱 (4 直接)
43KB
813 行
hecs-hierarchy
hecs-hierarchy
用于与 hecs ECS 一起使用的层次结构实现。
功能
- 遍历父级的子级
- 查找子级的父级
- 深度优先遍历层次结构
- 广度优先遍历层次结构
- 遍历祖先
- 从层次结构中分离子级
- 高效的树构建
- 反向迭代
- 排序
- (可选) 关联数据到关系
动机
一个 ECS 是一种出色的设计原则,它允许面向数据的设计。大多数时候,ECS 是扁平的,可能只有几个组件通过 Entity
id 来相互引用。然而,有时需要创建和管理良好的图形。
这就是 hecs-hierarchy 出现的地方,它能够管理连接实体的有向图。当使用 ECS 设计模式开发 UI 库,或纯粹为了将来自同一模型的所有实体分组在一起时,这非常有用。
使用方法
导入扩展 hecs::World 的 Hierarchy 特性
Hierarchy 特性扩展了 hecs::World,提供了操作和遍历层次结构树的功能。
层次结构使用一个标记类型,使得单个实体可以属于多个层次结构树。
示例使用
use hecs_hierarchy::*;
// Marker type which allows several hierarchies.
struct Tree;
let mut world = hecs::World::default();
// Create a root entity, there can be several.
let root = world.spawn(("Root",));
// Create a loose entity
let child = world.spawn(("Child 1",));
// Attaches the child to a parent, in this case `root`
world.attach::<Tree>(child, root).unwrap();
// Iterate children
for child in world.children::<Tree>(root) {
let name = world.get::<&&str>(child).unwrap();
println!("Child: {:?} {}", child, *name);
}
// Add a grandchild
world.attach_new::<Tree, _>(child, ("Grandchild",)).unwrap();
// Iterate recursively
for child in world.descendants_depth_first::<Tree>(root) {
let name = world.get::<&&str>(child).unwrap();
println!("Child: {:?} {}", child, *name)
}
// Detach `child` and `grandchild`
world.detach::<Tree>(child).unwrap();
let child2 = world.attach_new::<Tree, _>(root, ("Child 2",)).unwrap();
// Reattach as a child of `child2`
world.attach::<Tree>(child, child2).unwrap();
world.attach_new::<Tree, _>(root, ("Child 3",)).unwrap();
// Hierarchy now looks like this:
// Root
// |-------- Child 3
// |-------- Child 2
// |-------- Child 1
// |-------- Grandchild
灵感
本项目深受 Shipyard
的层次结构实现的影响,并公开了类似的 API。
依赖关系
~2–2.9MB
~51K SLoC