From 9bbd68c302a9b1bdc2e52f5eb9c21f2935582771 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 24 Feb 2025 17:01:51 -0500 Subject: [PATCH] Some other small main.lua refactoring --- src/main.lua | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main.lua b/src/main.lua index dfa1881..b1219b5 100644 --- a/src/main.lua +++ b/src/main.lua @@ -383,7 +383,8 @@ local SwingForwardDeg = 170 ---@param offenseHandler InputHandler function Game:updateBatting(offenseHandler) - local batDeg, batSpeed = offenseHandler:updateBat(self.state.ball, self.state.pitchIsOver, self.state.deltaSeconds) + local ball = self.state.ball + local batDeg, batSpeed = offenseHandler:updateBat(ball, self.state.pitchIsOver, self.state.deltaSeconds) self.state.batAngleDeg = batDeg if not self.state.pitchIsOver and batDeg > SwingBackDeg and batDeg < SwingForwardDeg then @@ -400,39 +401,36 @@ function Game:updateBatting(offenseHandler) self.state.batTip.x = self.state.batBase.x + (C.BatLength * math.sin(batAngle)) self.state.batTip.y = self.state.batBase.y + (C.BatLength * math.cos(batAngle)) - if - not ( - batSpeed > 0 - and self.state.ball.y < 232 - and utils.pointDirectlyUnderLine( - self.state.ball.x, - self.state.ball.y, - self.state.batBase.x, - self.state.batBase.y, - self.state.batTip.x, - self.state.batTip.y, - C.Screen.H - ) + local ballWasHit = batSpeed > 0 + and ball.y < 232 + and utils.pointDirectlyUnderLine( + ball.x, + ball.y, + self.state.batBase.x, + self.state.batBase.y, + self.state.batTip.x, + self.state.batTip.y, + C.Screen.H ) - then + + if not ballWasHit then return end -- Hit! BatCrackReverb:play() self.state.offenseState = C.Offense.running - local ballAngle = batAngle + math.rad(90) + local ballAngle = batAngle + math.rad(90) local mult = math.abs(batSpeed / 15) - local ballVelX = mult * 10 * math.sin(ballAngle) - local ballVelY = mult * 5 * math.cos(ballAngle) + local ballVelX = mult * C.BattingPower * 10 * math.sin(ballAngle) + local ballVelY = mult * C.BattingPower * 5 * math.cos(ballAngle) if ballVelY > 0 then ballVelX = ballVelX * -1 ballVelY = ballVelY * -1 end - local ballDest = - utils.xy(self.state.ball.x + (ballVelX * C.BattingPower), self.state.ball.y + (ballVelY * C.BattingPower)) + local ballDest = utils.xy(ball.x + ballVelX, ball.y + ballVelY) pitchTracker:reset() local flyTimeMs = 2000 @@ -450,7 +448,7 @@ function Game:updateBatting(offenseHandler) if utils.pointIsSquarelyAboveLine(utils.xy(ballDest.x, ballDest.y), C.OutfieldWall) then playdate.timer.new(flyTimeMs, function() -- Verify that the home run wasn't intercepted - if utils.within(1, self.state.ball.x, ballDest.x) and utils.within(1, self.state.ball.y, ballDest.y) then + if utils.within(1, ball.x, ballDest.x) and utils.within(1, ball.y, ballDest.y) then self.announcer:say("HOME RUN!") self.state.offenseState = C.Offense.homeRun -- Linger on the home-run ball for a moment, before panning to the bases. @@ -464,7 +462,7 @@ function Game:updateBatting(offenseHandler) end local hitBallScaler = gfx.animator.new(2000, 9 + (mult * mult * 0.5), C.SmallestBallRadius, utils.easingHill) - self.state.ball:launch(ballDest.x, ballDest.y, playdate.easingFunctions.outQuint, flyTimeMs, nil, hitBallScaler) + ball:launch(ballDest.x, ballDest.y, playdate.easingFunctions.outQuint, flyTimeMs, nil, hitBallScaler) self.baserunning:convertBatterToRunner() self.fielding:haveSomeoneChase(ballDest.x, ballDest.y) @@ -525,9 +523,10 @@ function Game:updatePitching(defenseHandler) end function Game:updateGameState() + playdate.timer.updateTimers() + gfx.animation.blinker.updateAll() self.state.deltaSeconds = playdate.getElapsedTime() or 0 playdate.resetElapsedTime() - self.state.ball:updatePosition() local offenseHandler, defenseHandler = self:currentInputHandlers() @@ -582,8 +581,6 @@ function Game:updateGameState() end function Game:update() - playdate.timer.updateTimers() - gfx.animation.blinker.updateAll() self:updateGameState() gfx.clear()