2021-07-30 14:17:40 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-07-30 17:26:23 +10:00
|
|
|
"encoding/gob"
|
2021-07-30 16:31:08 +10:00
|
|
|
"math"
|
2021-07-30 14:17:40 +10:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2021-07-30 17:26:23 +10:00
|
|
|
func init() {
|
|
|
|
gob.Register(Scene{})
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:25:32 +10:00
|
|
|
// Scene manages drawing and updating a bunch of components.
|
|
|
|
type Scene struct {
|
2021-07-30 14:17:40 +10:00
|
|
|
Components []interface{}
|
2021-08-02 14:38:48 +10:00
|
|
|
Disabled bool
|
2021-08-01 17:08:26 +10:00
|
|
|
Hidden bool
|
|
|
|
ID
|
2021-08-01 20:28:33 +10:00
|
|
|
Transform GeoMDef
|
2021-08-01 17:08:26 +10:00
|
|
|
ZPos
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:25:32 +10:00
|
|
|
// Draw draws all components in order.
|
2021-07-30 14:36:11 +10:00
|
|
|
func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
2021-08-01 17:08:26 +10:00
|
|
|
if s.Hidden {
|
|
|
|
return
|
|
|
|
}
|
2021-08-01 20:28:33 +10:00
|
|
|
geom.Concat(*s.Transform.GeoM())
|
|
|
|
|
2021-07-30 14:36:11 +10:00
|
|
|
for _, i := range s.Components {
|
2021-07-30 14:17:40 +10:00
|
|
|
if d, ok := i.(Drawer); ok {
|
|
|
|
d.Draw(screen, geom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortByZ sorts the components by Z position.
|
2021-07-30 14:25:32 +10:00
|
|
|
// Stable sort is used to avoid Z-fighting among layers without a Z, or
|
|
|
|
// among those with equal Z. All non-ZPositioners are sorted first.
|
2021-07-30 14:36:11 +10:00
|
|
|
func (s *Scene) sortByZ() {
|
|
|
|
sort.SliceStable(s.Components, func(i, j int) bool {
|
|
|
|
a, aok := s.Components[i].(ZPositioner)
|
|
|
|
b, bok := s.Components[j].(ZPositioner)
|
2021-07-30 14:17:40 +10:00
|
|
|
if aok && bok {
|
|
|
|
return a.Z() < b.Z()
|
|
|
|
}
|
|
|
|
return !aok && bok
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-02 12:21:24 +10:00
|
|
|
// Scan returns all immediate subcomponents.
|
2021-08-02 12:16:10 +10:00
|
|
|
func (s *Scene) Scan() []interface{} { return s.Components }
|
2021-08-01 16:10:30 +10:00
|
|
|
|
2021-07-30 14:17:40 +10:00
|
|
|
// Update calls Update on all Updater components.
|
2021-07-30 14:36:11 +10:00
|
|
|
func (s *Scene) Update() error {
|
2021-08-02 14:38:48 +10:00
|
|
|
if s.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-30 16:31:08 +10:00
|
|
|
needsSort := false
|
2021-08-01 17:24:38 +10:00
|
|
|
curZ := -math.MaxFloat64 // fun fact: this is min float64
|
2021-07-30 14:36:11 +10:00
|
|
|
for _, c := range s.Components {
|
2021-07-30 16:31:08 +10:00
|
|
|
// Update each updater in turn
|
2021-07-30 14:17:40 +10:00
|
|
|
if u, ok := c.(Updater); ok {
|
|
|
|
if err := u.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 16:31:08 +10:00
|
|
|
if !needsSort {
|
|
|
|
// Check if the update put the components out of order
|
|
|
|
if z, ok := c.(ZPositioner); ok {
|
|
|
|
if t := z.Z(); t < curZ {
|
|
|
|
needsSort = true
|
|
|
|
curZ = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
2021-07-30 16:31:08 +10:00
|
|
|
if needsSort {
|
2021-07-30 14:36:11 +10:00
|
|
|
s.sortByZ()
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|