goonscape/game/context.go

39 lines
642 B
Go
Raw Normal View History

2025-02-14 13:35:09 +01:00
package game
import (
"context"
"sync"
)
type GameContext struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
assetsLock sync.RWMutex
}
func NewGameContext() *GameContext {
ctx, cancel := context.WithCancel(context.Background())
return &GameContext{
ctx: ctx,
cancel: cancel,
}
}
func (gc *GameContext) Shutdown() {
gc.cancel()
gc.wg.Wait()
}
func (gc *GameContext) LoadAssets(fn func() error) error {
gc.assetsLock.Lock()
defer gc.assetsLock.Unlock()
return fn()
}
func (gc *GameContext) UnloadAssets(fn func()) {
gc.assetsLock.Lock()
defer gc.assetsLock.Unlock()
fn()
}