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"
|
"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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
30
main.go
30
main.go
|
@ -19,14 +19,19 @@ func main() {
|
||||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||||
ebiten.SetWindowTitle("TODO")
|
ebiten.SetWindowTitle("TODO")
|
||||||
|
|
||||||
boxesFile, err := assets.Open("assets/boxes.png")
|
var boxesPNG image.Image
|
||||||
if err != nil {
|
func() {
|
||||||
log.Fatalf("Couldn't open asset: %v", err)
|
boxesFile, err := assets.Open("assets/boxes.png")
|
||||||
}
|
if err != nil {
|
||||||
boxesPNG, _, err := image.Decode(boxesFile)
|
log.Fatalf("Couldn't open asset: %v", err)
|
||||||
if err != nil {
|
}
|
||||||
log.Fatalf("Couldn't decode 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{
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue