2023-11-30 06:28:09 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// match represents the game match
|
|
|
|
type match struct {
|
|
|
|
ball *ball
|
|
|
|
leftPaddle *paddle
|
|
|
|
rightPaddle *paddle
|
|
|
|
score *score
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMatch(screenWidth, screenHeight int, f *fonts) *match {
|
|
|
|
return &match{
|
|
|
|
ball: newBall(screenWidth, screenHeight),
|
|
|
|
leftPaddle: newLeftPaddle(screenWidth, screenHeight, true),
|
|
|
|
rightPaddle: newRightPaddle(screenWidth, screenHeight, false),
|
|
|
|
score: &score{
|
|
|
|
fonts: f,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var backgroundColor = color.Black
|
|
|
|
|
|
|
|
func (m *match) draw(img *ebiten.Image) {
|
|
|
|
img.Fill(backgroundColor)
|
|
|
|
|
|
|
|
m.ball.draw(img)
|
|
|
|
m.leftPaddle.draw(img)
|
|
|
|
m.rightPaddle.draw(img)
|
|
|
|
m.score.draw(img)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *match) update(bounds image.Rectangle) error {
|
2023-12-01 05:42:57 +00:00
|
|
|
if err := m.ball.update(bounds, m.leftPaddle.bounds(), m.rightPaddle.bounds()); err != nil {
|
2023-11-30 06:28:09 +00:00
|
|
|
return fmt.Errorf("couldn't update ball: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.leftPaddle.update(bounds, m.ball); err != nil {
|
|
|
|
return fmt.Errorf("couldn't update left paddle: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.rightPaddle.update(bounds, m.ball); err != nil {
|
|
|
|
return fmt.Errorf("couldn't update right paddle: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.score.update(bounds, m.ball, m.leftPaddle, m.rightPaddle); err != nil {
|
|
|
|
return fmt.Errorf("couldn't update score: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *match) resize(x, y float32) {
|
|
|
|
m.ball.resize(x, y)
|
|
|
|
m.leftPaddle.resize(x, y)
|
|
|
|
m.rightPaddle.resize(x, y)
|
|
|
|
}
|