ichigo/engine/game.go

163 lines
3.7 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-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"
)
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-27 15:39:10 +10:00
db map[string]Identifier // Named components by ID
dex map[dexKey][]interface{} // Ancestor/behaviour index
}
type dexKey struct {
ancestor interface{}
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-01 17:14:57 +10:00
// Component returns the component with a given ID, or nil if there is none.
2021-08-27 15:39:10 +10:00
// This only returns sensible values 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()
return g.db[id]
}
2021-08-01 16:10:30 +10:00
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.
func (g *Game) Query(ancestor interface{}, behaviour reflect.Type) []interface{} {
g.dbmu.RLock()
defer g.dbmu.RUnlock()
return g.dex[dexKey{ancestor, behaviour}]
}
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-27 13:56:50 +10:00
// Walk calls v with every path of components reachable from c via Scan, for as
// long as visit returns nil.
func Walk(c interface{}, v func(interface{}, []interface{}) error) error {
2021-08-27 14:07:21 +10:00
return walk(c, make([]interface{}, 0, 16), v)
2021-08-27 13:56:50 +10:00
}
func walk(c interface{}, p []interface{}, v func(interface{}, []interface{}) error) error {
if err := v(c, p); err != nil {
2021-08-20 15:01:31 +10:00
return err
2021-08-01 16:41:10 +10:00
}
2021-08-15 17:11:26 +10:00
sc, ok := c.(Scanner)
if !ok {
2021-08-20 15:01:31 +10:00
return nil
2021-08-15 17:11:26 +10:00
}
2021-08-27 13:56:50 +10:00
p = append(p, c)
2021-08-15 17:11:26 +10:00
for _, c := range sc.Scan() {
2021-08-27 13:56:50 +10:00
if err := walk(c, p, v); 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-27 14:52:24 +10:00
if err := Walk(g, func(c interface{}, _ []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-27 14:43:37 +10:00
g.db = make(map[string]Identifier)
2021-08-27 15:39:10 +10:00
g.dex = make(map[dexKey][]interface{})
2021-08-27 14:52:24 +10:00
if err := Walk(g, g.registerComponent); 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-27 16:46:04 +10:00
for _, p := range g.Query(g, PrepperType) {
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
func (g *Game) registerComponent(c interface{}, path []interface{}) error {
// register in g.dex
ct := reflect.TypeOf(c)
for _, b := range Behaviours {
if !ct.Implements(b) {
continue
}
// TODO: sub-quadratic?
for _, p := range append(path, c) {
k := dexKey{p, b}
g.dex[k] = append(g.dex[k], c)
}
}
// register in g.db
i, ok := c.(Identifier)
if !ok {
return nil
}
id := i.Ident()
if id == "" {
return nil
}
if _, exists := g.db[id]; exists {
return fmt.Errorf("duplicate id %q", id)
}
g.db[id] = i
return nil
}