Refactor to centralize some secondsSince var usage

Prevent fielder dance as they take the field.
This commit is contained in:
Sage Vaillancourt 2025-02-02 20:20:53 -05:00
parent aa571dab4e
commit a46bac174c
1 changed files with 23 additions and 25 deletions

View File

@ -55,9 +55,13 @@ local PlayerImageBlipper <const> = blipper.new(100, "images/game/player.png", "i
local DanceBounceMs <const> = 500 local DanceBounceMs <const> = 500
local DanceBounceCount <const> = 4 local DanceBounceCount <const> = 4
local FielderDanceAnimator <const> = gfx.animator.new(DanceBounceMs, 10, 0, easingHill) local FielderDanceAnimator <const> = gfx.animator.new(1, 10, 0, easingHill)
FielderDanceAnimator.repeatCount = DanceBounceCount - 1 FielderDanceAnimator.repeatCount = DanceBounceCount - 1
function fieldersDance()
FielderDanceAnimator:reset(DanceBounceMs)
end
local BallOffscreen <const> = 999 local BallOffscreen <const> = 999
local PitchFlyMs <const> = 2500 local PitchFlyMs <const> = 2500
@ -207,18 +211,16 @@ function throwBall(destX, destY, easingFunc, flyTimeMs, floaty)
end end
end end
local PitchAfterSeconds = 5
-- TODO: Replace with a timer, repeatedly reset instead of setting to 0
local secondsSincePitchAllowed = -5
function pitch() function pitch()
currentMode = Modes.batting currentMode = Modes.batting
ballAnimatorX = gfx.animator.new(0, PitchStartX, PitchStartX, playdate.easingFunctions.linear) ballAnimatorX = gfx.animator.new(0, PitchStartX, PitchStartX, playdate.easingFunctions.linear)
ballAnimatorY = PitchAnimator ballAnimatorY = PitchAnimator
PitchAnimator:reset() PitchAnimator:reset()
end secondsSincePitchAllowed = 0
function playdate.AButtonDown()
if not batter then
return
end
pitch()
end end
local elapsedSec = 0 local elapsedSec = 0
@ -260,7 +262,7 @@ function outRunner(runnerIndex)
outs = outs + 1 outs = outs + 1
outRunners[#outRunners + 1] = runners[runnerIndex] outRunners[#outRunners + 1] = runners[runnerIndex]
table.remove(runners, runnerIndex) table.remove(runners, runnerIndex)
FielderDanceAnimator:reset() fieldersDance()
updateForcedRunners() updateForcedRunners()
announcer:say("YOU'RE OUT!") announcer:say("YOU'RE OUT!")
if outs == 3 then if outs == 3 then
@ -322,14 +324,10 @@ end
--- Returns true if at least one runner is still moving --- Returns true if at least one runner is still moving
---@return boolean ---@return boolean
function updateRunners() function updateRunners(currentRunners)
local autoRunSpeed = 20 * deltaSeconds local autoRunSpeed = 20 * deltaSeconds
--autoRunSpeed = 140 --autoRunSpeed = 140
-- TODO: Filter for the runner closest to the currently-held direction button -- TODO: Filter for the runner closest to the currently-held direction button
local currentRunners = currentMode == Modes.batting and { batter }
or filter(runners, function(runner)
return runner ~= batter
end)
local runnerMoved = false local runnerMoved = false
for runnerIndex, runner in ipairs(currentRunners) do for runnerIndex, runner in ipairs(currentRunners) do
@ -436,11 +434,9 @@ function getNextThrowTarget()
end end
local ResetFieldersAfterSeconds = 4 local ResetFieldersAfterSeconds = 4
-- TODO: Replace with a timer, repeatedly reset instead of setting to 0
local secondsSinceLastRunnerMove = 0 local secondsSinceLastRunnerMove = 0
local PitchAfterSeconds = 5
local secondsSincePitchAllowed = -5
function init() function init()
playdate.display.setRefreshRate(50) playdate.display.setRefreshRate(50)
gfx.setBackgroundColor(gfx.kColorWhite) gfx.setBackgroundColor(gfx.kColorWhite)
@ -464,11 +460,6 @@ function tryToThrowOut(self)
end end
function updateBatting() function updateBatting()
secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds
if secondsSincePitchAllowed > PitchAfterSeconds then
pitch()
secondsSincePitchAllowed = 0
end
if ball.y < BallOffscreen then if ball.y < BallOffscreen then
ball.y = ballAnimatorY:currentValue() + ballFloatAnimator:currentValue() ball.y = ballAnimatorY:currentValue() + ballFloatAnimator:currentValue()
ball.size = 6 ball.size = 6
@ -514,9 +505,11 @@ function updateBatting()
end end
function updateRunning() function updateRunning()
secondsSincePitchAllowed = 0 local nonBatterRunners = filter(runners, function (runner)
return runner ~= batter
end)
ball.size = ballSizeAnimator:currentValue() ball.size = ballSizeAnimator:currentValue()
if updateRunners() then if updateRunners(nonBatterRunners) then
secondsSinceLastRunnerMove = 0 secondsSinceLastRunnerMove = 0
else else
secondsSinceLastRunnerMove = secondsSinceLastRunnerMove + deltaSeconds secondsSinceLastRunnerMove = secondsSinceLastRunnerMove + deltaSeconds
@ -555,9 +548,14 @@ function updateGameState()
end end
if currentMode == Modes.batting then if currentMode == Modes.batting then
secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds
if secondsSincePitchAllowed > PitchAfterSeconds then
pitch()
end
updateBatting() updateBatting()
updateRunners() updateRunners({ batter })
elseif currentMode == Modes.running then elseif currentMode == Modes.running then
secondsSincePitchAllowed = 0
updateRunning() updateRunning()
end end