Some other small main.lua refactoring

This commit is contained in:
Sage Vaillancourt 2025-02-24 17:01:51 -05:00
parent 7c7b5ff762
commit 9bbd68c302
1 changed files with 22 additions and 25 deletions

View File

@ -383,7 +383,8 @@ local SwingForwardDeg <const> = 170
---@param offenseHandler InputHandler ---@param offenseHandler InputHandler
function Game:updateBatting(offenseHandler) 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 self.state.batAngleDeg = batDeg
if not self.state.pitchIsOver and batDeg > SwingBackDeg and batDeg < SwingForwardDeg then 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.x = self.state.batBase.x + (C.BatLength * math.sin(batAngle))
self.state.batTip.y = self.state.batBase.y + (C.BatLength * math.cos(batAngle)) self.state.batTip.y = self.state.batBase.y + (C.BatLength * math.cos(batAngle))
if local ballWasHit = batSpeed > 0
not ( and ball.y < 232
batSpeed > 0 and utils.pointDirectlyUnderLine(
and self.state.ball.y < 232 ball.x,
and utils.pointDirectlyUnderLine( ball.y,
self.state.ball.x, self.state.batBase.x,
self.state.ball.y, self.state.batBase.y,
self.state.batBase.x, self.state.batTip.x,
self.state.batBase.y, self.state.batTip.y,
self.state.batTip.x, C.Screen.H
self.state.batTip.y,
C.Screen.H
)
) )
then
if not ballWasHit then
return return
end end
-- Hit! -- Hit!
BatCrackReverb:play() BatCrackReverb:play()
self.state.offenseState = C.Offense.running 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 mult = math.abs(batSpeed / 15)
local ballVelX = mult * 10 * math.sin(ballAngle) local ballVelX = mult * C.BattingPower * 10 * math.sin(ballAngle)
local ballVelY = mult * 5 * math.cos(ballAngle) local ballVelY = mult * C.BattingPower * 5 * math.cos(ballAngle)
if ballVelY > 0 then if ballVelY > 0 then
ballVelX = ballVelX * -1 ballVelX = ballVelX * -1
ballVelY = ballVelY * -1 ballVelY = ballVelY * -1
end end
local ballDest = local ballDest = utils.xy(ball.x + ballVelX, ball.y + ballVelY)
utils.xy(self.state.ball.x + (ballVelX * C.BattingPower), self.state.ball.y + (ballVelY * C.BattingPower))
pitchTracker:reset() pitchTracker:reset()
local flyTimeMs = 2000 local flyTimeMs = 2000
@ -450,7 +448,7 @@ function Game:updateBatting(offenseHandler)
if utils.pointIsSquarelyAboveLine(utils.xy(ballDest.x, ballDest.y), C.OutfieldWall) then if utils.pointIsSquarelyAboveLine(utils.xy(ballDest.x, ballDest.y), C.OutfieldWall) then
playdate.timer.new(flyTimeMs, function() playdate.timer.new(flyTimeMs, function()
-- Verify that the home run wasn't intercepted -- 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.announcer:say("HOME RUN!")
self.state.offenseState = C.Offense.homeRun self.state.offenseState = C.Offense.homeRun
-- Linger on the home-run ball for a moment, before panning to the bases. -- Linger on the home-run ball for a moment, before panning to the bases.
@ -464,7 +462,7 @@ function Game:updateBatting(offenseHandler)
end end
local hitBallScaler = gfx.animator.new(2000, 9 + (mult * mult * 0.5), C.SmallestBallRadius, utils.easingHill) 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.baserunning:convertBatterToRunner()
self.fielding:haveSomeoneChase(ballDest.x, ballDest.y) self.fielding:haveSomeoneChase(ballDest.x, ballDest.y)
@ -525,9 +523,10 @@ function Game:updatePitching(defenseHandler)
end end
function Game:updateGameState() function Game:updateGameState()
playdate.timer.updateTimers()
gfx.animation.blinker.updateAll()
self.state.deltaSeconds = playdate.getElapsedTime() or 0 self.state.deltaSeconds = playdate.getElapsedTime() or 0
playdate.resetElapsedTime() playdate.resetElapsedTime()
self.state.ball:updatePosition() self.state.ball:updatePosition()
local offenseHandler, defenseHandler = self:currentInputHandlers() local offenseHandler, defenseHandler = self:currentInputHandlers()
@ -582,8 +581,6 @@ function Game:updateGameState()
end end
function Game:update() function Game:update()
playdate.timer.updateTimers()
gfx.animation.blinker.updateAll()
self:updateGameState() self:updateGameState()
gfx.clear() gfx.clear()