feat: add match status

This commit is contained in:
Dawid Wysokiński 2023-12-01 07:19:38 +01:00
parent cfacd2704d
commit 1f7a8aed38
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
4 changed files with 102 additions and 16 deletions

View File

@ -118,7 +118,7 @@ linters-settings:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#argument-limit
- name: argument-limit
severity: warning
disabled: false
disabled: true
arguments: [4]
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#atomic
- name: atomic

View File

@ -8,12 +8,22 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)
type matchStatus uint8
const (
matchStatusNotStarted matchStatus = iota
matchStatusInProgress
matchStatusEnded
)
// match represents the game match
type match struct {
status matchStatus
ball *ball
leftPaddle *paddle
rightPaddle *paddle
score *score
fonts *fonts
}
func newMatch(screenWidth, screenHeight int, f *fonts) *match {
@ -24,6 +34,7 @@ func newMatch(screenWidth, screenHeight int, f *fonts) *match {
score: &score{
fonts: f,
},
fonts: f,
}
}
@ -32,27 +43,78 @@ 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)
switch m.status {
case matchStatusNotStarted:
bounds := img.Bounds()
drawCenteredText(
img,
"Press space to start!",
m.fonts.robotoRegularFont,
bounds.Max.X/2,
bounds.Max.Y/2,
color.White,
)
case matchStatusInProgress:
m.ball.draw(img)
m.leftPaddle.draw(img)
m.rightPaddle.draw(img)
m.score.draw(img)
case matchStatusEnded:
bounds := img.Bounds()
winLoseMessage := "You win!"
if m.score.right >= requiredScoreToWin {
winLoseMessage = "You lose :(."
}
drawCenteredText(
img,
winLoseMessage+" Press space to start!",
m.fonts.robotoRegularFont,
bounds.Max.X/2,
bounds.Max.Y/2,
color.White,
)
}
}
const requiredScoreToWin = 1
//nolint:gocyclo
func (m *match) update(bounds image.Rectangle) error {
if err := m.ball.update(bounds, m.leftPaddle.bounds(), m.rightPaddle.bounds()); err != nil {
return fmt.Errorf("couldn't update ball: %w", err)
}
switch m.status {
case matchStatusNotStarted:
if ebiten.IsKeyPressed(ebiten.KeySpace) {
m.status = matchStatusInProgress
}
case matchStatusInProgress:
if err := m.ball.update(bounds, m.leftPaddle.bounds(), m.rightPaddle.bounds()); err != nil {
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.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.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)
if err := m.score.update(bounds, m.ball, m.leftPaddle, m.rightPaddle); err != nil {
return fmt.Errorf("couldn't update score: %w", err)
}
if m.score.left >= requiredScoreToWin || m.score.right >= requiredScoreToWin {
m.status = matchStatusEnded
}
case matchStatusEnded:
if !ebiten.IsKeyPressed(ebiten.KeySpace) {
return nil
}
m.ball.reset()
m.leftPaddle.reset()
m.rightPaddle.reset()
m.score.reset()
m.status = matchStatusInProgress
}
return nil

View File

@ -60,3 +60,8 @@ func (s *score) update(
return nil
}
func (s *score) reset() {
s.left = 0
s.right = 0
}

19
internal/utils.go Normal file
View File

@ -0,0 +1,19 @@
package internal
import (
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
)
func drawCenteredText(img *ebiten.Image, s string, f font.Face, cx, cy int, c color.Color) {
bounds, _ := font.BoundString(f, s)
minX := bounds.Min.X.Round()
minY := bounds.Min.Y.Round()
maxX := bounds.Max.X.Round()
maxY := bounds.Max.Y.Round()
x, y := cx-minX-(maxX-minX)/2, cy-minY-(maxY-minY)
text.Draw(img, s, f, x, y, c)
}