Extract Game:updatePitching()
Also, pull more from updateGameState() into updateBatting()
This commit is contained in:
parent
b928ee3658
commit
7c7b5ff762
68
src/main.lua
68
src/main.lua
|
@ -381,8 +381,11 @@ end
|
|||
local SwingBackDeg <const> = 30
|
||||
local SwingForwardDeg <const> = 170
|
||||
|
||||
---@param batDeg number
|
||||
function Game:updateBatting(batDeg, batSpeed)
|
||||
---@param offenseHandler InputHandler
|
||||
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
|
||||
self.state.didSwing = true
|
||||
end
|
||||
|
@ -468,7 +471,6 @@ function Game:updateBatting(batDeg, batSpeed)
|
|||
end
|
||||
|
||||
---@param appliedSpeed number | fun(runner: Runner): number
|
||||
---
|
||||
---@return boolean runnersStillMoving, number secondsSinceLastRunnerMove
|
||||
function Game:updateNonBatterRunners(appliedSpeed, forcedOnly, isAutoRun)
|
||||
local runnersStillMoving, runnersScored, secondsSinceLastRunnerMove =
|
||||
|
@ -497,6 +499,31 @@ function Game:returnToPitcher()
|
|||
end)
|
||||
end
|
||||
|
||||
---@param defenseHandler InputHandler
|
||||
function Game:updatePitching(defenseHandler)
|
||||
pitchTracker:recordIfPassed(self.state.ball)
|
||||
|
||||
if self:pitcherIsOnTheMound() then
|
||||
pitchTracker.secondsSinceLastPitch = pitchTracker.secondsSinceLastPitch + self.state.deltaSeconds
|
||||
end
|
||||
|
||||
if pitchTracker.secondsSinceLastPitch > C.ReturnToPitcherAfterSeconds and not self.state.pitchIsOver then
|
||||
local outcome = pitchTracker:updatePitchCounts(self.state.didSwing, self:fieldingTeamCurrentInning())
|
||||
if outcome == PitchOutcomes.StrikeOut then
|
||||
self:strikeOut()
|
||||
elseif outcome == PitchOutcomes.Walk then
|
||||
self:walk()
|
||||
end
|
||||
self:returnToPitcher()
|
||||
self.state.pitchIsOver = true
|
||||
self.state.didSwing = false
|
||||
end
|
||||
|
||||
if pitchTracker.secondsSinceLastPitch > C.PitchAfterSeconds then
|
||||
defenseHandler:pitch()
|
||||
end
|
||||
end
|
||||
|
||||
function Game:updateGameState()
|
||||
self.state.deltaSeconds = playdate.getElapsedTime() or 0
|
||||
playdate.resetElapsedTime()
|
||||
|
@ -508,37 +535,11 @@ function Game:updateGameState()
|
|||
local fielderHoldingBall = self.fielding:updateFielderPositions(self.state.ball, self.state.deltaSeconds)
|
||||
|
||||
if self.state.offenseState == C.Offense.batting then
|
||||
pitchTracker:recordIfPassed(self.state.ball)
|
||||
|
||||
local pitcher = self.fielding.fielders.pitcher
|
||||
if utils.distanceBetween(pitcher.x, pitcher.y, C.PitcherStartPos.x, C.PitcherStartPos.y) < C.BaseHitbox then
|
||||
pitchTracker.secondsSinceLastPitch = pitchTracker.secondsSinceLastPitch + self.state.deltaSeconds
|
||||
end
|
||||
|
||||
if pitchTracker.secondsSinceLastPitch > C.ReturnToPitcherAfterSeconds and not self.state.pitchIsOver then
|
||||
local outcome = pitchTracker:updatePitchCounts(self.state.didSwing, self:fieldingTeamCurrentInning())
|
||||
if outcome == PitchOutcomes.StrikeOut then
|
||||
self:strikeOut()
|
||||
elseif outcome == PitchOutcomes.Walk then
|
||||
self:walk()
|
||||
end
|
||||
self:returnToPitcher()
|
||||
self.state.pitchIsOver = true
|
||||
self.state.didSwing = false
|
||||
end
|
||||
|
||||
local batSpeed
|
||||
self.state.batAngleDeg, batSpeed =
|
||||
offenseHandler:updateBat(self.state.ball, self.state.pitchIsOver, self.state.deltaSeconds)
|
||||
|
||||
self:updateBatting(self.state.batAngleDeg, batSpeed)
|
||||
self:updatePitching(defenseHandler)
|
||||
self:updateBatting(offenseHandler)
|
||||
|
||||
-- Walk batter to the plate
|
||||
self.baserunning:updateRunner(self.baserunning.batter, nil, 0, false, self.state.deltaSeconds)
|
||||
|
||||
if pitchTracker.secondsSinceLastPitch > C.PitchAfterSeconds then
|
||||
defenseHandler:pitch()
|
||||
end
|
||||
elseif self.state.offenseState == C.Offense.running then
|
||||
local appliedSpeed = function(runner)
|
||||
return offenseHandler:runningSpeed(runner, self.state.ball)
|
||||
|
@ -584,7 +585,6 @@ function Game:update()
|
|||
playdate.timer.updateTimers()
|
||||
gfx.animation.blinker.updateAll()
|
||||
self:updateGameState()
|
||||
local ball = self.state.ball
|
||||
|
||||
gfx.clear()
|
||||
gfx.setColor(gfx.kColorBlack)
|
||||
|
@ -601,6 +601,8 @@ function Game:update()
|
|||
characterDraws[#characterDraws + 1] = { y = y, drawAction = drawAction }
|
||||
end
|
||||
|
||||
local ball = self.state.ball
|
||||
|
||||
local danceOffset = FielderDanceAnimator:currentValue()
|
||||
---@type Fielder | nil
|
||||
local ballHeldBy
|
||||
|
@ -608,7 +610,7 @@ function Game:update()
|
|||
addDraw(fielder.y + danceOffset, function()
|
||||
local ballHeldByThisFielder = drawFielder(
|
||||
self.state.fieldingTeamSprites[fielder.spriteIndex],
|
||||
self.state.ball,
|
||||
ball,
|
||||
fielder.x,
|
||||
fielder.y + danceOffset
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue