Refactor to centralize some secondsSince var usage
Prevent fielder dance as they take the field.
This commit is contained in:
parent
aa571dab4e
commit
a46bac174c
48
src/main.lua
48
src/main.lua
|
@ -55,9 +55,13 @@ local PlayerImageBlipper <const> = blipper.new(100, "images/game/player.png", "i
|
|||
|
||||
local DanceBounceMs <const> = 500
|
||||
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
|
||||
|
||||
function fieldersDance()
|
||||
FielderDanceAnimator:reset(DanceBounceMs)
|
||||
end
|
||||
|
||||
local BallOffscreen <const> = 999
|
||||
|
||||
local PitchFlyMs <const> = 2500
|
||||
|
@ -207,18 +211,16 @@ function throwBall(destX, destY, easingFunc, flyTimeMs, floaty)
|
|||
end
|
||||
end
|
||||
|
||||
local PitchAfterSeconds = 5
|
||||
-- TODO: Replace with a timer, repeatedly reset instead of setting to 0
|
||||
local secondsSincePitchAllowed = -5
|
||||
|
||||
function pitch()
|
||||
currentMode = Modes.batting
|
||||
ballAnimatorX = gfx.animator.new(0, PitchStartX, PitchStartX, playdate.easingFunctions.linear)
|
||||
ballAnimatorY = PitchAnimator
|
||||
PitchAnimator:reset()
|
||||
end
|
||||
|
||||
function playdate.AButtonDown()
|
||||
if not batter then
|
||||
return
|
||||
end
|
||||
pitch()
|
||||
secondsSincePitchAllowed = 0
|
||||
end
|
||||
|
||||
local elapsedSec = 0
|
||||
|
@ -260,7 +262,7 @@ function outRunner(runnerIndex)
|
|||
outs = outs + 1
|
||||
outRunners[#outRunners + 1] = runners[runnerIndex]
|
||||
table.remove(runners, runnerIndex)
|
||||
FielderDanceAnimator:reset()
|
||||
fieldersDance()
|
||||
updateForcedRunners()
|
||||
announcer:say("YOU'RE OUT!")
|
||||
if outs == 3 then
|
||||
|
@ -322,14 +324,10 @@ end
|
|||
|
||||
--- Returns true if at least one runner is still moving
|
||||
---@return boolean
|
||||
function updateRunners()
|
||||
function updateRunners(currentRunners)
|
||||
local autoRunSpeed = 20 * deltaSeconds
|
||||
--autoRunSpeed = 140
|
||||
-- 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
|
||||
for runnerIndex, runner in ipairs(currentRunners) do
|
||||
|
@ -436,11 +434,9 @@ function getNextThrowTarget()
|
|||
end
|
||||
|
||||
local ResetFieldersAfterSeconds = 4
|
||||
-- TODO: Replace with a timer, repeatedly reset instead of setting to 0
|
||||
local secondsSinceLastRunnerMove = 0
|
||||
|
||||
local PitchAfterSeconds = 5
|
||||
local secondsSincePitchAllowed = -5
|
||||
|
||||
function init()
|
||||
playdate.display.setRefreshRate(50)
|
||||
gfx.setBackgroundColor(gfx.kColorWhite)
|
||||
|
@ -464,11 +460,6 @@ function tryToThrowOut(self)
|
|||
end
|
||||
|
||||
function updateBatting()
|
||||
secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds
|
||||
if secondsSincePitchAllowed > PitchAfterSeconds then
|
||||
pitch()
|
||||
secondsSincePitchAllowed = 0
|
||||
end
|
||||
if ball.y < BallOffscreen then
|
||||
ball.y = ballAnimatorY:currentValue() + ballFloatAnimator:currentValue()
|
||||
ball.size = 6
|
||||
|
@ -514,9 +505,11 @@ function updateBatting()
|
|||
end
|
||||
|
||||
function updateRunning()
|
||||
secondsSincePitchAllowed = 0
|
||||
local nonBatterRunners = filter(runners, function (runner)
|
||||
return runner ~= batter
|
||||
end)
|
||||
ball.size = ballSizeAnimator:currentValue()
|
||||
if updateRunners() then
|
||||
if updateRunners(nonBatterRunners) then
|
||||
secondsSinceLastRunnerMove = 0
|
||||
else
|
||||
secondsSinceLastRunnerMove = secondsSinceLastRunnerMove + deltaSeconds
|
||||
|
@ -555,9 +548,14 @@ function updateGameState()
|
|||
end
|
||||
|
||||
if currentMode == Modes.batting then
|
||||
secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds
|
||||
if secondsSincePitchAllowed > PitchAfterSeconds then
|
||||
pitch()
|
||||
end
|
||||
updateBatting()
|
||||
updateRunners()
|
||||
updateRunners({ batter })
|
||||
elseif currentMode == Modes.running then
|
||||
secondsSincePitchAllowed = 0
|
||||
updateRunning()
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue