ichigo/engine/game.go

119 lines
2.8 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-22 20:27:08 +10:00
"io/fs"
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-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
componentsByID map[string]interface{}
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-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.
func (g *Game) Update() error { return g.Root.Update() }
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-01 17:10:47 +10:00
g.componentsByID[id] = c
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-01 17:10:47 +10:00
delete(g.componentsByID, id)
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-01 16:10:30 +10:00
func (g *Game) Component(id string) interface{} { return g.componentsByID[id] }
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-04 12:40:51 +10:00
// Walk calls v with every component reachable from c via Scan, recursively,
2021-08-20 15:01:31 +10:00
// for as long as visit returns nil.
func Walk(c interface{}, v func(interface{}) error) error {
if err := v(c); err != nil {
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
}
for _, c := range sc.Scan() {
2021-08-20 15:01:31 +10:00
if err := Walk(c, v); err != nil {
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-20 16:31:06 +10:00
// Load calls Load on all Loaders reachable via Scan (using Walk).
// It stops on the first error.
2021-08-23 10:09:49 +10:00
func (g *Game) Load(assets fs.FS) error {
2021-08-20 16:46:26 +10:00
return Walk(g.Root, 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-20 16:31:06 +10:00
})
}
2021-08-20 15:52:01 +10:00
// Prepare builds the component database (using Walk) and then calls
// Prepare on every Preparer. You must call Prepare before any calls
2021-08-20 16:23:10 +10:00
// to Component. You may call Prepare again (e.g. as an alternative to
// fastidiously calling RegisterComponent/UnregisterComponent).
2021-08-20 15:52:01 +10:00
func (g *Game) Prepare() {
2021-08-01 17:08:26 +10:00
g.componentsByID = make(map[string]interface{})
2021-08-20 16:46:26 +10:00
Walk(g.Root, func(c 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-20 16:46:26 +10:00
Walk(g.Root, func(c 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-01 16:10:30 +10:00
}