90 lines
1.5 KiB
Go
90 lines
1.5 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
)
|
|
|
|
const (
|
|
BaseWidth = 640
|
|
BaseHeight = 360
|
|
)
|
|
|
|
type Game struct {
|
|
width int
|
|
height int
|
|
ball *ball
|
|
debug bool
|
|
img *ebiten.Image
|
|
}
|
|
|
|
var _ ebiten.Game = (*Game)(nil)
|
|
|
|
func NewGame(width, height int, debug bool) *Game {
|
|
return &Game{
|
|
width: width,
|
|
height: height,
|
|
ball: newBall(width, height),
|
|
debug: debug,
|
|
}
|
|
}
|
|
|
|
func (g *Game) Update() error {
|
|
return g.ball.update(image.Rect(0, 0, g.width, g.height))
|
|
}
|
|
|
|
var backgroundColor = color.Black
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
if g.img == nil {
|
|
g.img = ebiten.NewImage(g.width, g.height)
|
|
}
|
|
|
|
g.img.Fill(backgroundColor)
|
|
|
|
g.ball.draw(g.img)
|
|
|
|
if g.debug {
|
|
g.drawDebug()
|
|
}
|
|
|
|
screen.DrawImage(g.img, nil)
|
|
}
|
|
|
|
func (g *Game) drawDebug() {
|
|
ebitenutil.DebugPrint(
|
|
g.img,
|
|
fmt.Sprintf(
|
|
"FPS: %0.2f"+
|
|
"\nBall: x=%v, y=%v, speedX=%v, speedY=%v, radius=%v",
|
|
ebiten.ActualFPS(),
|
|
g.ball.x,
|
|
g.ball.y,
|
|
g.ball.speedX,
|
|
g.ball.speedY,
|
|
g.ball.r,
|
|
),
|
|
)
|
|
}
|
|
|
|
//nolint:nonamedreturns
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (_screenWidth, _screenHeight int) {
|
|
if g.width != outsideWidth || g.height != outsideHeight {
|
|
g.ball.resize(float32(outsideWidth)/float32(g.width), float32(outsideHeight)/float32(g.height))
|
|
|
|
if g.img != nil {
|
|
g.img.Dispose()
|
|
g.img = nil
|
|
}
|
|
|
|
g.width = outsideWidth
|
|
g.height = outsideHeight
|
|
}
|
|
|
|
return outsideWidth, outsideHeight
|
|
}
|