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-08-01 17:08:26 +10:00
|
|
|
// Identifier components have a sense of self. This makes it easier for
|
|
|
|
// components to find and interact with one another.
|
|
|
|
type Identifier interface {
|
|
|
|
Ident() string
|
2021-08-01 16:10:30 +10:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:16:10 +10:00
|
|
|
// Builder components can be built. It is called when the game
|
2021-08-01 16:10:30 +10:00
|
|
|
// component database is being constructed. It should store the Game reference
|
2021-08-02 12:16:10 +10:00
|
|
|
// (if needed later on).
|
|
|
|
type Builder interface {
|
|
|
|
Build(game *Game)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scanner components can be scanned. It is called when the game tree is walked
|
|
|
|
// (such as when the game component database is constructed).
|
2021-08-02 12:21:24 +10:00
|
|
|
// Scan should return a slice containing all immediate subcomponents.
|
2021-08-01 16:10:30 +10:00
|
|
|
type Scanner interface {
|
2021-08-02 12:16:10 +10:00
|
|
|
Scan() []interface{}
|
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.
|
|
|
|
type Game struct {
|
2021-07-23 13:13:50 +10:00
|
|
|
ScreenWidth int
|
|
|
|
ScreenHeight int
|
2021-07-30 14:25:32 +10:00
|
|
|
Scene *Scene
|
2021-08-01 16:10:30 +10:00
|
|
|
|
|
|
|
componentsByID map[string]interface{}
|
2021-07-23 13:12:54 +10:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:17:40 +10:00
|
|
|
// Draw draws the entire thing.
|
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-07-30 14:17:40 +10:00
|
|
|
// Update just passes the call onto Layers.
|
|
|
|
func (g *Game) Update() error {
|
2021-07-30 14:25:32 +10:00
|
|
|
return g.Scene.Update()
|
2021-07-23 13:46:19 +10:00
|
|
|
}
|
2021-08-01 16:10:30 +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-02 12:16:10 +10:00
|
|
|
// Walk calls v with every component reachable via Scan, for as long as visit
|
|
|
|
// returns true.
|
2021-08-01 16:41:10 +10:00
|
|
|
func (g *Game) Walk(v func(interface{}) bool) {
|
|
|
|
g.walk(g.Scene, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) walk(c interface{}, v func(interface{}) bool) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
g.walk(c, v)
|
2021-08-01 16:10:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build builds the component database.
|
|
|
|
func (g *Game) Build() {
|
2021-08-01 17:08:26 +10:00
|
|
|
g.componentsByID = make(map[string]interface{})
|
|
|
|
g.Walk(func(c interface{}) bool {
|
2021-08-02 12:16:10 +10:00
|
|
|
if b, ok := c.(Builder); ok {
|
|
|
|
b.Build(g)
|
|
|
|
}
|
2021-08-01 17:08:26 +10:00
|
|
|
g.RegisterComponent(c)
|
2021-08-01 16:41:10 +10:00
|
|
|
return true
|
|
|
|
})
|
2021-08-01 16:10:30 +10:00
|
|
|
}
|