Composable z-position

This commit is contained in:
Josh Deprez 2021-07-23 17:05:05 +10:00 committed by Josh Deprez
parent b396b247a2
commit b9ab613167
4 changed files with 30 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package engine
import (
"fmt"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -14,7 +15,7 @@ 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
func (TPSDisplay) Z() float64 {
// Always draw on top
return math.MaxFloat64
}

View file

@ -15,7 +15,7 @@ type Updater interface {
// DrawAfter is used to reorder components.
type Drawer interface {
Draw(*ebiten.Image)
DrawAfter(Drawer) bool
Z() float64
}
// Game implements the ebiten methods using a collection of components.
@ -52,13 +52,14 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
}
// Sort sorts the components by draw order.
// Non-Drawers are sorted first.
// Non-Drawers are sorted before all Drawers.
func (g *Game) Sort() {
sort.Slice(g.Components, func(i, j int) bool {
// SliceStable to avoid z-fighting (among Non-Drawers and equal Drawers)
sort.SliceStable(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 a.Z() < b.Z()
}
return !aok && bok
})

16
engine/tiles.go Normal file
View file

@ -0,0 +1,16 @@
package engine
import "github.com/hajimehoshi/ebiten/v2"
type Tilemap struct {
Map [][]int
Src *ebiten.Image
TileSize int
GeoM *ebiten.GeoM
ZPos
}
func (t *Tilemap) Draw(screen *ebiten.Image) {
}

5
engine/z.go Normal file
View file

@ -0,0 +1,5 @@
package engine
type ZPos float64
func (z ZPos) Z() float64 { return float64(z) }