ichigo/engine/scene.go

86 lines
1.8 KiB
Go
Raw Normal View History

package engine
import (
2021-07-30 17:26:23 +10:00
"encoding/gob"
"math"
"sort"
"github.com/hajimehoshi/ebiten/v2"
)
2021-07-30 17:26:23 +10:00
func init() {
gob.Register(Scene{})
}
// Drawer is a component that can draw itself. Draw is called often.
type Drawer interface {
Draw(screen *ebiten.Image, geom ebiten.GeoM)
}
// Updater is a component that can update. Update is called repeatedly.
type Updater interface {
Update() error
}
// ZPositioner is used to reorder layers.
type ZPositioner interface {
Z() float64
}
2021-07-30 14:25:32 +10:00
// Scene manages drawing and updating a bunch of components.
type Scene struct {
Components []interface{}
2021-07-30 14:36:11 +10:00
Transform ebiten.GeoM
}
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) {
geom.Concat(s.Transform)
for _, i := range s.Components {
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)
if aok && bok {
return a.Z() < b.Z()
}
return !aok && bok
})
}
// Update calls Update on all Updater components.
2021-07-30 14:36:11 +10:00
func (s *Scene) Update() error {
needsSort := false
curZ := -math.MaxFloat64 // this is min float64
2021-07-30 14:36:11 +10:00
for _, c := range s.Components {
// Update each updater in turn
if u, ok := c.(Updater); ok {
if err := u.Update(); err != nil {
return err
}
}
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
}
}
}
}
if needsSort {
2021-07-30 14:36:11 +10:00
s.sortByZ()
}
return nil
}