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
|
|
|
|
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
|
2021-09-21 12:36:49 +10:00
|
|
|
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
|
|
|
|
}
|
2021-09-21 12:36:49 +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) {
|
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-13 16:50:35 +10:00
|
|
|
if g.Disabled() {
|
2021-08-25 16:46:30 +10:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-31 17:03:43 +10:00
|
|
|
|
2021-09-01 09:17:08 +10:00
|
|
|
// Need to do a similar trick for Draw: disabling a parent object should
|
|
|
|
// disable the child objects.
|
2021-09-14 19:25:29 +10:00
|
|
|
// cache memoises the disabled state for each component.
|
|
|
|
cache := map[interface{}]bool{
|
2021-09-01 09:17:08 +10:00
|
|
|
g: false,
|
2021-08-31 17:03:43 +10:00
|
|
|
}
|
2021-09-01 09:17:08 +10:00
|
|
|
|
|
|
|
// Update everything that is not disabled.
|
2021-09-20 16:02:01 +10:00
|
|
|
return PostorderWalk(g, func(c, _ interface{}) error {
|
2021-09-01 09:17:08 +10:00
|
|
|
// Skip g (note g satisfies Updater, so this would infinitely recurse)
|
2021-09-20 16:02:01 +10:00
|
|
|
if c == g {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
u, ok := c.(Updater)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2021-09-01 09:17:08 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is u disabled itself?
|
2021-09-13 16:50:35 +10:00
|
|
|
if d, ok := u.(Disabler); ok && d.Disabled() {
|
2021-09-14 19:25:29 +10:00
|
|
|
cache[u] = true
|
2021-09-20 16:02:01 +10:00
|
|
|
return nil
|
2021-09-01 09:17:08 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk up g.par to find the nearest state in accum.
|
|
|
|
var st bool
|
|
|
|
stack := []interface{}{u}
|
|
|
|
for p := g.par[u]; ; p = g.par[p] {
|
2021-09-14 19:25:29 +10:00
|
|
|
if s, found := cache[p]; found {
|
2021-09-01 09:17:08 +10:00
|
|
|
st = s
|
|
|
|
break
|
2021-08-31 18:19:14 +10:00
|
|
|
}
|
2021-09-01 09:17:08 +10:00
|
|
|
stack = append(stack, p)
|
|
|
|
}
|
|
|
|
// Unwind the stack, accumulating state along the way.
|
|
|
|
for len(stack) > 0 {
|
|
|
|
l1 := len(stack) - 1
|
|
|
|
p := stack[l1]
|
|
|
|
stack = stack[:l1]
|
|
|
|
if d, ok := p.(Disabler); ok {
|
2021-09-13 16:50:35 +10:00
|
|
|
st = st || d.Disabled()
|
2021-09-01 09:17:08 +10:00
|
|
|
}
|
2021-09-14 19:25:29 +10:00
|
|
|
cache[p] = st
|
2021-09-01 09:17:08 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip updating if disabled.
|
|
|
|
if st {
|
2021-09-20 16:02:01 +10:00
|
|
|
return nil
|
2021-09-01 09:17:08 +10:00
|
|
|
}
|
|
|
|
|
2021-09-20 16:02:01 +10:00
|
|
|
// Update
|
|
|
|
return u.Update()
|
|
|
|
})
|
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-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-21 12:36:49 +10:00
|
|
|
// Scan returns g.Root.
|
|
|
|
func (g *Game) Scan() []interface{} { return []interface{}{g.Root} }
|
2021-08-01 16:41:10 +10:00
|
|
|
|
2021-09-02 12:15:23 +10:00
|
|
|
// PreorderWalk calls visit with every component and its parent, reachable from
|
|
|
|
// the given component via Scan, for as long as visit returns nil. The parent
|
|
|
|
// value passed to visit when visiting component will be nil. The parent will be
|
|
|
|
// visited before the children.
|
|
|
|
func PreorderWalk(component interface{}, visit func(component, parent interface{}) error) error {
|
|
|
|
return preorderWalk(component, nil, visit)
|
2021-08-27 13:56:50 +10:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:15:23 +10:00
|
|
|
func preorderWalk(component, parent interface{}, visit func(component, parent interface{}) error) error {
|
2021-08-30 16:25:50 +10:00
|
|
|
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-09-02 12:15:23 +10:00
|
|
|
if err := preorderWalk(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-09-02 12:15:23 +10:00
|
|
|
// PostorderWalk calls visit with every component and its parent, reachable from
|
|
|
|
// the given component via Scan, for as long as visit returns nil. The parent
|
|
|
|
// value passed to visit when visiting component will be nil. The children will
|
|
|
|
// be visited before the parent.
|
|
|
|
func PostorderWalk(component interface{}, visit func(component, parent interface{}) error) error {
|
2021-09-20 15:52:44 +10:00
|
|
|
return postorderWalk(component, nil, visit)
|
2021-09-02 12:15:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func postorderWalk(component, parent interface{}, visit func(component, parent interface{}) error) error {
|
|
|
|
if sc, ok := component.(Scanner); ok {
|
|
|
|
for _, c := range sc.Scan() {
|
|
|
|
if err := postorderWalk(c, component, visit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return visit(component, parent)
|
|
|
|
}
|
|
|
|
|
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-02 12:15:23 +10:00
|
|
|
if err := PreorderWalk(g, func(c, _ interface{}) error {
|
2021-09-20 15:52:44 +10:00
|
|
|
if l, ok := c.(Loader); ok {
|
|
|
|
return l.Load(assets)
|
2021-08-20 16:31:06 +10:00
|
|
|
}
|
2021-09-20 15:52:44 +10:00
|
|
|
return nil
|
2021-08-27 13:56:50 +10:00
|
|
|
}); err != nil {
|
|
|
|
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-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-09-02 12:15:23 +10:00
|
|
|
if err := PreorderWalk(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-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-20 15:52:44 +10:00
|
|
|
if err := PostorderWalk(g, func(c, _ interface{}) error {
|
|
|
|
if p, ok := c.(Prepper); ok {
|
|
|
|
return p.Prepare(g)
|
2021-08-05 12:26:41 +10:00
|
|
|
}
|
2021-09-20 15:52:44 +10:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2021-08-27 16:46:04 +10:00
|
|
|
}
|
2021-09-20 15:52:44 +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-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-09-02 12:15:23 +10:00
|
|
|
// 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-20 15:52:44 +10:00
|
|
|
// preorderWalk goes in the right order for registering.
|
2021-09-02 12:15:23 +10:00
|
|
|
return preorderWalk(component, parent, g.register)
|
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-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-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-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.
|
2021-09-02 12:15:23 +10:00
|
|
|
// 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-20 15:52:44 +10:00
|
|
|
// postorderWalk goes in the right order for unregistering.
|
|
|
|
postorderWalk(component, nil, g.unregister)
|
2021-08-30 15:49:51 +10:00
|
|
|
g.dbmu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:52:44 +10:00
|
|
|
func (g *Game) unregister(component, _ interface{}) error {
|
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-20 15:52:44 +10:00
|
|
|
return nil
|
2021-08-30 15:07:57 +10:00
|
|
|
}
|
2021-09-01 09:17:08 +10:00
|
|
|
|
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
|
|
|
|
}
|