ichigo/engine/game.go

97 lines
2.1 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"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
gob.Register(Game{})
}
2021-07-23 13:12:54 +10:00
// Game implements the ebiten methods using a collection of components.
type Game struct {
2021-07-23 13:13:50 +10:00
ScreenWidth int
ScreenHeight int
2021-08-05 12:40:32 +10:00
*Scene
2021-08-01 16:10:30 +10:00
componentsByID map[string]interface{}
2021-07-23 13:12:54 +10:00
}
2021-08-05 12:40:32 +10:00
// Draw draws the entire thing, with no geometric transform.
2021-07-23 13:12:54 +10:00
func (g *Game) Draw(screen *ebiten.Image) {
2021-07-30 14:25:32 +10:00
g.Scene.Draw(screen, ebiten.GeoM{})
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-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.
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.
func (g *Game) Scan() []interface{} { return []interface{}{g.Scene} }
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,
// for as long as visit returns true.
func Walk(c interface{}, v func(interface{}) bool) {
2021-08-01 16:41:10 +10:00
if !v(c) {
return
}
if sc, ok := c.(Scanner); ok {
2021-08-02 12:16:10 +10:00
for _, c := range sc.Scan() {
2021-08-01 16:41:10 +10:00
if !v(c) {
return
}
2021-08-04 12:40:51 +10:00
Walk(c, v)
2021-08-01 16:10:30 +10:00
}
}
}
2021-08-05 12:26:41 +10:00
// PrepareToRun builds the component database (using Walk) and then calls
// Prepare on every Preparer. You must call PrepareToRun before passing to
// ebiten.RunGame.
func (g *Game) PrepareToRun() {
2021-08-01 17:08:26 +10:00
g.componentsByID = make(map[string]interface{})
2021-08-05 12:26:41 +10:00
Walk(g, func(c interface{}) bool {
2021-08-01 17:08:26 +10:00
g.RegisterComponent(c)
2021-08-01 16:41:10 +10:00
return true
})
2021-08-05 12:26:41 +10:00
Walk(g, func(c interface{}) bool {
if p, ok := c.(Prepper); ok {
p.Prepare(g)
}
return true
})
2021-08-01 16:10:30 +10:00
}