std/ecs/hierarchy

std/ecs/src/hierarchy.trb

type Parent

type Parent with Equals, Show

Whose space an entity's transform is written in. A hierarchy is a component, not a second structure: the column of Parent is the tree, and depthOrder computes the order a pass over it needs.

Examples

var parents: Column<Parent> = Column()
parents.put Entity(1), Parent(Entity(0))
assert(parents.get(Entity(1)) == Some(Parent(Entity(0))))

field entity

entity: Entity

The entity this one's space is written in.

fn depthOrder

fn depthOrder(parents: Column<Parent>, entities: Iterate<Entity>): List<Entity>

entities in an order where every parent comes before its children, each one listed once. Entities that do not depend on each other keep the order they have in entities; a parent that is not in entities is not listed, but its place in the chain still orders the entities below it.

A chain of any depth is walked with a loop, not a recursion. A cycle in parents is a bug in the program and cannot be prevented by the type system, so it panics with the entity the walk found it at and does not break it quietly.

Panics

When the parents form a cycle, with the parents of <entity> form a cycle: <ancestor> is its own ancestor.

Examples

var parents: Column<Parent> = Column()
const root = Entity 0
const child = Entity 1
const grandchild = Entity 2
parents.put grandchild, Parent(child)
parents.put child, Parent(root)
assert(depthOrder(parents, [grandchild, child, root]) == [root, child, grandchild])