ichigo/engine/game.go

463 lines
12 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-09-17 12:50:29 +10:00
"sort"
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-09-16 14:56:27 +10:00
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
2021-08-01 16:10:30 +10:00
)
2021-09-17 12:50:29 +10:00
const topologicalDrawSort = true
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
Root interface{} // typically a *Scene or SceneRef though
2021-09-08 20:08:57 +10:00
Projection geom.IntProjection
VoxelScale geom.Float3
2021-08-01 16:10:30 +10:00
2021-08-31 17:03:43 +10:00
dbmu sync.RWMutex
byID map[string]Identifier // Named components by ID
byAB map[abKey]map[interface{}]struct{} // Ancestor/behaviour index
2021-09-01 09:17:08 +10:00
drawList drawList // draw list :|
2021-08-31 17:03:43 +10:00
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-08-31 22:05:21 +10:00
2021-09-01 09:17:08 +10:00
// Hiding a parent component should hide the child objects, and the
// transform applied to a child should be the cumulative transform of all
// parents as well.
2021-09-14 19:25:29 +10:00
// cache memoises the results for each component.
2021-09-01 09:17:08 +10:00
type state struct {
2021-09-07 14:00:50 +10:00
hidden bool
opts ebiten.DrawImageOptions
2021-09-01 09:17:08 +10:00
}
2021-09-14 19:25:29 +10:00
cache := map[interface{}]state{
2021-09-01 09:17:08 +10:00
g: {hidden: false},
}
// Draw everything in g.drawList, where not hidden (itself or any parent)
2021-09-13 16:32:44 +10:00
for _, d := range g.drawList.list {
2021-09-01 09:17:08 +10:00
// Is d hidden itself?
2021-09-13 16:50:35 +10:00
if h, ok := d.(Hider); ok && h.Hidden() {
2021-09-14 19:25:29 +10:00
cache[d] = state{hidden: true}
2021-09-01 09:17:08 +10:00
continue // skip drawing
2021-08-31 18:19:14 +10:00
}
2021-09-01 09:17:08 +10:00
// Walk up g.par to find the nearest state in accum.
var st state
stack := []interface{}{d}
for p := g.par[d]; ; 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 h, ok := p.(Hider); ok {
2021-09-13 16:50:35 +10:00
st.hidden = st.hidden || h.Hidden()
2021-08-31 18:19:14 +10:00
}
2021-08-31 22:05:21 +10:00
if st.hidden {
2021-09-14 19:25:29 +10:00
cache[p] = state{hidden: true}
2021-08-31 18:19:14 +10:00
continue
}
2021-09-07 14:00:50 +10:00
// p is not hidden, so compute its cumulative opts.
2021-09-03 10:08:56 +10:00
if tf, ok := p.(Transformer); ok {
2021-09-11 17:39:52 +10:00
st.opts = concatOpts(tf.Transform(), st.opts)
2021-09-01 09:17:08 +10:00
}
2021-09-14 19:25:29 +10:00
cache[p] = st
2021-08-31 17:03:43 +10:00
}
2021-09-01 09:17:08 +10:00
// Skip drawing if hidden.
if st.hidden {
continue
}
2021-09-07 14:00:50 +10:00
d.Draw(screen, &st.opts)
2021-08-31 17:03:43 +10:00
}
2021-09-16 14:56:27 +10:00
if true {
// Infodump about draw list
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("len(drawList.list) = %d", len(g.drawList.list)), 0, 30)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("len(drawList.rev) = %d", len(g.drawList.list)), 0, 45)
}
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-01 21:50:58 +10:00
// TODO: do it in a fixed order? map essentially randomises iteration order
2021-09-01 09:17:08 +10:00
for u := range g.Query(g.Ident(), UpdaterType) {
// Skip g (note g satisfies Updater, so this would infinitely recurse)
if u == g {
continue
}
// 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-01 09:17:08 +10:00
continue
}
// 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 {
continue
}
if err := u.(Updater).Update(); err != nil {
return err
}
}
// Sort the draw list (on every frame - this isn't as bad as it sounds)
2021-09-17 12:50:29 +10:00
if topologicalDrawSort {
g.drawList.topsort()
} else {
sort.Stable(g.drawList)
// Truncate tombstones from the end.
for i := g.drawList.Len() - 1; i >= 0; i-- {
if g.drawList.list[i] != (tombstone{}) {
break
}
g.drawList.list = g.drawList.list[:i]
2021-08-31 17:03:43 +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-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
// 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
}
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() {
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
}
// 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 {
return preorderWalk(component, nil, visit)
}
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()
if err := PreorderWalk(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-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-09-13 17:07:43 +10:00
g.drawList.list = nil
2021-09-13 16:32:44 +10:00
g.drawList.rev = make(map[Drawer]int)
2021-08-30 15:49:51 +10:00
g.par = make(map[interface{}]interface{})
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-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
}
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.
// 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()
// walk goes in the right order for registering.
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-31 17:03:43 +10:00
// register in g.drawList
if d, ok := component.(Drawer); ok {
2021-09-13 16:32:44 +10:00
if _, exists := g.drawList.rev[d]; exists {
// already registered
return fmt.Errorf("double registration of %v", d)
}
g.drawList.rev[d] = len(g.drawList.list)
g.drawList.list = append(g.drawList.list, d)
2021-08-31 17:03:43 +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-16 10:34:54 +10:00
id, ok := p.(Identifier)
if !ok || id.Ident() == "" {
2021-08-28 18:14:37 +10:00
continue
}
2021-09-16 10:34:54 +10:00
k := abKey{id.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
}
}
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()
postorderWalk(component, nil, func(c, _ interface{}) error {
g.unregister(c)
return nil
})
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-09-16 10:34:54 +10:00
id, ok := p.(Identifier)
if !ok || id.Ident() == "" {
2021-08-30 15:07:57 +10:00
continue
}
2021-09-16 10:34:54 +10:00
k := abKey{id.Ident(), b}
2021-08-30 15:07:57 +10:00
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
2021-08-31 17:03:43 +10:00
// unregister from g.drawList
2021-09-13 16:32:44 +10:00
if d, ok := component.(Drawer); ok {
if i, found := g.drawList.rev[d]; found {
g.drawList.list[i] = tombstone{}
delete(g.drawList.rev, d)
2021-08-31 17:03:43 +10:00
}
}
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-02 13:16:57 +10:00
// --------- Helper stuff ---------
2021-09-01 09:17:08 +10:00
type abKey struct {
ancestor string
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
}