2021-07-14 16:14:32 +10:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-14 16:26:47 +10:00
|
|
|
"fmt"
|
2021-07-14 16:14:32 +10:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2021-07-14 16:26:47 +10:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
2021-07-14 16:14:32 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
const screenWidth, screenHeight = 320, 240
|
|
|
|
|
2021-07-16 15:41:46 +10:00
|
|
|
type updater interface {
|
|
|
|
Update() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type drawer interface {
|
|
|
|
Draw(*ebiten.Image)
|
|
|
|
}
|
|
|
|
|
|
|
|
type game struct {
|
|
|
|
components []interface{}
|
|
|
|
}
|
2021-07-14 16:14:32 +10:00
|
|
|
|
|
|
|
func (g *game) Update() error {
|
2021-07-16 15:41:46 +10:00
|
|
|
for _, c := range g.components {
|
|
|
|
if u, ok := c.(updater); ok {
|
|
|
|
if err := u.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 16:14:32 +10:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) Draw(screen *ebiten.Image) {
|
2021-07-16 15:41:46 +10:00
|
|
|
for _, c := range g.components {
|
|
|
|
if d, ok := c.(drawer); ok {
|
|
|
|
d.Draw(screen)
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 16:26:47 +10:00
|
|
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
2021-07-14 16:14:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|