hierarchy

package
v0.0.0-...-94fff03 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

+game:group:name=hierarchy

Package hierarchy attaches entities to a parent. A child holds one component, Parent, with the parent entity and a Local offset; the System writes the child's world gfx.Transform and, by default, its draw layer from the parent chain every tick. Children of a deleted parent are deleted. In Y-sorted layers a child sorts just in front of or behind its root. See doc/guide/engine/hierarchy.md.

Package game installs the System; package extract reads Parent.RootY.

Index

Constants

View Source
const MaxDepth = 32

MaxDepth is the longest parent chain the System walks. A longer chain, or a cycle, panics.

Variables

View Source
var (
	CID_Parent = ecs.RegisterComp[Parent]()
)

Functions

func Attach

func Attach(w *ecs.World, child, parent ecs.Entity, keepWorld bool)

Attach makes child a child of parent. Neither may be stale, and child must not be an ancestor of parent or parent itself (panics with "hierarchy: attach would create a cycle"). It adds a gfx.Transform to child if it has none, and a Parent or, if child is already attached, changes its Entity. Adding the component moves the child to another archetype, so do not call Attach inside an Each over it.

With keepWorld the Local is computed from the current world Transforms so that the child stays where it is, which reads parent's Transform as of the last System run. Without it the child keeps its existing Local (zero for a new attachment), so it jumps to the parent. The first placement after Attach clears the child's Transform.Prev, so the move is not interpolated.

func CheckCycles

func CheckCycles(w *ecs.World, ents []ecs.Entity, name func(ecs.Entity) string) error

CheckCycles reports every parent cycle, and every chain deeper than MaxDepth, that starts at one of ents. The System panics on those, so the engine checks the objects of a document before it lets them run. Each problem is its own error in the joined result, naming its entities; name maps an entity to the text to print (an object id), nil prints the entity. Entities without a Parent or that are not alive are ignored.

func Children

func Children(w *ecs.World, e ecs.Entity) []ecs.Entity

Children returns the entities directly attached to e. It scans every Parent in the World: use it in tools and one-off code, not per tick. The order is that of the archetype storage.

func Compose

func Compose(parent gfx.Transform, l Local) gfx.Transform

Compose returns the world transform of a child placed at l in parent's space. Only Pos, Rot and Scale are set; Prev is the caller's business. Non-uniform scale with rotation has no shear: the offset is scaled and then rotated, and the child's own scale and rotation stay independent.

func Delete

func Delete(w *ecs.World, e ecs.Entity)

Delete deletes e and all its descendants now. Like ecs.World.DeleteEntity it panics inside an Each; during a tick, delete the parent instead and let the System delete the children. It scans every Parent in the World.

func Detach

func Detach(w *ecs.World, child ecs.Entity)

Detach removes child's Parent. The Transform already holds the world placement, so the child stays where it is. Detaching an entity that has no Parent does nothing. Do not call it inside an Each over child.

func Reposition

func Reposition(w *ecs.World, child ecs.Entity)

Reposition places child from its Local again in the next System run, in either PlaceMode; for a PlaceAbsolute child it is the way to move it back next to its parent. Like the first placement it clears Transform.Prev, so the move is not interpolated. It panics with "hierarchy: Reposition: <child> has no Parent".

func SetPlace

func SetPlace(w *ecs.World, child ecs.Entity, mode PlaceMode, keepWorld bool)

SetPlace sets the PlaceMode of child. Switching to PlaceAbsolute keeps child where it is: its Transform already holds the world placement. With PlaceRelative and keepWorld, the Local (Pos, Rot, Scale) is computed with LocalOf from the parent's WorldOf and child's Transform so that child stays where it is; without keepWorld it moves back to its Local in the next System run. Use it rather than writing Local.PlaceMode when the child should not jump. It panics with "hierarchy: SetPlace: <child> has no Parent". The component is changed in place: no archetype move.

func WorldOf

func WorldOf(w *ecs.World, e ecs.Entity) (gfx.Transform, bool)

WorldOf returns the world Transform (Pos, Rot, Scale) of e as the System would write it now: the Locals of e's chain composed onto its root's Transform, or onto the Transform of the nearest PlaceAbsolute link once that is placed (e's own Transform if e is that link). Use it where placement is read between a structural change and the next System run, such as a tool framing objects that were just spawned and whose Transform is not placed yet. An entity without a Parent gets its own Transform. ok is false when e or a link of its chain is dead, or when e has neither a Parent nor a Transform. It walks the chain without allocating, and panics like the System on a cycle or a chain deeper than MaxDepth.

Types

type LayerMode

type LayerMode uint8

LayerMode says how a child's draw layer follows its parent's.

+game:enum

const (
	// LayerRelative sets the child's layers to the parent's layer plus
	// [Local.LayerOffset] every tick.
	LayerRelative LayerMode = iota
	// LayerAbsolute leaves the child's layers alone.
	LayerAbsolute
)

Layer modes.

type Local

type Local struct {
	// Pos is the offset in the parent's space: scaled and rotated by the
	// parent's world Scale and Rot.
	Pos gfx.Vec2
	// Rot is added to the parent's rotation, in radians. Under a mirrored
	// parent (one negative scale axis) it is subtracted.
	//
	// +game:editor:unit=deg,step=5
	Rot float32
	// Scale multiplies the parent's scale. Zero means (1, 1).
	//
	// +game:default:X=1,Y=1
	Scale gfx.Vec2
	// PlaceMode says how the child's Transform follows the parent. With
	// PlaceAbsolute, Local is the placement the child starts from, not a
	// live offset: the System never writes it.
	PlaceMode PlaceMode
	// LayerOffset is added to the parent's layer with LayerRelative.
	LayerOffset int16
	// LayerMode says how the child's layers follow the parent's.
	LayerMode LayerMode
	// YSortSide says where the child sorts relative to its root in a
	// Y-sorted layer.
	YSortSide YSortSide
}

Local is the placement of a child relative to its parent.

func LocalOf

func LocalOf(parent, world gfx.Transform) Local

LocalOf is the inverse of Compose: the Local (Pos, Rot, Scale) that places a child at world under parent. A parent scale axis of zero cannot be inverted; the local axis is then zero.

type Parent

type Parent struct {
	// Entity is the parent. In a GOD level it is the id of an object of the
	// same document.
	Entity ecs.Entity
	// Local is the placement relative to the parent.
	Local Local
	// contains filtered or unexported fields
}

Parent attaches its entity to Entity. The entity also needs a gfx.Transform: the System overwrites it each tick with the world transform computed from the parent chain, so a serialized Transform of a child is derived data. Use Attach from code.

+game:component

func (*Parent) RootY

func (p *Parent) RootY() float32

RootY returns the world Y of the root of the child's parent chain as of the last System run. The sprite extract uses it, plus or minus a small step, as the child's sort key.

type PlaceMode

type PlaceMode uint8

PlaceMode says how a child's Transform follows its parent.

+game:enum

const (
	// PlaceRelative writes the child's Transform from the parent chain and
	// [Local] every tick.
	PlaceRelative PlaceMode = iota
	// PlaceAbsolute places the child from [Local] once (when it spawns, is
	// attached, or after [Reposition]) and then leaves its Transform alone,
	// so it keeps its world placement while the parent moves. Its lifetime
	// and draw layer still follow the parent, and it is the root of the
	// placement (and the Y-sort key) of its own children.
	PlaceAbsolute
)

Place modes.

type System

type System struct {
	// contains filtered or unexported fields
}

System writes the world gfx.Transform and the draw layers of every entity with a Parent, and deletes the children of dead parents.

For each child it walks up to the root (the first ancestor without a Parent) and composes the Locals of the chain onto the root's Transform. It never reads an intermediate ancestor's Transform or layer, so the order in which the entities are visited does not matter and no child lists or depth tags are needed. The cost is O(children x depth), with 0 allocations.

A root without a Transform counts as a zero Transform. A child with PlaceAbsolute is composed like this only until it is placed; after that its own Transform is kept, and it stands in for the root of its descendants (see [place]).

func NewSystem

func NewSystem(w *ecs.World) *System

NewSystem returns the System of w.

func (*System) Run

func (s *System) Run(*system.Context)

Run implements system.Runner.

type YSortSide

type YSortSide uint8

YSortSide says where a child sorts relative to its root in a Y-sorted layer.

+game:enum

const (
	// YSortInFront sorts the child just in front of its root.
	YSortInFront YSortSide = iota
	// YSortBehind sorts the child just behind its root.
	YSortBehind
)

Y-sort sides.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL