ichigo/engine/game.go

258 lines
6.1 KiB
Go
Raw Normal View History

2021-07-23 13:12:54 +10:00
package engine
2021-08-01 16:10:30 +10:00
import (
"encoding/gob"
2021-08-30 16:25:50 +10:00
"errors"
2021-08-27 14:30:39 +10:00
"fmt"
2021-08-22 20:27:08 +10:00
"io/fs"
2021-08-27 15:39:10 +10:00
"reflect"
2021-08-25 16:46:30 +10:00
"sync"
2021-08-01 16:10:30 +10:00
"github.com/hajimehoshi/ebiten/v2"
)
2021-08-28 18:14:37 +10:00
var _ interface {
Disabler
Hider
Identifier
Updater
Scanner
} = &Game{}
2021-08-30 16:25:50 +10:00
var (
errNilComponent = errors.New("nil component")
errNilParent = errors.New("nil parent")
)
2021-08-01 16:10:30 +10:00
func init() {
2021-08-25 15:04:38 +10:00
gob.Register(&Game{})
2021-08-01 16:10:30 +10:00
}
2021-08-27 16:48:09 +10:00
// Game implements the ebiten methods using a collection of components. One
// component must be the designated root component - usually a scene of some
// kind.
2021-07-23 13:12:54 +10:00
type Game struct {
2021-08-25 16:46:30 +10:00
Disabled
Hidden
2021-07-23 13:13:50 +10:00
ScreenWidth int
ScreenHeight int
2021-08-20 16:46:26 +10:00
Root DrawUpdater // typically a *Scene or SceneRef though
2021-08-01 16:10:30 +10:00
2021-08-25 16:46:30 +10:00
dbmu sync.RWMutex
2021-08-30 15:03:22 +10:00
byID map[string]Identifier // Named components by ID
byAB map[abKey]map[interface{}]struct{} // Ancestor/behaviour index
2021-08-30 15:49:51 +10:00
par map[interface{}]interface{} // par[x] is parent of x
2021-08-27 15:39:10 +10:00
}
2021-08-30 13:33:01 +10:00
type abKey struct {
2021-08-28 18:14:37 +10:00
ancestor string
2021-08-27 15:39:10 +10:00
behaviour reflect.Type
2021-07-23 13:12:54 +10:00
}
2021-08-15 15:52:40 +10:00
// Draw draws the entire thing, with default draw options.
2021-07-23 13:12:54 +10:00
func (g *Game) Draw(screen *ebiten.Image) {
2021-08-25 16:46:30 +10:00
if g.Hidden {
return
}
2021-08-20 16:46:26 +10:00
g.Root.Draw(screen, ebiten.DrawImageOptions{})
2021-07-23 13:12:54 +10:00
}
// Layout returns the configured screen width/height.
func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
return g.ScreenWidth, g.ScreenHeight
}
2021-07-23 13:46:19 +10:00
2021-08-20 16:46:26 +10:00
// Update updates the scene.
2021-08-25 16:46:30 +10:00
func (g *Game) Update() error {
if g.Disabled {
return nil
}
return g.Root.Update()
}
2021-08-20 16:46:26 +10:00
2021-08-28 18:14:37 +10:00
// Ident returns "__GAME__".
func (g *Game) Ident() string { return "__GAME__" }
2021-08-01 17:14:57 +10:00
// Component returns the component with a given ID, or nil if there is none.
2021-08-30 15:49:51 +10:00
// This only returns sensible values for registered components (e.g. after
// LoadAndPrepare).
2021-08-27 14:43:37 +10:00
func (g *Game) Component(id string) Identifier {
2021-08-25 16:46:30 +10:00
g.dbmu.RLock()
defer g.dbmu.RUnlock()
2021-08-30 13:33:01 +10:00
return g.byID[id]
2021-08-25 16:46:30 +10:00
}
2021-08-01 16:10:30 +10:00
2021-08-30 15:49:51 +10:00
// Parent returns the parent of a given component, or nil if there is none.
// This only returns sensible values for registered components (e.g. after
// LoadAndPrepare).
func (g *Game) Parent(c interface{}) interface{} {
g.dbmu.RLock()
defer g.dbmu.RUnlock()
return g.par[c]
}
2021-08-27 15:39:10 +10:00
// Query looks for components having both a given ancestor and implementing
// a given behaviour (see Behaviors in interface.go). This only returns sensible
// values after LoadAndPrepare. Note that every component is its own ancestor.
2021-08-30 15:03:22 +10:00
func (g *Game) Query(ancestorID string, behaviour reflect.Type) map[interface{}]struct{} {
2021-08-27 15:39:10 +10:00
g.dbmu.RLock()
defer g.dbmu.RUnlock()
2021-08-30 13:33:01 +10:00
return g.byAB[abKey{ancestorID, behaviour}]
2021-08-27 15:39:10 +10:00
}
2021-08-04 12:40:51 +10:00
// Scan implements Scanner.
2021-08-20 16:46:26 +10:00
func (g *Game) Scan() []interface{} { return []interface{}{g.Root} }
2021-08-01 16:41:10 +10:00
2021-08-30 16:25:50 +10:00
// Walk calls visit with every component and its parent, reachable from the
// given component via Scan, for as long as visit returns nil. The parent of
// the first component (as passed to visit) will be nil.
func Walk(component interface{}, visit func(component, parent interface{}) error) error {
return walk(component, nil, visit)
2021-08-27 13:56:50 +10:00
}
2021-08-30 16:25:50 +10:00
func walk(component, parent interface{}, visit func(component, parent interface{}) error) error {
if err := visit(component, parent); err != nil {
2021-08-20 15:01:31 +10:00
return err
2021-08-01 16:41:10 +10:00
}
2021-08-30 16:25:50 +10:00
sc, ok := component.(Scanner)
2021-08-15 17:11:26 +10:00
if !ok {
2021-08-20 15:01:31 +10:00
return nil
2021-08-15 17:11:26 +10:00
}
for _, c := range sc.Scan() {
2021-08-30 16:25:50 +10:00
if err := walk(c, component, visit); err != nil {
2021-08-20 15:01:31 +10:00
return err
2021-08-01 16:10:30 +10:00
}
}
2021-08-20 15:01:31 +10:00
return nil
2021-08-01 16:10:30 +10:00
}
2021-08-27 15:39:10 +10:00
// LoadAndPrepare first calls Load on all Loaders. Once loading is complete, it
// builds the component databases and then calls Prepare on every Preparer.
// LoadAndPrepare must be called before any calls to Component or Query.
2021-08-27 13:56:50 +10:00
func (g *Game) LoadAndPrepare(assets fs.FS) error {
2021-08-27 16:46:04 +10:00
// Load all the Loaders.
2021-08-30 16:25:50 +10:00
if err := Walk(g, func(c, _ interface{}) error {
2021-08-20 16:31:06 +10:00
l, ok := c.(Loader)
if !ok {
return nil
}
2021-08-23 10:09:49 +10:00
return l.Load(assets)
2021-08-27 13:56:50 +10:00
}); err != nil {
return err
}
2021-08-27 14:43:37 +10:00
// Build the component databases
2021-08-25 16:46:30 +10:00
g.dbmu.Lock()
2021-08-30 13:33:01 +10:00
g.byID = make(map[string]Identifier)
2021-08-30 15:03:22 +10:00
g.byAB = make(map[abKey]map[interface{}]struct{})
2021-08-30 15:49:51 +10:00
g.par = make(map[interface{}]interface{})
2021-08-30 16:27:12 +10:00
if err := Walk(g, g.register); err != nil {
2021-08-27 14:43:37 +10:00
return err
}
2021-08-25 16:46:30 +10:00
g.dbmu.Unlock()
2021-08-27 13:56:50 +10:00
2021-08-27 14:43:37 +10:00
// Prepare all the Preppers
2021-08-30 15:03:22 +10:00
for p := range g.Query(g.Ident(), PrepperType) {
2021-08-27 16:46:04 +10:00
if err := p.(Prepper).Prepare(g); err != nil {
return err
2021-08-05 12:26:41 +10:00
}
2021-08-27 16:46:04 +10:00
}
return nil
2021-08-01 16:10:30 +10:00
}
2021-08-27 16:59:20 +10:00
2021-08-30 16:27:12 +10:00
// Register registers a component into the component database (as the
2021-08-30 16:25:50 +10:00
// child of a given parent). Passing a nil component or parent is an error.
// Registering multiple components with the same ID is also an error.
2021-08-30 16:27:12 +10:00
func (g *Game) Register(component, parent interface{}) error {
2021-08-30 16:25:50 +10:00
if component == nil {
return errNilComponent
}
if parent == nil && component != g {
return errNilParent
}
g.dbmu.Lock()
defer g.dbmu.Unlock()
2021-08-30 16:27:12 +10:00
return g.register(component, parent)
2021-08-30 16:25:50 +10:00
}
2021-08-30 16:27:12 +10:00
func (g *Game) register(component, parent interface{}) error {
2021-08-30 15:49:51 +10:00
// register in g.par
2021-08-30 16:25:50 +10:00
if parent != nil {
g.par[component] = parent
2021-08-30 15:49:51 +10:00
}
2021-08-30 15:08:22 +10:00
// register in g.byAB
2021-08-30 16:25:50 +10:00
ct := reflect.TypeOf(component)
2021-08-27 16:59:20 +10:00
for _, b := range Behaviours {
if !ct.Implements(b) {
continue
}
2021-08-30 16:25:50 +10:00
// TODO: better than O(len(path)^2) time and memory?
for p := component; p != nil; p = g.par[p] {
2021-08-28 18:14:37 +10:00
i, ok := p.(Identifier)
2021-08-30 15:49:51 +10:00
if !ok {
2021-08-28 18:14:37 +10:00
continue
}
2021-08-30 13:33:01 +10:00
k := abKey{i.Ident(), b}
2021-08-30 15:03:22 +10:00
if g.byAB[k] == nil {
g.byAB[k] = make(map[interface{}]struct{})
}
2021-08-30 16:25:50 +10:00
g.byAB[k][component] = struct{}{}
2021-08-27 16:59:20 +10:00
}
}
2021-08-30 15:49:51 +10:00
// register in g.byID if needed
2021-08-30 16:25:50 +10:00
i, ok := component.(Identifier)
2021-08-30 15:49:51 +10:00
if !ok {
2021-08-27 16:59:20 +10:00
return nil
}
id := i.Ident()
2021-08-30 13:33:01 +10:00
if _, exists := g.byID[id]; exists {
2021-08-27 16:59:20 +10:00
return fmt.Errorf("duplicate id %q", id)
}
2021-08-30 13:33:01 +10:00
g.byID[id] = i
2021-08-27 16:59:20 +10:00
return nil
}
2021-08-30 15:07:57 +10:00
2021-08-30 16:27:12 +10:00
// Unregister removes the component from the component database.
2021-08-30 16:25:50 +10:00
// Passing a nil component has no effect.
2021-08-30 16:27:12 +10:00
func (g *Game) Unregister(component interface{}) {
2021-08-30 16:25:50 +10:00
if component == nil {
return
}
2021-08-30 15:49:51 +10:00
g.dbmu.Lock()
2021-08-30 16:27:12 +10:00
g.unregister(component)
2021-08-30 15:49:51 +10:00
g.dbmu.Unlock()
}
2021-08-30 16:27:12 +10:00
func (g *Game) unregister(component interface{}) {
2021-08-30 15:49:51 +10:00
// unregister from g.byAB, using g.par to trace the path
2021-08-30 16:25:50 +10:00
ct := reflect.TypeOf(component)
2021-08-30 15:07:57 +10:00
for _, b := range Behaviours {
if !ct.Implements(b) {
continue
}
2021-08-30 16:25:50 +10:00
for p := component; p != nil; p = g.par[p] {
2021-08-30 15:07:57 +10:00
i, ok := p.(Identifier)
2021-08-30 15:49:51 +10:00
if !ok {
2021-08-30 15:07:57 +10:00
continue
}
k := abKey{i.Ident(), b}
if g.byAB[k] == nil {
continue
}
2021-08-30 16:25:50 +10:00
delete(g.byAB[k], component)
2021-08-30 15:07:57 +10:00
}
}
2021-08-30 15:49:51 +10:00
// unregister from g.par
2021-08-30 16:25:50 +10:00
delete(g.par, component)
2021-08-30 15:49:51 +10:00
// unregister from g.byID if needed
2021-08-30 16:25:50 +10:00
i, ok := component.(Identifier)
2021-08-30 15:49:51 +10:00
if !ok {
2021-08-30 15:07:57 +10:00
return
}
delete(g.byID, i.Ident())
}