instancing

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package instancing implements ephemeral isolated session worlds — the "dungeon / raid / sandbox" model: spin up a world for a group, track objectives within it, fire bindings (open doors, gate triggers) as state advances, persist enough to resume after restart, and tear it all down when the session ends.

Game dungeons / raids are the canonical use; the same mechanism applies to CI pipeline runs, ephemeral training environments, and sandboxed execution contexts.

Per the architectural invariants, this package never imports kit/unreal/* or project services — entity ownership and spawn data are supplied by a caller-implemented Loader interface, keeping kit/game/* independent in the import graph.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownInstance  = errors.New("instancing: unknown instance")
	ErrInstanceExists   = errors.New("instancing: instance already exists")
	ErrUnknownObjective = errors.New("instancing: unknown objective")
	ErrTerminalState    = errors.New("instancing: objective is in a terminal state")
)

Errors surfaced by the instancing package.

Functions

This section is empty.

Types

type Binding

type Binding struct {
	// Objective is the objective this binding watches.
	Objective ObjectiveID
	// Object is the world object the binding affects.
	Object string
	// OnState maps an objective state to an action token (caller-defined
	// string, e.g. "open", "close", "spawn").
	OnState map[ObjectiveState]string
}

Binding links a named object (door, gate, spawner) to an objective state. When the objective transitions to a state listed in OnState, the manager invokes Definition.OnBinding with the action token.

type Definition

type Definition struct {
	Name       string
	Objectives []ObjectiveID
	Bindings   []Binding
	Loader     Loader

	OnCreate      func(ctx context.Context, inst *Instance, plan SpawnPlan) error
	OnPlayerEnter func(ctx context.Context, inst *Instance, player string) error
	OnPlayerExit  func(ctx context.Context, inst *Instance, player string) error
	OnObjective   func(ctx context.Context, inst *Instance, obj ObjectiveID, prev, next ObjectiveState)
	OnBinding     func(ctx context.Context, inst *Instance, b Binding, action string)
	OnDestroy     func(ctx context.Context, inst *Instance)
}

Definition is the shape of an instance type — what objectives exist, which bindings react to state changes, and the lifecycle hooks.

type Instance

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

Instance is a live session world.

func (*Instance) ID

func (i *Instance) ID() InstanceID

ID returns the instance's id.

func (*Instance) Objective

func (i *Instance) Objective(obj ObjectiveID) ObjectiveState

Objective returns the current state of an objective. Returns ObjectiveNotStarted for unknown objectives — callers that need to distinguish "not started" from "doesn't exist" should consult the Definition.

func (*Instance) Objectives

func (i *Instance) Objectives() map[ObjectiveID]ObjectiveState

Objectives returns a snapshot of all objective states.

func (*Instance) PlayerEnter

func (i *Instance) PlayerEnter(ctx context.Context, player string) error

PlayerEnter adds a player to the roster and fires OnPlayerEnter.

func (*Instance) PlayerExit

func (i *Instance) PlayerExit(ctx context.Context, player string) error

PlayerExit removes a player from the roster and fires OnPlayerExit.

func (*Instance) Players

func (i *Instance) Players() []string

Players returns the current player roster.

func (*Instance) SetObjective

func (i *Instance) SetObjective(ctx context.Context, obj ObjectiveID, next ObjectiveState) error

SetObjective transitions an objective's state, fires the Definition's OnObjective hook, then runs any bindings that match (objective, newState). Returns ErrTerminalState if the objective is already completed/failed.

func (*Instance) SpawnPlan

func (i *Instance) SpawnPlan() SpawnPlan

SpawnPlan returns the loader-provided spawn data.

type InstanceID

type InstanceID string

InstanceID uniquely identifies a live instance.

type Loader

type Loader interface {
	Load(ctx context.Context, def *Definition, inst InstanceID) (SpawnPlan, error)
}

Loader provides spawn data for a Definition. The implementation typically calls into a spawn-service or content pack. The kit never imports those; the Loader is the seam.

type Manager

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

Manager creates, looks up, and destroys instances of a single Definition. Safe for concurrent use.

func NewManager

func NewManager(def *Definition) *Manager

NewManager constructs a Manager for the given Definition.

func (*Manager) Count

func (m *Manager) Count() int

Count returns the number of live instances.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, id InstanceID) (*Instance, error)

Create instantiates a new Instance. The Loader is consulted for spawn data, OnCreate fires, and the instance is registered for lookup via Get.

func (*Manager) Destroy

func (m *Manager) Destroy(ctx context.Context, id InstanceID) error

Destroy fires OnDestroy and removes the instance from the manager.

func (*Manager) Get

func (m *Manager) Get(id InstanceID) (*Instance, error)

Get returns the live Instance for id, or ErrUnknownInstance.

type ObjectiveID

type ObjectiveID string

ObjectiveID names one tracked objective in a Definition.

type ObjectiveState

type ObjectiveState uint8

ObjectiveState enumerates the lifecycle of a single objective.

const (
	// ObjectiveNotStarted — initial state.
	ObjectiveNotStarted ObjectiveState = iota
	// ObjectiveInProgress — actively being worked on.
	ObjectiveInProgress
	// ObjectiveCompleted — succeeded; terminal.
	ObjectiveCompleted
	// ObjectiveFailed — failed; terminal.
	ObjectiveFailed
)

func (ObjectiveState) String

func (s ObjectiveState) String() string

String returns the human-readable label.

type SpawnPlan

type SpawnPlan any

SpawnPlan describes the entity spawn data the runtime should instantiate. The shape is intentionally opaque — callers cast to the concrete type their Loader produces.

Jump to

Keyboard shortcuts

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