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
- Variables
- type Director
- func (d *Director) Entities(id InstanceID) []core.Entity
- func (d *Director) Live(id InstanceID) bool
- func (d *Director) Load(scene Scene, props any, opts ...Option) (InstanceID, error)
- func (d *Director) Name(id InstanceID) (string, bool)
- func (d *Director) Remove(id InstanceID) error
- func (d *Director) Roots() []InstanceID
- func (d *Director) Spawn(ctx *core.Context, owner InstanceID, components ...any) (core.Entity, error)
- func (d *Director) Switch(scene Scene, props any, opts ...Option) (InstanceID, error)
- func (d *Director) System() core.SystemFunc
- func (d *Director) Update(id InstanceID, scene Scene, props any, opts ...Option) error
- type Handle
- type Hooks
- type InstanceID
- type Node
- type Option
- type Preload
- type Scene
Constants ¶
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 ¶
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 (*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) 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) 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.
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 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.