refactor: refactor ball <-> paddle collision logic #3
|
@ -5,6 +5,7 @@ run:
|
||||||
linters:
|
linters:
|
||||||
disable-all: true
|
disable-all: true
|
||||||
enable:
|
enable:
|
||||||
|
- gocyclo
|
||||||
- asasalint
|
- asasalint
|
||||||
- asciicheck
|
- asciicheck
|
||||||
- bodyclose
|
- bodyclose
|
||||||
|
@ -54,6 +55,8 @@ linters:
|
||||||
- revive
|
- revive
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
|
gocyclo:
|
||||||
|
min-complexity: 10
|
||||||
tagliatelle:
|
tagliatelle:
|
||||||
case:
|
case:
|
||||||
rules:
|
rules:
|
||||||
|
@ -176,7 +179,7 @@ linters-settings:
|
||||||
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#cyclomatic
|
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#cyclomatic
|
||||||
- name: cyclomatic
|
- name: cyclomatic
|
||||||
severity: warning
|
severity: warning
|
||||||
disabled: false
|
disabled: true
|
||||||
arguments: [10]
|
arguments: [10]
|
||||||
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#datarace
|
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#datarace
|
||||||
- name: datarace
|
- name: datarace
|
||||||
|
@ -465,6 +468,7 @@ issues:
|
||||||
- path: _test\.go
|
- path: _test\.go
|
||||||
linters:
|
linters:
|
||||||
- gosec
|
- gosec
|
||||||
|
- gocyclo
|
||||||
- path: _test\.go
|
- path: _test\.go
|
||||||
text: add-constant
|
text: add-constant
|
||||||
- linters:
|
- linters:
|
||||||
|
|
|
@ -47,35 +47,52 @@ func (b *ball) draw(img *ebiten.Image) {
|
||||||
vector.DrawFilledCircle(img, b.x, b.y, b.r, ballColor, false)
|
vector.DrawFilledCircle(img, b.x, b.y, b.r, ballColor, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ball) update(bounds image.Rectangle) error {
|
func (b *ball) update(bounds image.Rectangle, obstacles ...image.Rectangle) error {
|
||||||
b.x, b.y, b.speedX, b.speedY = b.nextPosAndSpeed(bounds)
|
b.x, b.y, b.speedX, b.speedY = b.nextPosAndSpeed(bounds, obstacles...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:nonamedreturns
|
//nolint:nonamedreturns
|
||||||
func (b *ball) nextPos(bounds image.Rectangle) (x, y float32) {
|
func (b *ball) nextPos(bounds image.Rectangle, obstacles ...image.Rectangle) (x, y float32) {
|
||||||
x, y, _, _ = b.nextPosAndSpeed(bounds)
|
x, y, _, _ = b.nextPosAndSpeed(bounds, obstacles...)
|
||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:nonamedreturns
|
//nolint:nonamedreturns
|
||||||
func (b *ball) nextPosAndSpeed(bounds image.Rectangle) (x, y, speedX, speedY float32) {
|
func (b *ball) nextPosAndSpeed(bounds image.Rectangle, obstacles ...image.Rectangle) (x, y, speedX, speedY float32) {
|
||||||
speedX, speedY = b.nextSpeed(bounds)
|
speedX, speedY = b.nextSpeed(bounds, obstacles...)
|
||||||
return b.x + speedX, b.y + speedY, speedX, speedY
|
return b.x + speedX, b.y + speedY, speedX, speedY
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:nonamedreturns
|
//nolint:nonamedreturns,gocyclo
|
||||||
func (b *ball) nextSpeed(bounds image.Rectangle) (speedX, speedY float32) {
|
func (b *ball) nextSpeed(bounds image.Rectangle, obstacles ...image.Rectangle) (speedX, speedY float32) {
|
||||||
|
ballBounds := b.bounds()
|
||||||
speedX = b.speedX
|
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
|
speedX *= -1
|
||||||
}
|
}
|
||||||
|
|
||||||
speedY = b.speedY
|
if ballBounds.Max.Y >= bounds.Max.Y || ballBounds.Min.Y <= bounds.Min.Y {
|
||||||
if b.y+b.r >= float32(bounds.Max.Y) || b.y-b.r <= float32(bounds.Min.Y) {
|
|
||||||
speedY *= -1
|
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
|
return speedX, speedY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,10 +111,6 @@ func (b *ball) resize(x, y float32) {
|
||||||
b.r *= y
|
b.r *= y
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ball) reverseDirectionHorizontal() {
|
|
||||||
b.speedX *= -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *ball) bounds() image.Rectangle {
|
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))
|
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 {
|
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)
|
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)
|
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 {
|
if err := m.score.update(bounds, m.ball, m.leftPaddle, m.rightPaddle); err != nil {
|
||||||
return fmt.Errorf("couldn't update score: %w", err)
|
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)
|
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 {
|
func (p *paddle) update(bounds image.Rectangle, b *ball) error {
|
||||||
newY := p.y
|
newY := p.y
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user