Documentation
¶
Overview ¶
Package hostbridge is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package hostbridge connects a game's loop and mixer to an embedding page.
Index ¶
- Constants
- type Bridge
- func (b *Bridge) GameplayStarted()
- func (b *Bridge) GameplayStopped()
- func (b *Bridge) HoldPause() func()
- func (b *Bridge) Loaded()
- func (b *Bridge) LoadingProgress(loaded, total int64)
- func (b *Bridge) OnEvent(fn func(event string)) func()
- func (b *Bridge) SuspendAudio() func()
- func (b *Bridge) WatchGroup(g *assets.Group)
Examples ¶
Constants ¶
const ( // EventPause asks the game to pause its loop. EventPause = "pause" // EventResume ends a host pause; the loop resumes unless the game had paused it before. EventResume = "resume" // EventMute asks the game to suspend its mixer. EventMute = "mute" // EventUnmute resumes a mixer suspended by EventMute. EventUnmute = "unmute" // EventLoadingProgress carries {"loaded": n, "total": n} bytes or items. EventLoadingProgress = "loadingProgress" // EventLoaded reports that loading finished. EventLoaded = "loaded" // EventGameplayStart reports that the player is in gameplay. EventGameplayStart = "gameplayStart" // EventGameplayStop reports that gameplay stopped, such as at a menu or game over. EventGameplayStop = "gameplayStop" )
Host event names. The embedding page sends EventPause, EventResume, EventMute and EventUnmute; the game posts the others.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bridge ¶
type Bridge struct {
// contains filtered or unexported fields
}
Bridge exposes the host embedding contract.
func Attach ¶
Attach listens for the stable pause and mute events sent by an embedding host.
Example ¶
Attach connects the game loop and mixer to an embedding page's pause and mute events. WatchGroup reports the first asset group's loading, and the bridge reports gameplay changes back. The example is compiled but not run: it needs a running NSGo app scope with a loop and a mixer.
package main
import (
"fmt"
"github.com/nsgo-dev/game"
"github.com/nsgo-dev/game/pkg/assets"
"github.com/nsgo-dev/game/pkg/hostbridge"
"github.com/nsgo-dev/game/pkg/sound"
"github.com/nsgo-dev/nsgo/core"
)
func main() {
var (
cx *core.Scope
loop *game.Loop
mixer *sound.Mixer
first *assets.Group
)
bridge := hostbridge.Attach(cx, loop, mixer)
bridge.WatchGroup(first)
bridge.GameplayStarted()
bridge.GameplayStopped()
fmt.Println(bridge != nil)
}
Output:
func (*Bridge) GameplayStarted ¶
func (b *Bridge) GameplayStarted()
GameplayStarted reports gameplay start to the host.
func (*Bridge) GameplayStopped ¶
func (b *Bridge) GameplayStopped()
GameplayStopped reports gameplay stop to the host.
func (*Bridge) HoldPause ¶
func (b *Bridge) HoldPause() func()
HoldPause acquires a pause hold on the bridge's loop and returns an idempotent release function.
When the first hold is acquired, Bridge remembers whether the loop was already paused by the game. Releasing the last hold resumes the loop only if it was not already paused before the hold began, so overlapping host pauses, portal ad breaks and game-side pause menus do not overwrite each other.
func (*Bridge) LoadingProgress ¶
LoadingProgress reports loading progress to the host.
func (*Bridge) OnEvent ¶
OnEvent registers fn to be notified whenever Loaded, GameplayStarted, or GameplayStopped runs (receiving EventLoaded, EventGameplayStart, or EventGameplayStop), and returns an idempotent unsubscribe function.
func (*Bridge) SuspendAudio ¶
func (b *Bridge) SuspendAudio() func()
SuspendAudio acquires a suspension hold on the bridge's mixer and returns an idempotent release function. Multiple concurrent holds (such as an embedding host mute, a portal muteAudio setting, and an ad break) compose through sound.Mixer.Suspend.
func (*Bridge) WatchGroup ¶
WatchGroup reports g's loading progress to the host and posts EventLoaded once g finishes.
It posts EventLoadingProgress whenever the group's loaded or total count changes, including the counts it starts with, and follows the group until the bridge's owner is disposed. Call it once with the first group the game loads, so a page's loader can show the game's asset loading after the module download. It does nothing without a scope or with a nil group; without an embedding host the posts are dropped.