Composable z-position
This commit is contained in:
parent
b396b247a2
commit
b9ab613167
4 changed files with 30 additions and 7 deletions
|
@ -2,6 +2,7 @@ package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
"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()))
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (TPSDisplay) DrawAfter(Drawer) bool {
|
func (TPSDisplay) Z() float64 {
|
||||||
// Always draw last
|
// Always draw on top
|
||||||
return true
|
return math.MaxFloat64
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ type Updater interface {
|
||||||
// DrawAfter is used to reorder components.
|
// DrawAfter is used to reorder components.
|
||||||
type Drawer interface {
|
type Drawer interface {
|
||||||
Draw(*ebiten.Image)
|
Draw(*ebiten.Image)
|
||||||
DrawAfter(Drawer) bool
|
Z() float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Game implements the ebiten methods using a collection of components.
|
// 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.
|
// Sort sorts the components by draw order.
|
||||||
// Non-Drawers are sorted first.
|
// Non-Drawers are sorted before all Drawers.
|
||||||
func (g *Game) Sort() {
|
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)
|
a, aok := g.Components[i].(Drawer)
|
||||||
b, bok := g.Components[j].(Drawer)
|
b, bok := g.Components[j].(Drawer)
|
||||||
if aok && bok {
|
if aok && bok {
|
||||||
return b.DrawAfter(a)
|
return a.Z() < b.Z()
|
||||||
}
|
}
|
||||||
return !aok && bok
|
return !aok && bok
|
||||||
})
|
})
|
||||||
|
|
16
engine/tiles.go
Normal file
16
engine/tiles.go
Normal 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
5
engine/z.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package engine
|
||||||
|
|
||||||
|
type ZPos float64
|
||||||
|
|
||||||
|
func (z ZPos) Z() float64 { return float64(z) }
|
Loading…
Reference in a new issue