scene

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: 8 Imported by: ? modules, 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 scene builds and runs scenes for the Castrum engine. A scene is a Scene builder that turns props into a Node tree. Entering an instance spawns an entity for each node; exiting it destroys those entities.

A Director queues requests to switch, load, update, or remove scene instances. Queuing does not apply a request. [NewSystem] applies the queue once per frame in FIFO order, so requests queued from a hook run in the next frame. Pending switches are coalesced, so only the latest one runs.

Director.Switch replaces every loaded instance with a new one. Director.Load adds an instance alongside the current ones. Switch and update build the new content before touching the old, so a failed request leaves the live content in place. A failed request does not stop the other requests in its batch.

Nodes can declare assets with Warm to load before entry. Enter hooks run after the tree spawns, and exit hooks run before its entities are destroyed.

Index

Constants

View Source
const (
	// StartupSystemName is the scene system's registration name in [core.PhaseStartup].
	StartupSystemName = "scene-startup"
	// FrameSystemName is the scene system's registration name in [core.PhasePreFrame].
	FrameSystemName = "scene"
)

Variables

View Source
var ErrAssetLoadFailed = errors.New("scene: asset load failed")

ErrAssetLoadFailed identifies a failure to preload a scene asset.

Functions

This section is empty.

Types

type Director

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

Director queues scene requests but does not execute them immediately. It manages scene instances and their lifecycle.

func NewDirector

func NewDirector() *Director

NewDirector creates an empty Director.

func (*Director) Entities added in v0.4.0

func (d *Director) Entities(id InstanceID) []core.Entity

Entities returns a copy of the entities owned by the instance id, or nil if id is unknown.

func (*Director) Live added in v0.4.0

func (d *Director) Live(id InstanceID) bool

Live reports whether id identifies a live instance.

func (*Director) Load

func (d *Director) Load(scene Scene, props any, opts ...Option) (InstanceID, error)

Load builds and validates scene and queues a load request.

func (*Director) Name added in v0.4.0

func (d *Director) Name(id InstanceID) (string, bool)

Name returns the name of the instance id. It reports false if id is unknown.

func (*Director) Remove

func (d *Director) Remove(id InstanceID) error

Remove queues a request to unload the specified instance. It returns an error if id is zero.

func (*Director) Roots added in v0.4.0

func (d *Director) Roots() []InstanceID

Roots returns a copy of the live root instance ids, in entry order.

func (*Director) Spawn added in v0.4.0

func (d *Director) Spawn(ctx *core.Context, owner InstanceID, components ...any) (core.Entity, error)

Spawn creates an entity owned by the instance owner. The entity is destroyed along with the instance when it unloads. It returns an error if ctx is nil, owner does not identify a live instance, or the entity cannot be created.

func (*Director) Switch

func (d *Director) Switch(scene Scene, props any, opts ...Option) (InstanceID, error)

Switch builds and validates scene and queues a switch request.

func (*Director) System added in v0.4.0

func (d *Director) System() core.SystemFunc

System returns the system that processes queued requests for the director. Scenes that declare assets require an asset.Loader resource in the world.

Register it in core.PhaseStartup as StartupSystemName and in core.PhasePreFrame as FrameSystemName.

func (*Director) Update

func (d *Director) Update(id InstanceID, scene Scene, props any, opts ...Option) error

Update builds and validates scene and queues a request to rebuild the instance id from props when the request executes. It returns an error if id is zero or the tree is invalid.

type Handle added in v0.4.0

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

Handle identifies a scene instance in lifecycle callbacks.

func (Handle) ID added in v0.4.0

func (h Handle) ID() InstanceID

ID returns the id of the handle's scene instance.

type Hooks

type Hooks struct {
	// Enter is the callback for scene entry.
	Enter func(ctx *core.Context, handle *Handle) error
	// Exit is the callback for scene exit.
	Exit func(ctx *core.Context, handle *Handle) error
}

Hooks contains scene lifecycle callbacks.

type InstanceID

type InstanceID uint64

InstanceID identifies a scene instance within a Director.

type Node added in v0.4.0

type Node struct {
	// Key identifies the node among its siblings. It must be nonempty and contain no "/".
	Key string
	// Assets lists assets to load before the scene enters.
	Assets []Preload
	// Components are the non-nil, non-pointer values attached to the entity.
	// Component-specific validation runs when the entity is created.
	Components []any
	// Children are nested scene nodes, not a transform hierarchy.
	Children []Node
	// Persistent marks the node as persistent for game runtime serialization.
	// Persistent nodes are kept across saves but do not affect scene lifetime.
	Persistent bool
}

Node describes an entity in a scene tree.

type Option added in v0.4.0

type Option func(*request)

Option configures a scene request before it is queued.

func WithHooks added in v0.4.0

func WithHooks(h Hooks) Option

WithHooks sets a scene request's lifecycle callbacks.

func WithName added in v0.4.0

func WithName(name string) Option

WithName sets a scene request's name.

type Preload added in v0.4.0

type Preload interface {
	// contains filtered or unexported methods
}

Preload describes a typed asset to load before entering a scene. Use Warm to create one.

func Warm added in v0.4.0

func Warm[T any](id asset.ID) Preload

Warm declares that the asset at id should be loaded as T before scene entry.

type Scene added in v0.4.0

type Scene func(props any) (Node, error)

Scene builds a Node tree from props.

func Of added in v0.4.0

func Of[P any](build func(P) Node) Scene

Of wraps a typed builder as a Scene.

The returned scene reports an error if props cannot be asserted to P.

Jump to

Keyboard shortcuts

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