Справочник API собран из Go doc-комментариев в исходном коде и написан на английском языке.
package game
import "github.com/nsgo-dev/game"Обзор
Package game is a game layer for NSGo apps: a fixed-step loop, input actions, sprites, tilemaps, scenes and sound, built on NSGo's public API only, so an app that does not import it links none of it.
This package holds Loop, which the others share. Each domain has a package of its own: math2d for 2D vectors, rectangles, circles and affine transforms, sprite for atlases, sprites, animations and batches, and sound for the mixer, its effects and music; input, tilemap (with the camera) and scene follow, and examples holds sample games.
Указатель
- type LeaveMode
- type Loop
- func (l *Loop) EffectiveTimeScale() float64
- func (l *Loop) Elapsed() time.Duration
- func (l *Loop) ElapsedSeconds() float64
- func (l *Loop) HandleLeave(background bool)
- func (l *Loop) MarkDirty()
- func (l *Loop) Run(cx *core.Scope) (stop func())
- func (l *Loop) SetPaused(paused bool)
- func (l *Loop) SetTimeScale(scale float64)
- func (l *Loop) Step(n int)
- func (l *Loop) StepDuration() time.Duration
- func (l *Loop) StepNow(n int) int
- func (l *Loop) StepOnce()
- func (l *Loop) StepRate() int
- func (l *Loop) Steps() uint64
Типы
type LeaveMode uint8LeaveMode controls which lifecycle transitions pause a Loop.
const (
// LeaveNone leaves lifecycle handling to the caller.
LeaveNone LeaveMode = iota
// LeaveBackground pauses when the scope enters LifecycleBackground.
LeaveBackground
// LeaveInactive pauses when the scope is anything other than LifecycleActive.
LeaveInactive
)type Loop struct {
// Rate is how many updates run a second, 60 when 0.
Rate int
// MaxSteps is the most updates one frame runs, Rate/10 (a tenth of a second's updates) and at least 1 when 0. A frame that would run more drops the whole steps past it, so an Update slower than its step, or a long hitch, slows the game rather than making each frame run more updates than the last.
MaxSteps int
// Paused stops Update from advancing with display time while Draw keeps running. Unpausing resumes from the current frame without a catch-up backlog.
Paused bool
// PauseOnLeave pauses the loop when its scope leaves the foreground. The pause is sticky, so returning to the foreground never resumes gameplay without an explicit SetPaused(false).
PauseOnLeave LeaveMode
// PausedAwake is how long the loop keeps the display awake (core.Scope.KeepAwake) after it is Paused or while an OnDemand loop is idle (Idle returns true with no MarkDirty or updates). Zero or negative releases the display hold at once.
PausedAwake time.Duration
// TimeScale scales simulated time, 1 when 0 or less (or not finite). Update still receives the fixed 1/Rate step, so 0.5 runs half as many updates a second and 2 runs twice as many.
TimeScale float64
// DrawRate caps Draw to at most DrawRate calls a second, uncapped when 0 or less. Update still runs at Rate.
DrawRate int
// MatchDisplayToRate asks the display to run at Rate when DrawRate is uncapped. It is useful when the game wants to avoid display wakeups above its simulation rate.
MatchDisplayToRate bool
// OnDemand skips Draw on frames where no Update ran, unless MarkDirty has been called since the last Draw. When Idle is also set and returns true, OnDemand skips Update steps too until Idle returns false or MarkDirty wakes the loop.
OnDemand bool
// Idle, when set with OnDemand, reports whether the game currently has no ongoing work (such as animations, timers or typewriter reveals) that needs fixed-step updates. While OnDemand is true, Paused is false and Idle returns true, display frames skip both Update and Draw without accumulating time, and the display keep-awake hold is released after PausedAwake. Calling MarkDirty wakes an idle loop to run one fixed Update step and Draw on the next frame; if that Update makes Idle return false, fixed-step updates resume until Idle returns true again.
Idle func() bool
// Immersive hides a phone's system bars (mobile.Immersive) while the game plays, that is while the loop runs and is not Paused, and brings them back while it is Paused, as under a pause menu, and once it stops or its scope is disposed. The loop hides them as it starts or resumes in the foreground and applies a change to Immersive or Paused on its next frame, and leaves the bars as they are while the app is in the background. Desktop and web targets ignore it.
Immersive bool
// Update moves the game on by dt, which is always 1/Rate seconds.
Update func(dt time.Duration)
// Draw draws the frame; alpha, from 0 up to but not including 1, is how far the frame falls between the last update and the next.
Draw func(alpha float64)
// FrameTime, when set, receives each frame's length, the time it adds (0 for a frame stamped before the run started or resumed), ahead of the frame's updates. It is what frame-time statistics such as quality.Selector's read; a frame that ends a pause in the background measures from the time the window showed again, not the time away. It fires while Paused is set too, so a caller measuring only frames that simulate skips those itself.
FrameTime func(dt time.Duration)
// OnPause, when set, runs whenever Paused changes, so subsystems such as sound.Mixer (sound.Mixer.SetPaused) pause and resume with the loop. SetPaused calls it at once when the state changes; changing Paused directly calls it on the next frame, or in the same frame when Update changes it. If Paused is already true when Run starts, Run calls OnPause(true). Backgrounding does not call OnPause; both Loop and sound.Mixer follow core.Env.Lifecycle directly.
OnPause func(paused bool)
// OnLeave runs once when the scope leaves the foreground, with background true for core.LifecycleBackground, and on quit for a run that has not stopped or already handled backgrounding. It runs before PauseOnLeave pauses the loop, untracked, so the signals it reads do not subscribe the loop's lifecycle effect.
OnLeave func(background bool)
// contains filtered or unexported fields
}Loop runs a game's updates at a fixed rate on a scope's display frames, and draws once a frame in between.
Each frame adds the time since the last one and runs Update once for each whole step of 1/Rate seconds it holds, zero or more times, then Draw once with how far the time left over reaches into the next step. A game moves its state in Update by the fixed dt, so it plays alike at any display rate, and Draw places what moves between the last two states by alpha, so it moves smoothly at any display rate.
Run reads the fields on each frame, so a game may change them while the loop runs.
func (l *Loop) EffectiveTimeScale() float64EffectiveTimeScale returns the simulation time scale actually applied by Run (TimeScale when finite and > 0, else 1).
func (l *Loop) Elapsed() time.DurationElapsed returns the simulated game time advanced by fixed Update steps, rounded to the nearest nanosecond from ElapsedSeconds (so 60 steps at 60 Hz is exactly time.Second).
func (l *Loop) ElapsedSeconds() float64ElapsedSeconds returns the simulated game time advanced by fixed Update steps in seconds. Each contiguous run at a fixed rate contributes steps/rate seconds without integer-nanosecond drift, so 60 steps at 60 Hz is exactly 1.0.
func (l *Loop) HandleLeave(background bool)HandleLeave applies leave policy and invokes OnLeave once for a transition away from active.
func (l *Loop) MarkDirty()MarkDirty requests a Draw on the next frame when OnDemand is true. When an unpaused OnDemand loop is idle (Idle returns true), MarkDirty also queues one fixed Update step on the next frame so Update can sample newly arrived input and advance state.
func (l *Loop) Run(cx *core.Scope) (stop func())Run starts the loop on cx's display frames until cx is disposed or stop is called.
The loop pauses while cx's window is in the background (core.Env.Lifecycle), that is hidden, minimized or covered on a desktop and the app in the background on a phone, and resumes from the time it shows again, so the time away runs no updates. While running in the foreground and active (not Paused and not idle under OnDemand and Idle) it keeps the display awake (core.Scope.KeepAwake). It releases the display hold immediately on background, stop or scope disposal, and once the loop has been Paused or OnDemand-idle (with no MarkDirty or updates) for PausedAwake (at once when PausedAwake <= 0), taking it again on unpause, when Idle returns false, and while pending Step updates or an OnDemand MarkDirty wake/draw run.
A frame adds the time between its display time and the last frame's, as core.Scope.RequestAnimationFrame gives it: the display's own frame time where the platform stamps one, so the frame length is the display's rather than when the engine got to the frame, and the graph clock's otherwise. The first frame after the loop starts or resumes adds the time from then to its display time, which may lie a frame ahead where the display stamps a frame with when it will show, so the loop's time is the shown frame's. A frame stamped before the time the loop started or resumed adds nothing, and the next frame adds the time from then.
With Immersive set, the run hides the system bars while the game plays and shows them again when it is paused, stopped or disposed. Runs in the same window share the bars: they stay hidden while any of them plays with Immersive set, and come back once the last of those pauses or ends, so a scene that starts its own loop before the last scene's loop is disposed keeps them hidden.
func (l *Loop) SetPaused(paused bool)SetPaused pauses or resumes the loop's updates without accumulating a catch-up backlog, calling OnPause if the paused state changed.
func (l *Loop) SetTimeScale(scale float64)SetTimeScale sets the simulation time scale; values at or below 0 run at 1.
func (l *Loop) Step(n int)Step queues n fixed updates to run on the next display frame while Paused is true, redrawing afterward. Values at or below 0 do nothing.
func (l *Loop) StepDuration() time.DurationStepDuration returns the fixed duration passed to Update (time.Second / StepRate()).
func (l *Loop) StepNow(n int) intStepNow synchronously executes n fixed Update steps right away (whether Paused is true or false), followed by a Draw when Draw is set, and returns how many steps ran. Values at or below 0 do nothing and return 0. If Update stops an active Run, StepNow stops immediately without calling Draw.
func (l *Loop) StepOnce()StepOnce queues one fixed update to run on the next display frame while Paused is true.
func (l *Loop) StepRate() intStepRate returns the effective fixed update rate in Hz (Rate when > 0, else 60).
func (l *Loop) Steps() uint64Steps returns the total number of fixed Update steps executed by l.