2021-09-20 12:18:03 +10:00
|
|
|
package engine
|
|
|
|
|
2021-09-23 16:47:43 +10:00
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
2021-09-20 12:18:03 +10:00
|
|
|
|
2021-09-21 12:36:49 +10:00
|
|
|
var _ interface {
|
|
|
|
Drawer
|
|
|
|
DrawManager
|
|
|
|
Scanner
|
|
|
|
} = &DrawDFS{}
|
|
|
|
|
2021-09-23 16:47:43 +10:00
|
|
|
func init() {
|
|
|
|
gob.Register(&DrawDFS{})
|
|
|
|
}
|
|
|
|
|
2021-09-24 12:29:55 +10:00
|
|
|
// DrawDFS is a DrawManager that does not add any structure. Components are
|
2021-09-20 12:18:03 +10:00
|
|
|
// drawn in the order in which they are encountered by a depth-first search
|
|
|
|
// through the game tree, without any extra sorting based on Z values or
|
|
|
|
// consideration for DrawOrderer).
|
|
|
|
type DrawDFS struct {
|
2021-09-22 14:37:00 +10:00
|
|
|
Child interface{}
|
2021-09-20 12:18:03 +10:00
|
|
|
Hides
|
|
|
|
}
|
|
|
|
|
2021-09-21 12:36:49 +10:00
|
|
|
func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
|
2021-09-20 12:18:03 +10:00
|
|
|
if d.Hidden() {
|
|
|
|
return
|
|
|
|
}
|
2021-09-27 16:57:29 +10:00
|
|
|
d.drawRecursive(d.Child, screen, *opts)
|
2021-09-20 12:18:03 +10:00
|
|
|
}
|
|
|
|
|
2021-09-27 16:57:29 +10:00
|
|
|
// ManagesDrawingSubcomponents is present so DrawDFS is recognised as a
|
|
|
|
// DrawManager.
|
2021-09-21 12:36:49 +10:00
|
|
|
func (DrawDFS) ManagesDrawingSubcomponents() {}
|
|
|
|
|
2021-09-27 16:57:29 +10:00
|
|
|
func (d *DrawDFS) drawRecursive(component interface{}, screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
2021-09-20 12:18:03 +10:00
|
|
|
// Hidden? stop drawing
|
|
|
|
if h, ok := component.(Hider); ok && h.Hidden() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Has a transform? apply to opts
|
|
|
|
if tf, ok := component.(Transformer); ok {
|
|
|
|
opts = concatOpts(tf.Transform(), opts)
|
|
|
|
}
|
|
|
|
// Does it draw itself? Draw
|
|
|
|
if dr, ok := component.(Drawer); ok {
|
|
|
|
dr.Draw(screen, &opts)
|
|
|
|
}
|
2021-09-27 16:57:29 +10:00
|
|
|
// Is it a DrawManager? It manages drawing all its subcomponents.
|
2021-09-21 12:36:49 +10:00
|
|
|
if _, ok := component.(DrawManager); ok {
|
|
|
|
return
|
|
|
|
}
|
2021-09-20 12:18:03 +10:00
|
|
|
// Has subcomponents? recurse
|
|
|
|
if sc, ok := component.(Scanner); ok {
|
2021-09-24 15:38:16 +10:00
|
|
|
sc.Scan(func(x interface{}) error {
|
2021-09-27 16:57:29 +10:00
|
|
|
d.drawRecursive(x, screen, opts)
|
2021-09-22 15:48:02 +10:00
|
|
|
return nil
|
2021-09-24 15:38:16 +10:00
|
|
|
})
|
2021-09-20 12:18:03 +10:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 14:37:00 +10:00
|
|
|
|
2021-09-22 15:55:38 +10:00
|
|
|
// Scan visits d.Child.
|
2021-09-22 15:48:02 +10:00
|
|
|
func (d *DrawDFS) Scan(visit func(interface{}) error) error {
|
|
|
|
return visit(d.Child)
|
|
|
|
}
|
2021-09-23 14:17:18 +10:00
|
|
|
|
|
|
|
func (d *DrawDFS) String() string { return "DrawDFS" }
|