refactor: refactor ball <-> paddle collision logic #3
|
@ -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:
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user