Implement sort?

This commit is contained in:
Josh Deprez 2021-07-23 13:46:19 +10:00 committed by Josh Deprez
parent 68b29ab021
commit 7b321a549f
2 changed files with 21 additions and 0 deletions

View file

@ -13,3 +13,8 @@ type TPSDisplay struct{}
func (TPSDisplay) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
}
func (TPSDisplay) DrawAfter(Drawer) bool {
// Always draw last
return true
}

View file

@ -1,6 +1,8 @@
package engine
import (
"sort"
"github.com/hajimehoshi/ebiten/v2"
)
@ -12,6 +14,7 @@ type Updater interface {
// Drawer is a component that can draw itself (called repeatedly).
type Drawer interface {
Draw(*ebiten.Image)
DrawAfter(Drawer) bool
}
// Game implements the ebiten methods using a collection of components.
@ -46,3 +49,16 @@ func (g *Game) Draw(screen *ebiten.Image) {
func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
return g.ScreenWidth, g.ScreenHeight
}
// Sort sorts the components by draw order.
// Non-Drawers are sorted first.
func (g *Game) Sort() {
sort.Slice(g.Components, func(i, j int) bool {
a, aok := g.Components[i].(Drawer)
b, bok := g.Components[j].(Drawer)
if aok && bok {
return b.DrawAfter(a)
}
return !aok && bok
})
}