Documentation
¶
Overview ¶
+game:group:name=room
Package room moves the game between rooms: levels named by their base key ("cave/cave": cave.level.json plus cave.tmj or cave.world, see game.Engine.LoadLevelBase) entered through exits placed in the level editor, with a screen transition that covers the swap.
A room.Exit is an area in a GOD level with a target room and a spawn name; a room.Spawn names a point; a room.Traveler (the hero) triggers exits, survives the swap and lands on the spawn. Install adds the director to an engine and returns its Rooms; Rooms.GoTo does the same from code. Transitions are a fade by default, or any registered Transition: a post shader (Shader), a function (Func) or a type of your own. See doc/guide/engine/rooms.md.
Package game never imports room: rooms are opt-in.
Index ¶
- Constants
- Variables
- type Config
- type Entered
- type Exit
- type ExitArea
- type ExitAreaView
- func (v ExitAreaView) Each() iter.Seq2[ecs.Entity, ExitArea]
- func (v ExitAreaView) Get(e ecs.Entity) ExitArea
- func (v ExitAreaView) Has(e ecs.Entity) bool
- func (v ExitAreaView) NewEntity(obj ExitArea) ecs.Entity
- func (v ExitAreaView) Remove(e ecs.Entity)
- func (v ExitAreaView) Set(e ecs.Entity, obj ExitArea)
- type ExitTrigger
- type Frame
- type Left
- type Option
- type Phase
- type Rooms
- func (r *Rooms) Busy() bool
- func (r *Rooms) Current() []*game.Level
- func (r *Rooms) GoTo(id, spawn string, opts ...Option) bool
- func (r *Rooms) Phase() Phase
- func (r *Rooms) Register(name string, t Transition)
- func (r *Rooms) Room() string
- func (r *Rooms) Run(ctx *system.Context)
- func (r *Rooms) RunsWhilePaused() bool
- func (r *Rooms) SetCurrent(room string, levels ...*game.Level)
- func (r *Rooms) Start(id, spawn string) bool
- type Spawn
- type SpawnPoint
- type SpawnPointView
- func (v SpawnPointView) Each() iter.Seq2[ecs.Entity, SpawnPoint]
- func (v SpawnPointView) Get(e ecs.Entity) SpawnPoint
- func (v SpawnPointView) Has(e ecs.Entity) bool
- func (v SpawnPointView) NewEntity(obj SpawnPoint) ecs.Entity
- func (v SpawnPointView) Remove(e ecs.Entity)
- func (v SpawnPointView) Set(e ecs.Entity, obj SpawnPoint)
- type Transition
- type Traveler
Constants ¶
const ( DefaultOut = 0.25 DefaultIn = 0.25 )
Default transition timing, in seconds, for a zero Config.Out or Config.In.
Variables ¶
var ( CID_Exit = ecs.RegisterComp[Exit]() CID_Spawn = ecs.RegisterComp[Spawn]() CID_Traveler = ecs.RegisterComp[Traveler]() CID_GfxTransform = ecs.RegisterComp[gfx.Transform]() )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Spawn returns the tilemap spawn options for the room id; nil uses the
// zero options.
Spawn func(id string) tilemap.SpawnOptions
// OnEnter runs after every swap (and after Start, and for a teleport),
// while the screen is still covered and before the travelers are
// placed: per-room setup such as music, render layers or saving. It may
// add levels to the room with SetCurrent (a GOD level loaded beside the
// room's base level); the director waits for them to spawn and looks for the spawn
// point in them too. The camera bounds are already set to the room's
// maps; set CameraRig().Bounds here to replace them, and the director
// keeps yours.
OnEnter func(Entered)
// OnLeave runs when a GoTo leaves a room for another one, while the
// screen is covered and before the old room's levels unload: the last
// moment to read their objects, for example to save the room's state.
// It does not run for Start (no room yet) or a teleport inside a room.
OnLeave func(Left)
// OnError receives what goes wrong in a transition (an unknown room,
// spawn, transition or action, a room that fails to load); nil logs it.
OnError func(error)
// Default names the transition of exits and GoTo calls that name none;
// empty means "fade".
Default string
// Out and In are the default durations of covering and uncovering, in
// seconds. Zero means DefaultOut / DefaultIn; negative means none.
Out, In float32
// KeepSimulating, when set, leaves StageSimulate running during
// transitions. By default the director holds it (game.Engine.Hold), so
// the hero cannot walk on while the screen is covered.
KeepSimulating bool
// NoExits keeps exits from firing (they still get their debug shapes);
// GoTo still works. Editor builds set it, so playing a level in the
// editor never unloads it.
NoExits bool
// Scene, when set, makes the director a system of that scene instead of
// the root, and loads rooms through it (game.Scene.LoadLevelBase), so
// popping or replacing the scene unloads the current room. The scene
// must not have been pushed yet; the room loads need it pushed, so the
// first GoTo comes from its OnEnter or a system. Nil: the root.
Scene *game.Scene
}
Config configures the director Install adds. The zero value works: rooms spawn with zero tilemap.SpawnOptions, exits fade through black for 0.25 s each way, errors are logged.
type Entered ¶
type Entered struct {
// From and To are the room ids ("" before the first room). They are
// equal for a teleport inside a room.
From, To string
// Spawn is the spawn name the travelers were placed on.
Spawn string
// Levels are the levels of the room now current (Rooms.Current).
Levels []*game.Level
}
Entered describes a swap, for Config.OnEnter. The director also sends it as an event (system.EventsOf[Entered]) after OnEnter, for systems that react to room changes (music, a room name in the HUD).
type Exit ¶
type Exit struct {
// Target is the room to go to: a level's base key ("cave/cave", see
// game.Engine.LoadLevelBase). Empty means the current room (a teleport).
Target string
// Spawn names the arrival point in the target room: a room:Spawn with
// that Name, else a Tiled object with that name.
Spawn string
// Size is the width and height of the area, in world units.
Size gfx.Vec2
// Trigger says what fires the exit: 0 walking in (OnEnter), 1 an input
// action (OnAction).
Trigger ExitTrigger
// Action is the input action an OnAction exit waits for ("interact"),
// pressed by the traveler's PlayerSlot player (player 0 without one).
Action string
// Transition is the name of a registered transition; empty uses the
// default ("fade" unless Config.Default says otherwise).
Transition string
}
Exit is an area that sends the travelers to another room. The area is the rectangle from the entity's Transform.Pos (its top-left corner) with Size, as a gfx.DebugShape rect draws it; rotation and scale are ignored. The director gives every exit such a debug shape, so the debug overlay and the editor show the area.
+game:component
type ExitArea ¶
type ExitArea struct {
// The top-left corner of the area.
// +game:component
Transform *gfx.Transform
// The target room and spawn.
// +game:component
Exit *Exit
}
ExitArea is the Game Object of an exit: where it is and where it leads. +game:object
type ExitAreaView ¶
type ExitAreaView struct {
// contains filtered or unexported fields
}
ExitAreaView is a thin handle over ExitArea entities, bound to one World. It holds no entity state of its own.
func NewExitAreaView ¶
func NewExitAreaView(world *ecs.World) ExitAreaView
NewExitAreaView returns a ExitAreaView bound to world. Create it once and keep it; copies share the same underlying state.
func (ExitAreaView) Each ¶
Each yields every entity that has all required components of ExitArea, regardless of how it was created. Optional fields are always resolved, so Each yields the same ExitArea that Get would return for the same entity.
Panics with "ecs: not bound to a world" if ExitAreaView is the zero value.
func (ExitAreaView) Get ¶
func (v ExitAreaView) Get(e ecs.Entity) ExitArea
Get returns a ExitArea whose component fields point to e's live components. Optional fields are resolved (nil when absent). Panics if e lacks a required component, and with "ecs: not bound to a world" if ExitAreaView is the zero value.
func (ExitAreaView) Has ¶
func (v ExitAreaView) Has(e ecs.Entity) bool
Has reports whether e has every required component of ExitArea, i.e. whether Get would not panic. Panics if e is stale, and with "ecs: not bound to a world" if ExitAreaView is the zero value.
func (ExitAreaView) NewEntity ¶
func (v ExitAreaView) NewEntity(obj ExitArea) ecs.Entity
NewEntity creates an entity from obj. A nil required pointer component is stored as the zero value. An optional component is added only when non-nil.
Panics with "ecs: not bound to a world" if ExitAreaView is the zero value.
func (ExitAreaView) Remove ¶
func (v ExitAreaView) Remove(e ecs.Entity)
Remove removes all of ExitArea's components (required and optional) that e has, in one archetype move. Other components stay.
Panics with "ecs: not bound to a world" if ExitAreaView is the zero value.
func (ExitAreaView) Set ¶
func (v ExitAreaView) Set(e ecs.Entity, obj ExitArea)
Set writes obj to e: required components are added or replaced (a nil required pointer overwrites the component with its zero value); optional components are added or replaced when non-nil and removed when nil. Components of e that are not part of ExitArea are left untouched.
Panics with "ecs: not bound to a world" if ExitAreaView is the zero value.
type ExitTrigger ¶
type ExitTrigger uint8
ExitTrigger says what fires an Exit.
+game:enum
const ( // OnEnter fires when a Traveler walks into the area: it is inside this // tick and was outside the tick before, so a traveler that arrives on a // spawn inside an exit does not leave at once. OnEnter ExitTrigger = iota // OnAction fires when a Traveler inside the area presses the input // action named Exit.Action (a door that needs a button). OnAction )
Exit triggers.
type Frame ¶
type Frame struct {
// Engine is the engine the director runs in.
Engine *game.Engine
// Phase is Out, Wait or In, and Idle in End.
Phase Phase
// T is the raw coverage: 0 open, 1 covered. It rises during Out, is 1
// during Wait and falls during In.
T float32
// Progress is T eased with tween.InOutSine: use it for the visuals.
Progress float32
// Focus is the first traveler's position in logical screen pixels (in
// the old room during Out, in the new one during In), or the screen
// centre without travelers: the centre of an iris.
Focus gfx.Vec2
// From and To are the room ids; Spawn is the arrival point's name.
From, To, Spawn string
}
Frame is what a Transition sees of the running transition. The director owns it; a transition reads it during the call only.
type Left ¶
type Left struct {
// From is the room being left, To the room being entered.
From, To string
// Levels are the levels of the room being left; they unload right after
// OnLeave returns.
Levels []*game.Level
}
Left describes a room the travelers leave, for Config.OnLeave.
type Option ¶
type Option func(*options)
Option changes one GoTo call.
type Phase ¶
type Phase uint8
Phase is the part of a transition that is running.
type Rooms ¶
type Rooms struct {
// contains filtered or unexported fields
}
Rooms is the director: it watches exits, runs transitions and swaps rooms. Get it from Install. Use it from systems and Start hooks (the World is single-goroutine).
func Install ¶
Install adds the director to e, in StagePost, and returns it (to Config.Scene instead of the root when set). Call it once, before the first Update (like Engine.AddRunner), or before Config.Scene is pushed. The director runs while a Tool pauses the game only to give exits and spawns their debug shapes; exits do not fire then.
func (*Rooms) Busy ¶
Busy reports whether a transition runs. Exits do not fire and GoTo does nothing while it does.
func (*Rooms) Current ¶
Current returns the levels the current room consists of: the base level a swap loaded, or what SetCurrent set. Leaving the room unloads them all.
func (*Rooms) GoTo ¶
GoTo starts a transition to the room id (a level base key, "cave/cave") with the travelers arriving on spawn. An empty id means the current room: the travelers move behind the transition and nothing loads. It returns false, and does nothing, while another transition runs. Problems found later (an unknown room or spawn) go to Config.OnError and leave the travelers where they were.
func (*Rooms) Register ¶
func (r *Rooms) Register(name string, t Transition)
Register makes t available under name for Exit.Transition, With and Config.Default, replacing an earlier one of that name. "fade" and "cut" are registered by Install.
func (*Rooms) Run ¶
Run marks new exits and spawns, then fires exits or advances the running transition.
func (*Rooms) RunsWhilePaused ¶
RunsWhilePaused makes the director run under a Tool's TickPause, where it only marks exits and spawns (see Install).
func (*Rooms) SetCurrent ¶
SetCurrent makes levels the current room, with id room, for games that load their first room themselves (a base level plus an extra GOD level, say). The next swap unloads them.
type Spawn ¶
type Spawn struct {
// Name is what Exit.Spawn and Rooms.GoTo look for.
Name string
}
Spawn names an arrival point at the entity's Transform.Pos.
+game:component
type SpawnPoint ¶
type SpawnPoint struct {
// Where travelers arrive.
// +game:component
Transform *gfx.Transform
// The point's name.
// +game:component
Spawn *Spawn
}
SpawnPoint is the Game Object of a named arrival point. +game:object
type SpawnPointView ¶
type SpawnPointView struct {
// contains filtered or unexported fields
}
SpawnPointView is a thin handle over SpawnPoint entities, bound to one World. It holds no entity state of its own.
func NewSpawnPointView ¶
func NewSpawnPointView(world *ecs.World) SpawnPointView
NewSpawnPointView returns a SpawnPointView bound to world. Create it once and keep it; copies share the same underlying state.
func (SpawnPointView) Each ¶
func (v SpawnPointView) Each() iter.Seq2[ecs.Entity, SpawnPoint]
Each yields every entity that has all required components of SpawnPoint, regardless of how it was created. Optional fields are always resolved, so Each yields the same SpawnPoint that Get would return for the same entity.
Panics with "ecs: not bound to a world" if SpawnPointView is the zero value.
func (SpawnPointView) Get ¶
func (v SpawnPointView) Get(e ecs.Entity) SpawnPoint
Get returns a SpawnPoint whose component fields point to e's live components. Optional fields are resolved (nil when absent). Panics if e lacks a required component, and with "ecs: not bound to a world" if SpawnPointView is the zero value.
func (SpawnPointView) Has ¶
func (v SpawnPointView) Has(e ecs.Entity) bool
Has reports whether e has every required component of SpawnPoint, i.e. whether Get would not panic. Panics if e is stale, and with "ecs: not bound to a world" if SpawnPointView is the zero value.
func (SpawnPointView) NewEntity ¶
func (v SpawnPointView) NewEntity(obj SpawnPoint) ecs.Entity
NewEntity creates an entity from obj. A nil required pointer component is stored as the zero value. An optional component is added only when non-nil.
Panics with "ecs: not bound to a world" if SpawnPointView is the zero value.
func (SpawnPointView) Remove ¶
func (v SpawnPointView) Remove(e ecs.Entity)
Remove removes all of SpawnPoint's components (required and optional) that e has, in one archetype move. Other components stay.
Panics with "ecs: not bound to a world" if SpawnPointView is the zero value.
func (SpawnPointView) Set ¶
func (v SpawnPointView) Set(e ecs.Entity, obj SpawnPoint)
Set writes obj to e: required components are added or replaced (a nil required pointer overwrites the component with its zero value); optional components are added or replaced when non-nil and removed when nil. Components of e that are not part of SpawnPoint are left untouched.
Panics with "ecs: not bound to a world" if SpawnPointView is the zero value.
type Transition ¶
Transition draws a room transition. Begin runs when the transition starts (in GoTo), Update every tick after it, and End once the new room is uncovered. The swap happens between the Update with Progress 1 that ends Wait and the first Update of In, so a transition must cover the whole screen at Progress 1 to hide it. One Transition value is used by one transition at a time.
func Cut ¶
func Cut() Transition
Cut returns the transition that shows nothing: the rooms swap from one frame to the next. Use it with Seconds(0, 0) for an instant swap.
func Fade ¶
func Fade(color gfx.Color) Transition
Fade returns the transition that fades the screen to color and back, through render.PostChain.Fade (no shader). The color's alpha is the strongest cover; the zero color fades to black.
func Func ¶
func Func(f func(*Frame)) Transition
Func returns a Transition that calls f every tick, and once more at End with Progress 0 and Phase Idle, so f can restore what it changed (a zoom, a tint).
func Shader ¶
func Shader(e *game.Engine, key string) Transition
Shader returns a transition drawn by the post shader at key, which it loads into e's global scope at once. While the transition runs, the director appends a pass to e.Post().Passes (do not replace Passes then) and removes it at End. The shader's Params uniform holds:
- x: Frame.Progress, 0 open to 1 covered;
- y, z: Frame.Focus, in logical screen pixels;
- w: 0 while covering (Out, Wait), 1 while uncovering (In).
A shader that is not loaded yet when a transition starts makes that transition a black fade (and says so once in the log).