Skip to content
Permalink
Browse files

Extract updateBall into its own helper

  • Loading branch information...
magopian committed Aug 7, 2019
1 parent c9cc341 commit 34722dbf26165fec2f9760334b34c0ee57e7afd3
Showing with 47 additions and 43 deletions.
  1. +47 −43 src/Main.elm
@@ -133,48 +133,8 @@ update msg model =
case msg of
OnAnimationFrame timeDelta ->
let
ball =
model.ball

shouldBounce =
shouldBallBounce model.rightPaddle model.ball
|| shouldBallBounce model.leftPaddle model.ball

horizSpeed =
if shouldBounce then
ball.horizSpeed * -1

else
ball.horizSpeed

shouldBounceVertically =
shouldBallBounceVertically model.ball

vertSpeed =
if shouldBounceVertically then
ball.vertSpeed * -1

else
ball.vertSpeed

updatedBall =
case model.gameStatus of
Winner _ ->
ball

NoWinner ->
{ ball
| x = ball.x + horizSpeed
, y = ball.y + vertSpeed
, horizSpeed = horizSpeed
, vertSpeed = vertSpeed
}

updatedRightPaddle =
updatePaddle model.rightPaddleMovement model.rightPaddle

updatedLeftPaddle =
updatePaddle model.leftPaddleMovement model.leftPaddle
updateBall model

( gameStatus, score, cmd ) =
case ( maybeWinner updatedBall, model.gameStatus ) of
@@ -198,8 +158,8 @@ update msg model =
in
( { model
| ball = updatedBall
, rightPaddle = updatedRightPaddle
, leftPaddle = updatedLeftPaddle
, rightPaddle = updatePaddle model.rightPaddleMovement model.rightPaddle
, leftPaddle = updatePaddle model.leftPaddleMovement model.leftPaddle
, gameStatus = gameStatus
, score = score
}
@@ -259,6 +219,50 @@ update msg model =
)


updateBall :
{ a
| gameStatus : GameStatus
, ball : Ball
, rightPaddle : Paddle
, leftPaddle : Paddle
}
-> Ball
updateBall { gameStatus, ball, rightPaddle, leftPaddle } =
let
shouldBounce =
shouldBallBounce rightPaddle ball
|| shouldBallBounce leftPaddle ball

horizSpeed =
if shouldBounce then
ball.horizSpeed * -1

else
ball.horizSpeed

shouldBounceVertically =
shouldBallBounceVertically ball

vertSpeed =
if shouldBounceVertically then
ball.vertSpeed * -1

else
ball.vertSpeed
in
case gameStatus of
Winner _ ->
ball

NoWinner ->
{ ball
| x = ball.x + horizSpeed
, y = ball.y + vertSpeed
, horizSpeed = horizSpeed
, vertSpeed = vertSpeed
}


updatePaddle : PaddleMovement -> Paddle -> Paddle
updatePaddle movement paddle =
let

0 comments on commit 34722db

Please sign in to comment.
You can’t perform that action at this time.