This commit is contained in:
Josh Deprez 2021-07-25 15:03:10 +10:00 committed by Josh Deprez
parent 8c23394fb9
commit 17c68f5455
3 changed files with 38 additions and 18 deletions

View file

@ -8,14 +8,14 @@ import (
"github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/ebitenutil"
) )
// TPSDisplay debugprints CurrentTPS in the top left. // PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
type TPSDisplay struct{} type PerfDisplay struct{}
func (TPSDisplay) Draw(screen *ebiten.Image) { func (PerfDisplay) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
} }
func (TPSDisplay) Z() float64 { func (PerfDisplay) Z() float64 {
// Always draw on top // Always draw on top
return math.MaxFloat64 return math.MaxFloat64
} }

View file

@ -23,6 +23,8 @@ type Game struct {
ScreenWidth int ScreenWidth int
ScreenHeight int ScreenHeight int
Components []interface{} Components []interface{}
needsSort bool
} }
// Update calls Update on all Updater components. // Update calls Update on all Updater components.
@ -34,6 +36,10 @@ func (g *Game) Update() error {
} }
} }
} }
if g.needsSort {
g.needsSort = false
g.sortDrawers()
}
return nil return nil
} }
@ -51,9 +57,15 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
return g.ScreenWidth, g.ScreenHeight return g.ScreenWidth, g.ScreenHeight
} }
// Sort sorts the components by Z position. // SetNeedsSort tells the game that the Drawers need sorting.
// This will be done in the current update.
func (g *Game) SetNeedsSort() {
g.needsSort = true
}
// sortDrawers sorts the components by Z position.
// Non-Drawers are sorted before all Drawers. // Non-Drawers are sorted before all Drawers.
func (g *Game) Sort() { func (g *Game) sortDrawers() {
// Stable sort to avoid z-fighting (among Non-Drawers and equal Drawers) // Stable sort to avoid z-fighting (among Non-Drawers and equal Drawers)
sort.SliceStable(g.Components, func(i, j int) bool { sort.SliceStable(g.Components, func(i, j int) bool {
a, aok := g.Components[i].(Drawer) a, aok := g.Components[i].(Drawer)

16
main.go
View file

@ -19,14 +19,19 @@ func main() {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2) ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("TODO") ebiten.SetWindowTitle("TODO")
var boxesPNG image.Image
func() {
boxesFile, err := assets.Open("assets/boxes.png") boxesFile, err := assets.Open("assets/boxes.png")
if err != nil { if err != nil {
log.Fatalf("Couldn't open asset: %v", err) log.Fatalf("Couldn't open asset: %v", err)
} }
boxesPNG, _, err := image.Decode(boxesFile) defer boxesFile.Close()
boxesPNG, _, err = image.Decode(boxesFile)
if err != nil { if err != nil {
log.Fatalf("Couldn't decode asset: %v", err) log.Fatalf("Couldn't decode asset: %v", err)
} }
}()
tiles := &engine.Tilemap{ tiles := &engine.Tilemap{
Map: [][]int{ Map: [][]int{
{0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1}, {0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1},
@ -49,14 +54,17 @@ func main() {
TileSize: 16, TileSize: 16,
} }
if err := ebiten.RunGame(&engine.Game{ game := &engine.Game{
ScreenHeight: screenHeight, ScreenHeight: screenHeight,
ScreenWidth: screenWidth, ScreenWidth: screenWidth,
Components: []interface{}{ Components: []interface{}{
tiles, tiles,
engine.TPSDisplay{}, engine.PerfDisplay{},
}, },
}); err != nil { }
game.SetNeedsSort()
if err := ebiten.RunGame(game); err != nil {
log.Fatalf("Game error: %v", err) log.Fatalf("Game error: %v", err)
} }
} }