2021-07-23 13:12:54 +10:00
|
|
|
package engine
|
|
|
|
|
2021-08-01 16:10:30 +10:00
|
|
|
import (
|
|
|
|
"encoding/gob"
|
2021-08-22 20:27:08 +10:00
|
|
|
"io/fs"
|
2021-08-25 17:22:22 +10:00
|
|
|
"log"
|
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-07-23 13:12:54 +10:00
|
|
|
// Game implements the ebiten methods using a collection of components.
|
2021-08-20 16:46:26 +10:00
|
|
|
// 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-27 13:56:50 +10:00
|
|
|
// Components by ID
|
2021-08-25 16:46:30 +10:00
|
|
|
dbmu sync.RWMutex
|
|
|
|
db map[string]interface{}
|
2021-08-27 13:56:50 +10:00
|
|
|
|
|
|
|
// Collision domains - all Collider subcomponents
|
|
|
|
cdmu sync.RWMutex
|
|
|
|
cd map[string]map[Collider]struct{}
|
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
|
|
|
// RegisterComponent tells the game there is a new component. Currently this is
|
|
|
|
// only necessary for components with IDs.
|
2021-08-01 17:08:26 +10:00
|
|
|
func (g *Game) RegisterComponent(c interface{}) {
|
2021-08-01 17:10:47 +10:00
|
|
|
i, ok := c.(Identifier)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := i.Ident()
|
|
|
|
if id == "" {
|
|
|
|
return
|
2021-08-01 17:08:26 +10:00
|
|
|
}
|
2021-08-25 16:46:30 +10:00
|
|
|
g.dbmu.Lock()
|
2021-08-25 17:22:22 +10:00
|
|
|
if _, exists := g.db[id]; exists {
|
|
|
|
log.Printf("duplicate id %q", id)
|
|
|
|
}
|
2021-08-25 16:46:30 +10:00
|
|
|
g.db[id] = c
|
|
|
|
g.dbmu.Unlock()
|
2021-08-01 17:08:26 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnregisterComponent tells the game the component is no more.
|
2021-08-15 15:52:40 +10:00
|
|
|
// Note this does not remove any references held by other components.
|
2021-08-01 17:08:26 +10:00
|
|
|
func (g *Game) UnregisterComponent(c interface{}) {
|
2021-08-01 17:10:47 +10:00
|
|
|
i, ok := c.(Identifier)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := i.Ident()
|
|
|
|
if id == "" {
|
|
|
|
return
|
2021-08-01 17:08:26 +10:00
|
|
|
}
|
2021-08-25 16:46:30 +10:00
|
|
|
g.dbmu.Lock()
|
|
|
|
delete(g.db, id)
|
|
|
|
g.dbmu.Unlock()
|
2021-08-01 17:08: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-25 16:46:30 +10:00
|
|
|
func (g *Game) Component(id string) interface{} {
|
|
|
|
g.dbmu.RLock()
|
|
|
|
defer g.dbmu.RUnlock()
|
|
|
|
return g.db[id]
|
|
|
|
}
|
2021-08-01 16:10:30 +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-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 13:56:50 +10:00
|
|
|
// LoadAndPrepare first calls Load on all Loaders. Once loading is complete,
|
|
|
|
// it builds the component database and then calls Prepare on every Preparer.
|
|
|
|
// You must call Prepare before any calls
|
|
|
|
// to Component. You may call Prepare again (e.g. as an alternative to
|
|
|
|
// fastidiously calling RegisterComponent/UnregisterComponent).
|
|
|
|
func (g *Game) LoadAndPrepare(assets fs.FS) error {
|
|
|
|
if err := Walk(g.Root, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
g.cdmu.Lock()
|
|
|
|
g.cd = make(map[string]map[Collider]struct{})
|
|
|
|
g.cdmu.Unlock()
|
2021-08-20 16:31:06 +10:00
|
|
|
|
2021-08-25 16:46:30 +10:00
|
|
|
g.dbmu.Lock()
|
|
|
|
g.db = make(map[string]interface{})
|
|
|
|
g.dbmu.Unlock()
|
2021-08-27 13:56:50 +10:00
|
|
|
|
|
|
|
// -> here <- is the moment in time where db is empty.
|
|
|
|
Walk(g.Root, func(c interface{}, p []interface{}) error {
|
2021-08-01 17:08:26 +10:00
|
|
|
g.RegisterComponent(c)
|
2021-08-20 15:01:31 +10:00
|
|
|
return nil
|
2021-08-01 16:41:10 +10:00
|
|
|
})
|
2021-08-27 13:56:50 +10:00
|
|
|
Walk(g.Root, func(c interface{}, _ []interface{}) error {
|
2021-08-05 12:26:41 +10:00
|
|
|
if p, ok := c.(Prepper); ok {
|
|
|
|
p.Prepare(g)
|
|
|
|
}
|
2021-08-20 15:01:31 +10:00
|
|
|
return nil
|
2021-08-05 12:26:41 +10:00
|
|
|
})
|
2021-08-27 13:56:50 +10:00
|
|
|
return nil
|
2021-08-01 16:10:30 +10:00
|
|
|
}
|