ichigo/engine/game.go

371 lines
9.3 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-09-01 12:07:49 +10:00
"image"
2021-08-22 20:27:08 +10:00
"io/fs"
2021-09-02 11:59:42 +10:00
"log"
2021-08-27 15:39:10 +10:00
"reflect"
2021-08-25 16:46:30 +10:00
"sync"
2021-09-02 11:59:42 +10:00
"time"
2021-08-01 16:10:30 +10:00
2021-09-08 20:08:57 +10:00
"drjosh.dev/gurgle/geom"
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
2021-09-21 14:42:18 +10:00
Registrar
2021-08-28 18:14:37 +10:00
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-09-13 16:50:35 +10:00
Disables
Hides
2021-09-01 12:07:49 +10:00
ScreenSize image.Point
Root Drawer // usually a DrawManager
2021-09-17 16:47:18 +10:00
Projection geom.Projector
2021-09-08 20:08:57 +10:00
VoxelScale geom.Float3
2021-08-01 16:10:30 +10:00
2021-09-20 12:18:03 +10:00
dbmu sync.RWMutex
byID map[string]Identifier // Named components by ID
byAB map[abKey]map[interface{}]struct{} // Ancestor/behaviour index
par map[interface{}]interface{} // par[x] is parent of x
2021-08-27 15:39:10 +10:00
}
2021-09-01 09:17:08 +10:00
// Draw draws everything.
2021-07-23 13:12:54 +10:00
func (g *Game) Draw(screen *ebiten.Image) {
2021-09-13 16:50:35 +10:00
if g.Hidden() {
2021-08-25 16:46:30 +10:00
return
}
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) {
2021-09-01 12:07:49 +10:00
return g.ScreenSize.X, g.ScreenSize.Y
2021-07-23 13:12:54 +10:00
}
2021-07-23 13:46:19 +10:00
2021-09-01 09:17:08 +10:00
// Update updates everything.
2021-08-25 16:46:30 +10:00
func (g *Game) Update() error {
2021-09-21 16:53:04 +10:00
return g.updateRecursive(g)
}
2021-08-31 17:03:43 +10:00
2021-09-21 16:53:04 +10:00
// updateRecursive updates everything in a post-order traversal. It terminates recursion
// early if the component reports it is Disabled.
func (g *Game) updateRecursive(c interface{}) error {
if d, ok := c.(Disabler); ok && d.Disabled() {
return nil
2021-08-31 17:03:43 +10:00
}
2021-09-21 16:53:04 +10:00
if sc, ok := c.(Scanner); ok {
2021-09-22 15:48:02 +10:00
if err := sc.Scan(g.updateRecursive); err != nil {
return err
}
2021-09-21 16:53:04 +10:00
}
2021-09-21 17:13:32 +10:00
if c == g { // prevent infinite recursion
2021-09-21 16:53:04 +10:00
return nil
}
if u, ok := c.(Updater); ok {
2021-09-20 16:02:01 +10:00
return u.Update()
2021-09-21 16:53:04 +10:00
}
return nil
2021-08-25 16:46:30 +10:00
}
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-09-21 17:09:49 +10:00
// PathRegister calls Register on every Registrar in the path between g and
// parent (top-to-bottom, i.e. g first)
func (g *Game) PathRegister(component, parent interface{}) error {
2021-09-21 17:23:59 +10:00
rp := g.ReversePath(parent)
for i := len(rp) - 1; i >= 0; i-- {
if r, ok := rp[i].(Registrar); ok {
2021-09-21 17:09:49 +10:00
if err := r.Register(component, parent); err != nil {
return err
}
}
2021-09-21 15:17:59 +10:00
}
2021-09-21 17:09:49 +10:00
return nil
2021-09-21 15:17:59 +10:00
}
2021-09-21 17:09:49 +10:00
// PathUnregister calls Unregister on every Registrar in the path between g and
2021-09-21 17:23:59 +10:00
// parent (bottom-to-top, i.e. parent first).
2021-09-21 17:09:49 +10:00
func (g *Game) PathUnregister(component interface{}) {
for _, p := range g.ReversePath(component) {
if r, ok := p.(Registrar); ok {
r.Unregister(component)
}
}
}
2021-09-21 17:13:32 +10:00
// Path returns a slice with the path of components to reach component from g
// (including g and component).
2021-09-21 16:53:04 +10:00
func (g *Game) Path(component interface{}) []interface{} {
2021-09-21 17:09:49 +10:00
stack := g.ReversePath(component)
for i, j := 0, len(stack)-1; i < j; i, j = i+1, j-1 {
stack[i], stack[j] = stack[j], stack[i]
2021-09-21 15:17:59 +10:00
}
2021-09-21 16:53:04 +10:00
return stack
}
2021-09-21 17:09:49 +10:00
// ReversePath returns the same slice as Path, but reversed. (ReversePath is
// faster than Path).
2021-09-21 16:53:04 +10:00
func (g *Game) ReversePath(component interface{}) []interface{} {
var stack []interface{}
g.dbmu.RLock()
for p := component; p != nil; p = g.Parent(p) {
stack = append(stack, p)
2021-09-21 15:17:59 +10:00
}
2021-09-21 16:53:04 +10:00
g.dbmu.RUnlock()
return stack
2021-09-21 15:17:59 +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.
2021-09-20 15:52:44 +10:00
func (g *Game) Query(ancestor interface{}, behaviour reflect.Type) map[interface{}]struct{} {
2021-08-27 15:39:10 +10:00
g.dbmu.RLock()
defer g.dbmu.RUnlock()
2021-09-20 15:52:44 +10:00
return g.byAB[abKey{ancestor, behaviour}]
2021-08-27 15:39:10 +10:00
}
2021-09-22 15:55:38 +10:00
// Scan visits g.Root.
2021-09-22 15:48:02 +10:00
func (g *Game) Scan(visit func(interface{}) error) error {
return visit(g.Root)
}
2021-08-01 16:41:10 +10:00
2021-09-22 17:04:27 +10:00
// Load loads a component and all subcomponents recursively.
// Note that this method does not implement Loader.
func (g *Game) Load(component interface{}, assets fs.FS) error {
if l, ok := component.(Loader); ok {
if err := l.Load(assets); err != nil {
return err
}
}
if sc, ok := component.(Scanner); ok {
return sc.Scan(func(x interface{}) error {
return g.Load(x, assets)
})
}
return nil
}
// Prepare prepares a component and all subcomponents recursively.
// Note that this method does not implement Prepper.
func (g *Game) Prepare(component interface{}) error {
// Postorder traversal, in case ancestors depend on descendants being
// ready to answer queries.
if sc, ok := component.(Scanner); ok {
if err := sc.Scan(g.Prepare); err != nil {
return err
}
}
if p, ok := component.(Prepper); ok {
return p.Prepare(g)
}
return nil
}
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-09-08 20:08:57 +10:00
if g.VoxelScale == (geom.Float3{}) {
2021-09-08 20:09:52 +10:00
g.VoxelScale = geom.Float3{X: 1, Y: 1, Z: 1}
2021-09-07 14:00:50 +10:00
}
2021-08-27 16:46:04 +10:00
// Load all the Loaders.
2021-09-02 11:59:42 +10:00
startLoad := time.Now()
2021-09-22 17:04:27 +10:00
if err := g.Load(g.Root, assets); err != nil {
2021-08-27 13:56:50 +10:00
return err
}
2021-09-02 11:59:42 +10:00
log.Printf("finished loading in %v", time.Since(startLoad))
2021-08-27 13:56:50 +10:00
2021-08-27 14:43:37 +10:00
// Build the component databases
2021-09-02 11:59:42 +10:00
startBuild := time.Now()
2021-09-23 14:17:18 +10:00
if err := g.build(); err != nil {
2021-08-27 14:43:37 +10:00
return err
}
2021-09-02 11:59:42 +10:00
log.Printf("finished building db in %v", time.Since(startBuild))
2021-08-27 13:56:50 +10:00
2021-08-27 14:43:37 +10:00
// Prepare all the Preppers
2021-09-02 11:59:42 +10:00
startPrep := time.Now()
2021-09-22 17:04:27 +10:00
if err := g.Prepare(g.Root); err != nil {
2021-09-20 15:52:44 +10:00
return err
2021-08-27 16:46:04 +10:00
}
2021-09-02 11:59:42 +10:00
log.Printf("finished preparing in %v", time.Since(startPrep))
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-09-23 14:17:18 +10:00
func (g *Game) build() error {
g.dbmu.Lock()
defer g.dbmu.Unlock()
g.byID = make(map[string]Identifier)
g.byAB = make(map[abKey]map[interface{}]struct{})
g.par = make(map[interface{}]interface{})
return g.registerRecursive(g, nil)
}
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.
// Registering a component will recursively register all children found via
// Scan.
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-09-23 14:17:18 +10:00
return g.registerRecursive(component, parent)
}
func (g *Game) registerRecursive(component, parent interface{}) error {
if err := g.registerOne(component, parent); err != nil {
return err
}
if sc, ok := component.(Scanner); ok {
return sc.Scan(func(x interface{}) error {
return g.registerRecursive(x, component)
})
}
return nil
2021-08-30 16:25:50 +10:00
}
2021-09-21 14:42:18 +10:00
func (g *Game) registerOne(component, parent interface{}) error {
2021-09-13 16:32:44 +10:00
// register in g.byID if needed
2021-09-02 14:09:53 +10:00
if i, ok := component.(Identifier); ok {
2021-09-16 10:34:54 +10:00
if id := i.Ident(); id != "" {
if _, exists := g.byID[id]; exists {
return fmt.Errorf("duplicate id %q", id)
}
g.byID[id] = i
2021-09-02 14:09:53 +10:00
}
}
2021-08-30 15:49:51 +10:00
// register in g.par
2021-09-22 17:05:50 +10:00
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-09-20 15:52:44 +10:00
k := abKey{p, 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
}
}
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.
// Passing a nil component has no effect. Unregistering a component will
// recursively unregister child components found via Scan.
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-09-23 14:17:18 +10:00
g.unregisterRecursive(component)
2021-08-30 15:49:51 +10:00
g.dbmu.Unlock()
}
2021-09-23 14:17:18 +10:00
func (g *Game) unregisterRecursive(component interface{}) {
if sc, ok := component.(Scanner); ok {
sc.Scan(func(x interface{}) error {
g.unregisterRecursive(x)
return nil
})
}
g.unregisterOne(component)
}
func (g *Game) unregisterOne(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-09-20 15:52:44 +10:00
if k := (abKey{p, b}); g.byAB[k] != nil {
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-09-16 10:34:54 +10:00
if id, ok := component.(Identifier); ok && id.Ident() != "" {
delete(g.byID, id.Ident())
2021-08-30 15:07:57 +10:00
}
}
2021-09-01 09:17:08 +10:00
2021-09-23 14:17:18 +10:00
func (g *Game) String() string { return "Game" }
2021-09-02 13:16:57 +10:00
// --------- Helper stuff ---------
2021-09-01 09:17:08 +10:00
type abKey struct {
2021-09-20 15:52:44 +10:00
ancestor interface{}
2021-09-01 09:17:08 +10:00
behaviour reflect.Type
}
2021-09-11 17:39:52 +10:00
// concatOpts returns the combined options (as though a was applied and then b).
func concatOpts(a, b ebiten.DrawImageOptions) ebiten.DrawImageOptions {
2021-09-07 14:00:50 +10:00
a.ColorM.Concat(b.ColorM)
a.GeoM.Concat(b.GeoM)
if b.CompositeMode != 0 {
a.CompositeMode = b.CompositeMode
}
if b.Filter != 0 {
a.Filter = b.Filter
}
return a
}