core

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 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

Package core provides World-owned entities and resources, Query iteration over components, and the [System] and Context contracts for updating a game.

Index

Constants

View Source
const PrevTransformSystemName = "engine.prev-transform"

PrevTransformSystemName is the name under which castrum.New registers the previous-transform capture system in the pre-fixed schedule.

Variables

View Source
var ErrResourceNotRegistered = errors.New("resource is not registered")

ErrResourceNotRegistered reports that World.Resource or World.MustResource was called for a type that is not registered. Distinguish it from resolution failures with errors.Is.

Functions

This section is empty.

Types

type Authored added in v0.4.0

type Authored interface {
	// Authored returns an error if the component is unsuitable for authored
	// file content.
	Authored() error
}

Authored is the opt-in contract for validating components in authored files.

Its rules may be stricter than Validatable, allowing runtime zero values while rejecting incomplete file content. File loading calls Authored on component values; runtime entity creation does not. Implement Authored with a value receiver so file loading can detect it.

type Context

type Context struct {
	// World is the game world.
	World *World
	// Tick counts fixed simulation steps since startup.
	Tick uint64
	// DeltaTime is capped elapsed frame time in [PhasePreFrame], [PhaseFrame],
	// or [PhasePostFrame], or the fixed tick interval in [PhasePreFixed],
	// [PhaseFixed], or [PhasePostFixed].
	DeltaTime time.Duration
}

Context gives systems access to the world and timing for the current phase. The game populates its fields; systems should not modify them. The world itself remains writable.

type Entity

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

Entity is a comparable handle for addressing an entity in its World. The zero value does not identify an entity. Use World.HasEntity to check whether a handle still refers to an entity in the world.

Handles do not encode world identity. Use a handle only with the world that created its entity; another world may have an entity with the same ID.

func NewEntity

func NewEntity(id EntityID) Entity

NewEntity returns a handle for id without checking whether it exists. Use World.NewEntity to create an entity.

func (Entity) AddComponent

func (e Entity) AddComponent[T any](w *World, value T) error

AddComponent attaches a component of type T to the entity. Existing components are preserved. It returns an error if value is a pointer. Adding a Transform also adds an initial PrevTransform if the entity does not already have one.

func (Entity) Component

func (e Entity) Component[T any](w *World) (T, bool)

Component returns the entity's component of type T. It reports false if the entity does not exist or has no component of that type.

func (Entity) HasComponent

func (e Entity) HasComponent[T any](w *World) bool

HasComponent reports whether the entity has a component of type T. A nonexistent entity reports false.

func (Entity) ID

func (e Entity) ID() EntityID

ID returns the entity's identifier.

func (Entity) RemoveComponent

func (e Entity) RemoveComponent[T any](w *World) error

RemoveComponent detaches the component of type T from the entity, migrating it to the archetype for its remaining components. Removing a component the entity does not have succeeds without effect.

func (Entity) SetComponent

func (e Entity) SetComponent[T any](w *World, value T) error

SetComponent overwrites the entity's component of type T. The entity must already have the component; use AddComponent to attach one. It returns an error if value is a pointer, the entity does not exist, or it has no component of that type.

func (Entity) Update added in v0.0.4

func (e Entity) Update[T any](w *World, fn func(*T)) error

Update reads the entity's component of type T, applies fn to the copy, and writes it back. It returns an error if the entity does not exist or has no component of that type; the write validates like SetComponent.

type EntityID

type EntityID = ecs.EntityID

EntityID identifies an entity within a World. Zero is reserved and IDs are not recycled during a world's lifetime.

type Entry

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

Entry is a query result for one entity, bound to its archetype row. Read methods work while visiting the entity and on an entry returned by Query.First until the next pass; write methods only work during the pass. Structural changes invalidate the entry and may cause methods to panic. Retain Entry.Entity instead for later use.

func (Entry) ChangedOn added in v0.4.0

func (e Entry) ChangedOn[T any]() (added, changed uint64)

ChangedOn returns the world change ticks at which the visited entity's component of type T was added and most recently written. The entity must have T and T must not be excluded with Query.Omit, and T must implement TrackChanges; otherwise it panics. The ticks are comparable with World.Tick.

func (Entry) Component

func (e Entry) Component[T any]() (T, bool)

Component returns the visited entity's component of type T. It reports false for types the entity lacks or that are excluded with Query.Omit.

func (Entry) ComponentPtr added in v0.4.0

func (e Entry) ComponentPtr[T any]() *T

ComponentPtr returns a pointer to the visited entity's component of type T for in-place reads and writes, or nil if the entity lacks T or T is excluded with Query.Omit. Writes through the pointer are not stamped; use Entry.MarkChanged when change detection matters.

func (Entry) Entity added in v0.4.0

func (e Entry) Entity() Entity

Entity returns the handle for the visited entity.

func (Entry) ID

func (e Entry) ID() EntityID

ID returns the identifier of the entity associated with this entry.

func (Entry) MarkChanged added in v0.4.0

func (e Entry) MarkChanged[T any]()

MarkChanged stamps the row's change tick for component type T, as if T were written through Entry.ComponentPtr. It panics if T is not available on this entry.

type Phase

type Phase int8

Phase identifies when a system runs.

const (
	// PhasePreStartup runs once after eager resources resolve and before startup systems.
	PhasePreStartup Phase = iota
	// PhaseStartup runs once after pre-startup systems.
	PhaseStartup
	// PhasePostStartup runs once after startup systems.
	PhasePostStartup
	// PhasePreFrame runs once per display frame before frame systems.
	PhasePreFrame
	// PhaseFrame runs once per display frame, before fixed ticks.
	// Input consumption belongs here.
	PhaseFrame
	// PhasePostFrame runs once per display frame after frame systems.
	PhasePostFrame
	// PhasePreFixed runs once per fixed tick before fixed systems.
	PhasePreFixed
	// PhaseFixed runs at the fixed simulation rate. Movement,
	// physics, and timers belong here.
	PhaseFixed
	// PhasePostFixed runs once per fixed tick after fixed systems.
	PhasePostFixed
)

func (Phase) String

func (s Phase) String() string

String returns the phase's schedule name, or a formatted value for an unknown phase.

type PrevTransform

type PrevTransform struct {
	// Position is the snapshot position.
	Position geom.Vector2
	// Rotation is the snapshot rotation in radians.
	Rotation float64
	// Scale is the snapshot scale, with zero components normalized to 1.
	Scale geom.Vector2
}

PrevTransform stores an entity's transform at the start of a fixed tick. Renderers interpolate between it and Transform to smooth motion between ticks.

Rotation is interpolated as a numeric value, not along the shortest arc.

The capture system updates this component; game code should treat it as read-only. A Transform without a PrevTransform is not rendered.

type Query

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

Query selects entities by component presence and optional predicates. Configure it with Query.Require, Query.Exclude, Query.Omit, Query.Where, Query.Added, and Query.Changed, then iterate its results with Query.Execute. Filter changes between passes take effect on the next execution.

Each entry exposes every component of its entity except types excluded with Query.Omit. Added and Changed compare component change ticks of the Query.Require types against this query's watermark, independently of other queries. On the first pass, the change filters admit every row; other filters still apply. Keep change-detection queries across ticks so their watermark is retained.

func NewQuery

func NewQuery(world *World) *Query

NewQuery creates a dynamic query over world. Prefer the typed NewQuery1..6 for systems; use this when the component set is not statically known.

func (*Query) Added added in v0.4.0

func (q *Query) Added() *Query

Added filters matches to entities whose component of one of the Query.Require types was added after the tick of this query's last Query.Execute pass. At least one required type must implement TrackChanges; other required types are ignored by the filter, and a query with no tracked required type panics. If Query.Changed is also set, a row passes when either filter matches.

func (*Query) Changed added in v0.4.0

func (q *Query) Changed() *Query

Changed filters match entities whose component of one of the Query.Require types was written after the tick of this query's last Query.Execute pass; additions count as changes. At least one required type must implement TrackChanges; other required types are ignored by the filter, and a query with no tracked required type panics.

Writes stamp at World.Tick. Within one tick a write is visible only to queries whose last pass ran at an earlier tick, so run a change-detection system after the systems that write the observed components. Multiple writes to a component between passes are reported as one match. See Query.Added for combining change filters.

func (*Query) Exclude added in v0.4.0

func (q *Query) Exclude(components ...any) *Query

Exclude adds component types that an entity must not have to match. Pass zero values as type markers; nil and pointer values cause a panic.

func (*Query) Execute

func (q *Query) Execute() iter.Seq[Entry]

Execute yields entries for matching entities in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's Added/Changed watermark, even when stopped early; unvisited matches will not be replayed automatically.

A query cannot be executed recursively; use a separate query for nested iteration. Do not spawn or destroy entities, or add or remove components, during a pass: such a structural change invalidates the iteration and may cause a panic. Collect entities during the pass and apply structural changes after it ends. Updating component values through an Entry is allowed.

func (*Query) First

func (q *Query) First() (Entry, bool)

First returns the first entry yielded by the query and whether a match exists. Like an early-stopped Query.Execute pass, it advances the watermark.

func (*Query) Omit added in v0.4.0

func (q *Query) Omit(components ...any) *Query

Omit removes component types from the entries' accessible columns: an entity that has them still matches, but Entry.Component reports false and the write methods panic for them. Pass zero values as type markers; nil and pointer values cause a panic. Omit affects access only; use Query.Exclude to exclude entities from matching.

func (*Query) Require added in v0.4.0

func (q *Query) Require(components ...any) *Query

Require adds component types that an entity must have to match. Pass zero values as type markers; nil and pointer values cause a panic. Require only selects matches; entry component access is not limited to these types, use Query.Omit to hide columns.

func (*Query) Where

func (q *Query) Where(predicate func(e Entry) bool) *Query

Where sets a predicate that an entity must satisfy to match, replacing any previous predicate. It runs for each candidate on every pass and can access any of the entity's components through Entry.Component, except types excluded with Query.Omit. Use Query.Require or Query.Exclude for component-presence filters.

type Query1 added in v0.4.0

type Query1[A any] struct {
	// contains filtered or unexported fields
}

Query1[A any] selects entities with all specified component types and iterates them as typed rows. Configure it with Query1.Exclude, Query1.Where, Query1.Added, and Query1.Changed, then iterate with Query1.Execute. Filter changes between passes take effect on the next pass. Added and Changed filters admit all candidates on the first pass and combine with OR.

func NewQuery1 added in v0.4.0

func NewQuery1[A any](w *World) *Query1[A]

NewQuery1 creates a query over World that requires all specified component types.

func (*Query1[A]) Added added in v0.4.0

func (q *Query1[A]) Added() *Query1[A]

Added matches rows whose tracked query component was added after the tick recorded by the previous pass. At least one query component must implement TrackChanges.

func (*Query1[A]) Changed added in v0.4.0

func (q *Query1[A]) Changed() *Query1[A]

Changed matches rows whose tracked query component was stamped as changed after the tick recorded by the previous pass; additions count as changes. At least one query component must implement TrackChanges.

func (*Query1[A]) Exclude added in v0.4.0

func (q *Query1[A]) Exclude[T any]() *Query1[A]

Exclude excludes entities with a component of type T.

func (*Query1[A]) Execute added in v0.4.0

func (q *Query1[A]) Execute() iter.Seq2[Entity, Row1[A]]

Execute yields matching rows in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's change-filter watermark, even when stopped early. Rows may be written in place through their pointers; call Row1.MarkChanged when change detection must observe those writes.

Do not spawn or destroy entities, or add or remove components, during a pass. A query cannot be executed recursively; use a separate query for nested iteration.

func (*Query1[A]) First added in v0.4.0

func (q *Query1[A]) First() (Entity, A, bool)

First returns the first matching entity, copies of its component values, and whether a match exists. Like an early-stopped Query1.Execute pass, it advances the watermark. The values are copies; writes need the entity handle.

func (*Query1[A]) Where added in v0.4.0

func (q *Query1[A]) Where(fn func(ent Entity, a *A) bool) *Query1[A]

Where sets a per-row predicate, replacing any previous predicate. Passing nil clears it.

type Query2 added in v0.4.0

type Query2[A, B any] struct {
	// contains filtered or unexported fields
}

Query2[A, B any] selects entities with all specified component types and iterates them as typed rows. Configure it with Query2.Exclude, Query2.Where, Query2.Added, and Query2.Changed, then iterate with Query2.Execute. Filter changes between passes take effect on the next pass. Added and Changed filters admit all candidates on the first pass and combine with OR.

func NewQuery2 added in v0.4.0

func NewQuery2[A, B any](w *World) *Query2[A, B]

NewQuery2 creates a query over World that requires all specified component types.

func (*Query2[A, B]) Added added in v0.4.0

func (q *Query2[A, B]) Added() *Query2[A, B]

Added matches rows whose tracked query component was added after the tick recorded by the previous pass. At least one query component must implement TrackChanges.

func (*Query2[A, B]) Changed added in v0.4.0

func (q *Query2[A, B]) Changed() *Query2[A, B]

Changed matches rows whose tracked query component was stamped as changed after the tick recorded by the previous pass; additions count as changes. At least one query component must implement TrackChanges.

func (*Query2[A, B]) Exclude added in v0.4.0

func (q *Query2[A, B]) Exclude[T any]() *Query2[A, B]

Exclude excludes entities with a component of type T.

func (*Query2[A, B]) Execute added in v0.4.0

func (q *Query2[A, B]) Execute() iter.Seq2[Entity, Row2[A, B]]

Execute yields matching rows in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's change-filter watermark, even when stopped early. Rows may be written in place through their pointers; call Row2.MarkChanged when change detection must observe those writes.

Do not spawn or destroy entities, or add or remove components, during a pass. A query cannot be executed recursively; use a separate query for nested iteration.

func (*Query2[A, B]) First added in v0.4.0

func (q *Query2[A, B]) First() (Entity, A, B, bool)

First returns the first matching entity, copies of its component values, and whether a match exists. Like an early-stopped Query2.Execute pass, it advances the watermark. The values are copies; writes need the entity handle.

func (*Query2[A, B]) Where added in v0.4.0

func (q *Query2[A, B]) Where(fn func(ent Entity, a *A, b *B) bool) *Query2[A, B]

Where sets a per-row predicate, replacing any previous predicate. Passing nil clears it.

type Query3 added in v0.4.0

type Query3[A, B, C any] struct {
	// contains filtered or unexported fields
}

Query3[A, B, C any] selects entities with all specified component types and iterates them as typed rows. Configure it with Query3.Exclude, Query3.Where, Query3.Added, and Query3.Changed, then iterate with Query3.Execute. Filter changes between passes take effect on the next pass. Added and Changed filters admit all candidates on the first pass and combine with OR.

func NewQuery3 added in v0.4.0

func NewQuery3[A, B, C any](w *World) *Query3[A, B, C]

NewQuery3 creates a query over World that requires all specified component types.

func (*Query3[A, B, C]) Added added in v0.4.0

func (q *Query3[A, B, C]) Added() *Query3[A, B, C]

Added matches rows whose tracked query component was added after the tick recorded by the previous pass. At least one query component must implement TrackChanges.

func (*Query3[A, B, C]) Changed added in v0.4.0

func (q *Query3[A, B, C]) Changed() *Query3[A, B, C]

Changed matches rows whose tracked query component was stamped as changed after the tick recorded by the previous pass; additions count as changes. At least one query component must implement TrackChanges.

func (*Query3[A, B, C]) Exclude added in v0.4.0

func (q *Query3[A, B, C]) Exclude[T any]() *Query3[A, B, C]

Exclude excludes entities with a component of type T.

func (*Query3[A, B, C]) Execute added in v0.4.0

func (q *Query3[A, B, C]) Execute() iter.Seq2[Entity, Row3[A, B, C]]

Execute yields matching rows in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's change-filter watermark, even when stopped early. Rows may be written in place through their pointers; call Row3.MarkChanged when change detection must observe those writes.

Do not spawn or destroy entities, or add or remove components, during a pass. A query cannot be executed recursively; use a separate query for nested iteration.

func (*Query3[A, B, C]) First added in v0.4.0

func (q *Query3[A, B, C]) First() (Entity, A, B, C, bool)

First returns the first matching entity, copies of its component values, and whether a match exists. Like an early-stopped Query3.Execute pass, it advances the watermark. The values are copies; writes need the entity handle.

func (*Query3[A, B, C]) Where added in v0.4.0

func (q *Query3[A, B, C]) Where(fn func(ent Entity, a *A, b *B, c *C) bool) *Query3[A, B, C]

Where sets a per-row predicate, replacing any previous predicate. Passing nil clears it.

type Query4 added in v0.4.0

type Query4[A, B, C, D any] struct {
	// contains filtered or unexported fields
}

Query4[A, B, C, D any] selects entities with all specified component types and iterates them as typed rows. Configure it with Query4.Exclude, Query4.Where, Query4.Added, and Query4.Changed, then iterate with Query4.Execute. Filter changes between passes take effect on the next pass. Added and Changed filters admit all candidates on the first pass and combine with OR.

func NewQuery4 added in v0.4.0

func NewQuery4[A, B, C, D any](w *World) *Query4[A, B, C, D]

NewQuery4 creates a query over World that requires all specified component types.

func (*Query4[A, B, C, D]) Added added in v0.4.0

func (q *Query4[A, B, C, D]) Added() *Query4[A, B, C, D]

Added matches rows whose tracked query component was added after the tick recorded by the previous pass. At least one query component must implement TrackChanges.

func (*Query4[A, B, C, D]) Changed added in v0.4.0

func (q *Query4[A, B, C, D]) Changed() *Query4[A, B, C, D]

Changed matches rows whose tracked query component was stamped as changed after the tick recorded by the previous pass; additions count as changes. At least one query component must implement TrackChanges.

func (*Query4[A, B, C, D]) Exclude added in v0.4.0

func (q *Query4[A, B, C, D]) Exclude[T any]() *Query4[A, B, C, D]

Exclude excludes entities with a component of type T.

func (*Query4[A, B, C, D]) Execute added in v0.4.0

func (q *Query4[A, B, C, D]) Execute() iter.Seq2[Entity, Row4[A, B, C, D]]

Execute yields matching rows in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's change-filter watermark, even when stopped early. Rows may be written in place through their pointers; call Row4.MarkChanged when change detection must observe those writes.

Do not spawn or destroy entities, or add or remove components, during a pass. A query cannot be executed recursively; use a separate query for nested iteration.

func (*Query4[A, B, C, D]) First added in v0.4.0

func (q *Query4[A, B, C, D]) First() (Entity, A, B, C, D, bool)

First returns the first matching entity, copies of its component values, and whether a match exists. Like an early-stopped Query4.Execute pass, it advances the watermark. The values are copies; writes need the entity handle.

func (*Query4[A, B, C, D]) Where added in v0.4.0

func (q *Query4[A, B, C, D]) Where(fn func(ent Entity, a *A, b *B, c *C, d *D) bool) *Query4[A, B, C, D]

Where sets a per-row predicate, replacing any previous predicate. Passing nil clears it.

type Query5 added in v0.4.0

type Query5[A, B, C, D, E any] struct {
	// contains filtered or unexported fields
}

Query5[A, B, C, D, E any] selects entities with all specified component types and iterates them as typed rows. Configure it with Query5.Exclude, Query5.Where, Query5.Added, and Query5.Changed, then iterate with Query5.Execute. Filter changes between passes take effect on the next pass. Added and Changed filters admit all candidates on the first pass and combine with OR.

func NewQuery5 added in v0.4.0

func NewQuery5[A, B, C, D, E any](w *World) *Query5[A, B, C, D, E]

NewQuery5 creates a query over World that requires all specified component types.

func (*Query5[A, B, C, D, E]) Added added in v0.4.0

func (q *Query5[A, B, C, D, E]) Added() *Query5[A, B, C, D, E]

Added matches rows whose tracked query component was added after the tick recorded by the previous pass. At least one query component must implement TrackChanges.

func (*Query5[A, B, C, D, E]) Changed added in v0.4.0

func (q *Query5[A, B, C, D, E]) Changed() *Query5[A, B, C, D, E]

Changed matches rows whose tracked query component was stamped as changed after the tick recorded by the previous pass; additions count as changes. At least one query component must implement TrackChanges.

func (*Query5[A, B, C, D, E]) Exclude added in v0.4.0

func (q *Query5[A, B, C, D, E]) Exclude[T any]() *Query5[A, B, C, D, E]

Exclude excludes entities with a component of type T.

func (*Query5[A, B, C, D, E]) Execute added in v0.4.0

func (q *Query5[A, B, C, D, E]) Execute() iter.Seq2[Entity, Row5[A, B, C, D, E]]

Execute yields matching rows in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's change-filter watermark, even when stopped early. Rows may be written in place through their pointers; call Row5.MarkChanged when change detection must observe those writes.

Do not spawn or destroy entities, or add or remove components, during a pass. A query cannot be executed recursively; use a separate query for nested iteration.

func (*Query5[A, B, C, D, E]) First added in v0.4.0

func (q *Query5[A, B, C, D, E]) First() (Entity, A, B, C, D, E, bool)

First returns the first matching entity, copies of its component values, and whether a match exists. Like an early-stopped Query5.Execute pass, it advances the watermark. The values are copies; writes need the entity handle.

func (*Query5[A, B, C, D, E]) Where added in v0.4.0

func (q *Query5[A, B, C, D, E]) Where(fn func(ent Entity, a *A, b *B, c *C, d *D, e *E) bool) *Query5[A, B, C, D, E]

Where sets a per-row predicate, replacing any previous predicate. Passing nil clears it.

type Query6 added in v0.4.0

type Query6[A, B, C, D, E, F any] struct {
	// contains filtered or unexported fields
}

Query6[A, B, C, D, E, F any] selects entities with all specified component types and iterates them as typed rows. Configure it with Query6.Exclude, Query6.Where, Query6.Added, and Query6.Changed, then iterate with Query6.Execute. Filter changes between passes take effect on the next pass. Added and Changed filters admit all candidates on the first pass and combine with OR.

func NewQuery6 added in v0.4.0

func NewQuery6[A, B, C, D, E, F any](w *World) *Query6[A, B, C, D, E, F]

NewQuery6 creates a query over World that requires all specified component types.

func (*Query6[A, B, C, D, E, F]) Added added in v0.4.0

func (q *Query6[A, B, C, D, E, F]) Added() *Query6[A, B, C, D, E, F]

Added matches rows whose tracked query component was added after the tick recorded by the previous pass. At least one query component must implement TrackChanges.

func (*Query6[A, B, C, D, E, F]) Changed added in v0.4.0

func (q *Query6[A, B, C, D, E, F]) Changed() *Query6[A, B, C, D, E, F]

Changed matches rows whose tracked query component was stamped as changed after the tick recorded by the previous pass; additions count as changes. At least one query component must implement TrackChanges.

func (*Query6[A, B, C, D, E, F]) Exclude added in v0.4.0

func (q *Query6[A, B, C, D, E, F]) Exclude[T any]() *Query6[A, B, C, D, E, F]

Exclude excludes entities with a component of type T.

func (*Query6[A, B, C, D, E, F]) Execute added in v0.4.0

func (q *Query6[A, B, C, D, E, F]) Execute() iter.Seq2[Entity, Row6[A, B, C, D, E, F]]

Execute yields matching rows in stable order while the world's structure is unchanged. Breaking the range stops the pass, and the query can be executed again immediately. Each completed pass records its starting World.Tick as the query's change-filter watermark, even when stopped early. Rows may be written in place through their pointers; call Row6.MarkChanged when change detection must observe those writes.

Do not spawn or destroy entities, or add or remove components, during a pass. A query cannot be executed recursively; use a separate query for nested iteration.

func (*Query6[A, B, C, D, E, F]) First added in v0.4.0

func (q *Query6[A, B, C, D, E, F]) First() (Entity, A, B, C, D, E, F, bool)

First returns the first matching entity, copies of its component values, and whether a match exists. Like an early-stopped Query6.Execute pass, it advances the watermark. The values are copies; writes need the entity handle.

func (*Query6[A, B, C, D, E, F]) Where added in v0.4.0

func (q *Query6[A, B, C, D, E, F]) Where(fn func(ent Entity, a *A, b *B, c *C, d *D, e *E, f *F) bool) *Query6[A, B, C, D, E, F]

Where sets a per-row predicate, replacing any previous predicate. Passing nil clears it.

type Removal added in v0.4.0

type Removal struct {
	// Entity is the handle of the entity that lost the component. The handle
	// may no longer refer to a live entity if the entity was destroyed.
	Entity Entity
	// Tick is the world change tick at which the removal was recorded.
	Tick uint64
}

Removal records the removal of a tracked component from an entity.

type Row1 added in v0.4.0

type Row1[A any] struct {
	// A points to the component of type A in this row.
	A *A
	// contains filtered or unexported fields
}

Row1[A any] is a typed row yielded by Query1: pointers into the matched archetype's columns, valid for the current pass.

func (Row1[A]) MarkChanged added in v0.4.0

func (r Row1[A]) MarkChanged()

MarkChanged stamps the current pass tick on every tracked component in the row.

type Row2 added in v0.4.0

type Row2[A, B any] struct {
	// A points to the component of type A in this row.
	A *A
	// B points to the component of type B in this row.
	B *B
	// contains filtered or unexported fields
}

Row2[A, B any] is a typed row yielded by Query2: pointers into the matched archetype's columns, valid for the current pass.

func (Row2[A, B]) MarkChanged added in v0.4.0

func (r Row2[A, B]) MarkChanged()

MarkChanged stamps the current pass tick on every tracked component in the row.

type Row3 added in v0.4.0

type Row3[A, B, C any] struct {
	// A points to the component of type A in this row.
	A *A
	// B points to the component of type B in this row.
	B *B
	// C points to the component of type C in this row.
	C *C
	// contains filtered or unexported fields
}

Row3[A, B, C any] is a typed row yielded by Query3: pointers into the matched archetype's columns, valid for the current pass.

func (Row3[A, B, C]) MarkChanged added in v0.4.0

func (r Row3[A, B, C]) MarkChanged()

MarkChanged stamps the current pass tick on every tracked component in the row.

type Row4 added in v0.4.0

type Row4[A, B, C, D any] struct {
	// A points to the component of type A in this row.
	A *A
	// B points to the component of type B in this row.
	B *B
	// C points to the component of type C in this row.
	C *C
	// D points to the component of type D in this row.
	D *D
	// contains filtered or unexported fields
}

Row4[A, B, C, D any] is a typed row yielded by Query4: pointers into the matched archetype's columns, valid for the current pass.

func (Row4[A, B, C, D]) MarkChanged added in v0.4.0

func (r Row4[A, B, C, D]) MarkChanged()

MarkChanged stamps the current pass tick on every tracked component in the row.

type Row5 added in v0.4.0

type Row5[A, B, C, D, E any] struct {
	// A points to the component of type A in this row.
	A *A
	// B points to the component of type B in this row.
	B *B
	// C points to the component of type C in this row.
	C *C
	// D points to the component of type D in this row.
	D *D
	// E points to the component of type E in this row.
	E *E
	// contains filtered or unexported fields
}

Row5[A, B, C, D, E any] is a typed row yielded by Query5: pointers into the matched archetype's columns, valid for the current pass.

func (Row5[A, B, C, D, E]) MarkChanged added in v0.4.0

func (r Row5[A, B, C, D, E]) MarkChanged()

MarkChanged stamps the current pass tick on every tracked component in the row.

type Row6 added in v0.4.0

type Row6[A, B, C, D, E, F any] struct {
	// A points to the component of type A in this row.
	A *A
	// B points to the component of type B in this row.
	B *B
	// C points to the component of type C in this row.
	C *C
	// D points to the component of type D in this row.
	D *D
	// E points to the component of type E in this row.
	E *E
	// F points to the component of type F in this row.
	F *F
	// contains filtered or unexported fields
}

Row6[A, B, C, D, E, F any] is a typed row yielded by Query6: pointers into the matched archetype's columns, valid for the current pass.

func (Row6[A, B, C, D, E, F]) MarkChanged added in v0.4.0

func (r Row6[A, B, C, D, E, F]) MarkChanged()

MarkChanged stamps the current pass tick on every tracked component in the row.

type SystemFunc

type SystemFunc func(ctx *Context) error

SystemFunc runs game logic during a schedule phase.

A non-nil error stops the schedule and is returned by the game.

func NewPrevTransformCapture

func NewPrevTransformCapture() SystemFunc

NewPrevTransformCapture returns the system that updates each existing PrevTransform from its entity's Transform for rendering interpolation.

Run it before systems that change transforms so the snapshot represents the start of the tick. castrum.New registers it first in the pre-fixed phase under PrevTransformSystemName.

type TrackChanges added in v0.4.0

type TrackChanges interface {
	// TrackChanges opts the component type into per-row change tracking.
	TrackChanges()
}

TrackChanges opts a component type into change detection. Tracked types support Query.Added, Query.Changed, Entry.ChangedOn, and World.Removals.

Implement it on the component value type; pointer components are rejected:

type Health struct{ Amount int }

func (Health) TrackChanges() {}

The world stamps writes made through World.NewEntity, World.NewEntities, Entity.AddComponent, Entity.SetComponent, Entity.Update, [Entry.SetComponent], and [Entry.Update]. Writes that bypass these APIs are not stamped.

type Transform

type Transform struct {
	// Position is the entity's position in 2D space.
	Position geom.Vector2 `json:"position"`
	// Rotation is the entity's rotation in radians.
	Rotation float64 `json:"rotation"`
	// Scale is the entity's 2D scale. A zero component is treated as 1.
	Scale geom.Vector2 `json:"scale"`
	// Offset is the transform's position offset.
	Offset geom.Vector2 `json:"offset"`
}

Transform stores an entity's position, rotation, scale, and offset in 2D space.

When World.NewEntity or Entity.AddComponent adds a Transform, the world also adds a PrevTransform if one is not already present.

type Validatable

type Validatable interface {
	// Validate checks that the component's values satisfy its
	// constraints.
	Validate() error
}

Validatable is the opt-in contract for component validation. Implement it on components with value constraints; the engine calls Validate before a component enters storage through the entity API - World.NewEntity, Entity.AddComponent, Entity.SetComponent, and Entity.Update. Query entry writes ([Entry.SetComponent] and [Entry.Update]) bypass validation so iteration stays allocation- and error-free; a system that consumes a component which other systems may write mid-loop is responsible for calling Validate itself before use.

type World

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

World owns a game's entities, components, and registered resources.

A World is not safe for concurrent use. In a running game, access it from the engine goroutine, including from systems. To do work on another goroutine, copy the needed data and return results to the engine goroutine. The asset loader's loading API is safe to call from other goroutines.

func NewWorld

func NewWorld() *World

NewWorld creates an empty world.

func (*World) AdvanceTick added in v0.4.0

func (w *World) AdvanceTick()

AdvanceTick advances the world's change tick and retires removals from the preceding tick.

The engine calls AdvanceTick once per fixed update. A manually driven world should call it once per update: writes made after a query has executed at the same tick are not visible to that query's change filters.

func (*World) DestroyEntities

func (w *World) DestroyEntities(entities []Entity) error

DestroyEntities removes entities, stopping at the first error. Entities removed before an error remain destroyed.

func (*World) DestroyEntity

func (w *World) DestroyEntity(entity Entity) error

DestroyEntity removes entity and its components from the world. It returns an error if entity does not exist.

func (*World) HasEntity added in v0.4.0

func (w *World) HasEntity(entity Entity) bool

HasEntity reports whether entity exists in the world.

func (*World) HasResource added in v0.4.0

func (w *World) HasResource[T any]() bool

HasResource reports whether a resource of type T is registered. It does not resolve the resource.

func (*World) InsertResource added in v0.4.0

func (w *World) InsertResource[T any](value T) error

InsertResource stores value as the resource of type T, returning an error if a resource of the same type is already registered.

func (*World) MustResource added in v0.1.0

func (w *World) MustResource[T any]() T

MustResource returns the resource of type T, panicking if it is not registered or cannot be resolved. Use it only for resources guaranteed by engine-owned wiring; use World.Resource when the resource may be absent.

func (*World) NewEntities

func (w *World) NewEntities(count int, components ...any) ([]Entity, error)

NewEntities creates count entities with the same components and returns their handles. Count must be positive. If creation fails, it returns the error and no handles, and creates no entities. Components must be non-nil, non-pointer values.

func (*World) NewEntity

func (w *World) NewEntity(components ...any) (Entity, error)

NewEntity creates an entity with the given components and returns its handle. It returns an error if a component is nil, a pointer, or fails validation. Adding a Transform also adds its initial PrevTransform unless one is provided.

func (*World) Provide

func (w *World) Provide[T any](ctor func(*World) (T, error)) error

Provide registers a resource constructor. The resource is created on its first World.Resource request. It returns an error if T is already registered.

func (*World) ProvideEager

func (w *World) ProvideEager[T any](ctor func(*World) (T, error)) error

ProvideEager registers a resource constructor for resolution by World.ResolveEager. It returns an error if T is already registered.

func (*World) Removals added in v0.4.0

func (w *World) Removals[T any]() []Removal

Removals returns the removals of component type T recorded during the current and previous change ticks, oldest first. A nonempty result is newly allocated; if there are no records, it returns nil.

Only tracked types are recorded: if T does not implement TrackChanges, Removals returns nil. Removing a component from an entity, or destroying the entity, records one removal per tracked component type the entity had.

func (*World) ReplaceResource added in v0.4.0

func (w *World) ReplaceResource[T any](value T) error

ReplaceResource replaces the resource of type T with value, discarding any pending constructor for T. It returns an error if T is not registered, or if value is nil or its Validate() error fails. On error the world is unchanged.

func (*World) ResolveEager

func (w *World) ResolveEager() error

ResolveEager resolves resources registered with World.ProvideEager, in registration order. It stops at and returns the first resolution error.

func (*World) Resource

func (w *World) Resource[T any]() (T, error)

Resource returns the registered resource of type T, resolving it on first access. It returns ErrResourceNotRegistered if T is not registered, and the resolution error if the resource's constructor fails. A successful nil interface result is returned as the zero value of T.

func (*World) Tick added in v0.4.0

func (w *World) Tick() uint64

Tick returns the world's current change tick, starting at zero. It is comparable with the ticks returned by Entry.ChangedOn and World.Removals. When advanced by the engine, it is in step with Context.Tick.

Directories

Path Synopsis
Command genquery generates typed query rows and queries for arities 1 through the maximum selected with -max.
Command genquery generates typed query rows and queries for arities 1 through the maximum selected with -max.

Jump to

Keyboard shortcuts

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