Refactoring
This commit is contained in:
parent
f370324147
commit
3a19cac212
4 changed files with 86 additions and 55 deletions
15
engine/debug.go
Normal file
15
engine/debug.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TPSDisplay debugprints CurrentTPS in the top left.
|
||||||
|
type TPSDisplay struct{}
|
||||||
|
|
||||||
|
func (TPSDisplay) Draw(screen *ebiten.Image) {
|
||||||
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
||||||
|
}
|
47
engine/game.go
Normal file
47
engine/game.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Updater is a component that can update (called repeatedly).
|
||||||
|
type Updater interface {
|
||||||
|
Update() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drawer is a component that can draw itself (called repeatedly).
|
||||||
|
type Drawer interface {
|
||||||
|
Draw(*ebiten.Image)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Game implements the ebiten methods using a collection of components.
|
||||||
|
type Game struct {
|
||||||
|
ScreenWidth, ScreenHeight int
|
||||||
|
Components []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update calls Update on all Updater components.
|
||||||
|
func (g *Game) Update() error {
|
||||||
|
for _, c := range g.Components {
|
||||||
|
if u, ok := c.(Updater); ok {
|
||||||
|
if err := u.Update(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw calls Draw on all Drawer components.
|
||||||
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
|
for _, c := range g.Components {
|
||||||
|
if d, ok := c.(Drawer); ok {
|
||||||
|
d.Draw(screen)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Layout returns the configured screen width/height.
|
||||||
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
|
||||||
|
return g.ScreenWidth, g.ScreenHeight
|
||||||
|
}
|
55
game.go
55
game.go
|
@ -1,55 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
const screenWidth, screenHeight = 320, 240
|
|
||||||
|
|
||||||
type updater interface {
|
|
||||||
Update() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type drawer interface {
|
|
||||||
Draw(*ebiten.Image)
|
|
||||||
}
|
|
||||||
|
|
||||||
type game struct {
|
|
||||||
components []interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *game) Update() error {
|
|
||||||
for _, c := range g.components {
|
|
||||||
if u, ok := c.(updater); ok {
|
|
||||||
if err := u.Update(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *game) Draw(screen *ebiten.Image) {
|
|
||||||
for _, c := range g.components {
|
|
||||||
if d, ok := c.(drawer); ok {
|
|
||||||
d.Draw(screen)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *game) Layout(outsideWidth, outsideHeight int) (w, h int) {
|
|
||||||
return screenWidth, screenHeight
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
|
||||||
ebiten.SetWindowTitle("ebiten")
|
|
||||||
if err := ebiten.RunGame(&game{}); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
24
main.go
Normal file
24
main.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"drjosh.dev/gurgle/engine"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
const screenWidth, screenHeight = 320, 240
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||||
|
ebiten.SetWindowTitle("ebiten")
|
||||||
|
if err := ebiten.RunGame(&engine.Game{
|
||||||
|
ScreenHeight: screenHeight,
|
||||||
|
ScreenWidth: screenWidth,
|
||||||
|
Components: []interface{}{
|
||||||
|
engine.TPSDisplay{},
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue