extract-input-controllers #4

Merged
sage merged 5 commits from extract-input-controllers into main 2025-02-24 19:50:23 -05:00
1 changed files with 35 additions and 33 deletions
Showing only changes of commit 7c7b5ff762 - Show all commits

View File

@ -381,8 +381,11 @@ end
local SwingBackDeg <const> = 30 local SwingBackDeg <const> = 30
local SwingForwardDeg <const> = 170 local SwingForwardDeg <const> = 170
---@param batDeg number ---@param offenseHandler InputHandler
function Game:updateBatting(batDeg, batSpeed) function Game:updateBatting(offenseHandler)
local batDeg, batSpeed = offenseHandler:updateBat(self.state.ball, self.state.pitchIsOver, self.state.deltaSeconds)
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
self.state.didSwing = true self.state.didSwing = true
end end
@ -468,7 +471,6 @@ function Game:updateBatting(batDeg, batSpeed)
end end
---@param appliedSpeed number | fun(runner: Runner): number ---@param appliedSpeed number | fun(runner: Runner): number
---
---@return boolean runnersStillMoving, number secondsSinceLastRunnerMove ---@return boolean runnersStillMoving, number secondsSinceLastRunnerMove
function Game:updateNonBatterRunners(appliedSpeed, forcedOnly, isAutoRun) function Game:updateNonBatterRunners(appliedSpeed, forcedOnly, isAutoRun)
local runnersStillMoving, runnersScored, secondsSinceLastRunnerMove = local runnersStillMoving, runnersScored, secondsSinceLastRunnerMove =
@ -497,21 +499,11 @@ function Game:returnToPitcher()
end) end)
end end
function Game:updateGameState() ---@param defenseHandler InputHandler
self.state.deltaSeconds = playdate.getElapsedTime() or 0 function Game:updatePitching(defenseHandler)
playdate.resetElapsedTime()
self.state.ball:updatePosition()
local offenseHandler, defenseHandler = self:currentInputHandlers()
local fielderHoldingBall = self.fielding:updateFielderPositions(self.state.ball, self.state.deltaSeconds)
if self.state.offenseState == C.Offense.batting then
pitchTracker:recordIfPassed(self.state.ball) pitchTracker:recordIfPassed(self.state.ball)
local pitcher = self.fielding.fielders.pitcher if self:pitcherIsOnTheMound() then
if utils.distanceBetween(pitcher.x, pitcher.y, C.PitcherStartPos.x, C.PitcherStartPos.y) < C.BaseHitbox then
pitchTracker.secondsSinceLastPitch = pitchTracker.secondsSinceLastPitch + self.state.deltaSeconds pitchTracker.secondsSinceLastPitch = pitchTracker.secondsSinceLastPitch + self.state.deltaSeconds
end end
@ -527,18 +519,27 @@ function Game:updateGameState()
self.state.didSwing = false self.state.didSwing = false
end end
local batSpeed
self.state.batAngleDeg, batSpeed =
offenseHandler:updateBat(self.state.ball, self.state.pitchIsOver, self.state.deltaSeconds)
self:updateBatting(self.state.batAngleDeg, batSpeed)
-- Walk batter to the plate
self.baserunning:updateRunner(self.baserunning.batter, nil, 0, false, self.state.deltaSeconds)
if pitchTracker.secondsSinceLastPitch > C.PitchAfterSeconds then if pitchTracker.secondsSinceLastPitch > C.PitchAfterSeconds then
defenseHandler:pitch() defenseHandler:pitch()
end end
end
function Game:updateGameState()
self.state.deltaSeconds = playdate.getElapsedTime() or 0
playdate.resetElapsedTime()
self.state.ball:updatePosition()
local offenseHandler, defenseHandler = self:currentInputHandlers()
local fielderHoldingBall = self.fielding:updateFielderPositions(self.state.ball, self.state.deltaSeconds)
if self.state.offenseState == C.Offense.batting then
self:updatePitching(defenseHandler)
self:updateBatting(offenseHandler)
-- Walk batter to the plate
self.baserunning:updateRunner(self.baserunning.batter, nil, 0, false, self.state.deltaSeconds)
elseif self.state.offenseState == C.Offense.running then elseif self.state.offenseState == C.Offense.running then
local appliedSpeed = function(runner) local appliedSpeed = function(runner)
return offenseHandler:runningSpeed(runner, self.state.ball) return offenseHandler:runningSpeed(runner, self.state.ball)
@ -584,7 +585,6 @@ function Game:update()
playdate.timer.updateTimers() playdate.timer.updateTimers()
gfx.animation.blinker.updateAll() gfx.animation.blinker.updateAll()
self:updateGameState() self:updateGameState()
local ball = self.state.ball
gfx.clear() gfx.clear()
gfx.setColor(gfx.kColorBlack) gfx.setColor(gfx.kColorBlack)
@ -601,6 +601,8 @@ function Game:update()
characterDraws[#characterDraws + 1] = { y = y, drawAction = drawAction } characterDraws[#characterDraws + 1] = { y = y, drawAction = drawAction }
end end
local ball = self.state.ball
local danceOffset = FielderDanceAnimator:currentValue() local danceOffset = FielderDanceAnimator:currentValue()
---@type Fielder | nil ---@type Fielder | nil
local ballHeldBy local ballHeldBy
@ -608,7 +610,7 @@ function Game:update()
addDraw(fielder.y + danceOffset, function() addDraw(fielder.y + danceOffset, function()
local ballHeldByThisFielder = drawFielder( local ballHeldByThisFielder = drawFielder(
self.state.fieldingTeamSprites[fielder.spriteIndex], self.state.fieldingTeamSprites[fielder.spriteIndex],
self.state.ball, ball,
fielder.x, fielder.x,
fielder.y + danceOffset fielder.y + danceOffset
) )