From 45120fe46e03112475c049aacec54bf2afbe5c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Thu, 30 Nov 2023 08:24:23 +0100 Subject: [PATCH] refactor: refactor ball <-> paddle collision logic --- .golangci.yml | 6 ++++- internal/ball.go | 43 +++++++++++++++++++++------------ internal/{court.go => match.go} | 7 +----- internal/paddle.go | 2 +- 4 files changed, 35 insertions(+), 23 deletions(-) rename internal/{court.go => match.go} (84%) diff --git a/.golangci.yml b/.golangci.yml index 8c27c22..707c1eb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ run: linters: disable-all: true enable: + - gocyclo - asasalint - asciicheck - bodyclose @@ -54,6 +55,8 @@ linters: - revive linters-settings: + gocyclo: + min-complexity: 10 tagliatelle: case: rules: @@ -176,7 +179,7 @@ linters-settings: # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#cyclomatic - name: cyclomatic severity: warning - disabled: false + disabled: true arguments: [10] # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#datarace - name: datarace @@ -465,6 +468,7 @@ issues: - path: _test\.go linters: - gosec + - gocyclo - path: _test\.go text: add-constant - linters: diff --git a/internal/ball.go b/internal/ball.go index e44b40a..f9b18c3 100644 --- a/internal/ball.go +++ b/internal/ball.go @@ -47,35 +47,52 @@ func (b *ball) draw(img *ebiten.Image) { vector.DrawFilledCircle(img, b.x, b.y, b.r, ballColor, false) } -func (b *ball) update(bounds image.Rectangle) error { - b.x, b.y, b.speedX, b.speedY = b.nextPosAndSpeed(bounds) +func (b *ball) update(bounds image.Rectangle, obstacles ...image.Rectangle) error { + b.x, b.y, b.speedX, b.speedY = b.nextPosAndSpeed(bounds, obstacles...) return nil } //nolint:nonamedreturns -func (b *ball) nextPos(bounds image.Rectangle) (x, y float32) { - x, y, _, _ = b.nextPosAndSpeed(bounds) +func (b *ball) nextPos(bounds image.Rectangle, obstacles ...image.Rectangle) (x, y float32) { + x, y, _, _ = b.nextPosAndSpeed(bounds, obstacles...) return x, y } //nolint:nonamedreturns -func (b *ball) nextPosAndSpeed(bounds image.Rectangle) (x, y, speedX, speedY float32) { - speedX, speedY = b.nextSpeed(bounds) +func (b *ball) nextPosAndSpeed(bounds image.Rectangle, obstacles ...image.Rectangle) (x, y, speedX, speedY float32) { + speedX, speedY = b.nextSpeed(bounds, obstacles...) return b.x + speedX, b.y + speedY, speedX, speedY } -//nolint:nonamedreturns -func (b *ball) nextSpeed(bounds image.Rectangle) (speedX, speedY float32) { +//nolint:nonamedreturns,gocyclo +func (b *ball) nextSpeed(bounds image.Rectangle, obstacles ...image.Rectangle) (speedX, speedY float32) { + ballBounds := b.bounds() speedX = b.speedX - if b.x+b.r >= float32(bounds.Max.X) || b.x-b.r <= float32(bounds.Min.X) { + speedY = b.speedY + + if ballBounds.Max.X >= bounds.Max.X || ballBounds.Min.X <= bounds.Min.X { speedX *= -1 } - speedY = b.speedY - if b.y+b.r >= float32(bounds.Max.Y) || b.y-b.r <= float32(bounds.Min.Y) { + if ballBounds.Max.Y >= bounds.Max.Y || ballBounds.Min.Y <= bounds.Min.Y { speedY *= -1 } + for _, ob := range obstacles { + inter := ob.Intersect(ballBounds) + if inter == (image.Rectangle{}) { + continue + } + + if float32(ob.Min.X) >= b.x || float32(ob.Max.X) <= b.x { + speedX *= -1 + } + + if float32(ob.Min.Y) >= b.y || float32(ob.Max.Y) <= b.y { + speedY *= -1 + } + } + return speedX, speedY } @@ -94,10 +111,6 @@ func (b *ball) resize(x, y float32) { b.r *= y } -func (b *ball) reverseDirectionHorizontal() { - b.speedX *= -1 -} - func (b *ball) bounds() image.Rectangle { return image.Rect(int(b.x-b.r), int(b.y-b.r), int(b.x+b.r), int(b.y+b.r)) } diff --git a/internal/court.go b/internal/match.go similarity index 84% rename from internal/court.go rename to internal/match.go index 62c7f77..2989c24 100644 --- a/internal/court.go +++ b/internal/match.go @@ -39,7 +39,7 @@ func (m *match) draw(img *ebiten.Image) { } func (m *match) update(bounds image.Rectangle) error { - if err := m.ball.update(bounds); err != nil { + if err := m.ball.update(bounds, m.leftPaddle.bounds(), m.rightPaddle.bounds()); err != nil { return fmt.Errorf("couldn't update ball: %w", err) } @@ -51,11 +51,6 @@ func (m *match) update(bounds image.Rectangle) error { return fmt.Errorf("couldn't update right paddle: %w", err) } - if m.ball.bounds().Intersect(m.leftPaddle.bounds()) != (image.Rectangle{}) || - m.ball.bounds().Intersect(m.rightPaddle.bounds()) != (image.Rectangle{}) { - m.ball.reverseDirectionHorizontal() - } - if err := m.score.update(bounds, m.ball, m.leftPaddle, m.rightPaddle); err != nil { return fmt.Errorf("couldn't update score: %w", err) } diff --git a/internal/paddle.go b/internal/paddle.go index 3e6e37b..1baa2bd 100644 --- a/internal/paddle.go +++ b/internal/paddle.go @@ -71,7 +71,7 @@ func (p *paddle) draw(img *ebiten.Image) { vector.DrawFilledRect(img, p.x, p.y, p.width, p.height, paddleColor, false) } -// nolint:Revive,cyclomatic +//nolint:gocyclo func (p *paddle) update(bounds image.Rectangle, b *ball) error { newY := p.y -- 2.45.1