Tweaks
This commit is contained in:
parent
8c23394fb9
commit
17c68f5455
3 changed files with 38 additions and 18 deletions
|
@ -8,14 +8,14 @@ import (
|
|||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||
)
|
||||
|
||||
// TPSDisplay debugprints CurrentTPS in the top left.
|
||||
type TPSDisplay struct{}
|
||||
// PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
|
||||
type PerfDisplay struct{}
|
||||
|
||||
func (TPSDisplay) Draw(screen *ebiten.Image) {
|
||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
||||
func (PerfDisplay) Draw(screen *ebiten.Image) {
|
||||
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
|
||||
return math.MaxFloat64
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ type Game struct {
|
|||
ScreenWidth int
|
||||
ScreenHeight int
|
||||
Components []interface{}
|
||||
|
||||
needsSort bool
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
|
@ -51,9 +57,15 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
|
|||
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.
|
||||
func (g *Game) Sort() {
|
||||
func (g *Game) sortDrawers() {
|
||||
// Stable sort 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)
|
||||
|
|
30
main.go
30
main.go
|
@ -19,14 +19,19 @@ func main() {
|
|||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("TODO")
|
||||
|
||||
boxesFile, err := assets.Open("assets/boxes.png")
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't open asset: %v", err)
|
||||
}
|
||||
boxesPNG, _, err := image.Decode(boxesFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't decode asset: %v", err)
|
||||
}
|
||||
var boxesPNG image.Image
|
||||
func() {
|
||||
boxesFile, err := assets.Open("assets/boxes.png")
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't open asset: %v", err)
|
||||
}
|
||||
defer boxesFile.Close()
|
||||
boxesPNG, _, err = image.Decode(boxesFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't decode asset: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
tiles := &engine.Tilemap{
|
||||
Map: [][]int{
|
||||
{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,
|
||||
}
|
||||
|
||||
if err := ebiten.RunGame(&engine.Game{
|
||||
game := &engine.Game{
|
||||
ScreenHeight: screenHeight,
|
||||
ScreenWidth: screenWidth,
|
||||
Components: []interface{}{
|
||||
tiles,
|
||||
engine.TPSDisplay{},
|
||||
engine.PerfDisplay{},
|
||||
},
|
||||
}); err != nil {
|
||||
}
|
||||
game.SetNeedsSort()
|
||||
|
||||
if err := ebiten.RunGame(game); err != nil {
|
||||
log.Fatalf("Game error: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue