diff --git a/engine/debug.go b/engine/debug.go index ca743b0..198f5a5 100644 --- a/engine/debug.go +++ b/engine/debug.go @@ -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 +} diff --git a/engine/game.go b/engine/game.go index 00c1be2..f7029b6 100644 --- a/engine/game.go +++ b/engine/game.go @@ -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 + }) +}