Make main.lua functions `local`, where possible
This commit is contained in:
parent
66bd97499a
commit
d85db79e52
56
src/main.lua
56
src/main.lua
|
@ -141,7 +141,7 @@ local Pitches <const> = {
|
|||
}
|
||||
|
||||
---@return boolean playerIsOnSide, boolean playerIsOnOtherSide
|
||||
function playerIsOn(side)
|
||||
local function playerIsOn(side)
|
||||
local ret
|
||||
if PlayerTeam == battingTeam then
|
||||
ret = side == C.Sides.offense
|
||||
|
@ -158,7 +158,7 @@ end
|
|||
---@param flyTimeMs number | nil
|
||||
---@param floaty boolean | nil
|
||||
---@param customBallScaler pd_animator | nil
|
||||
function throwBall(destX, destY, easingFunc, flyTimeMs, floaty, customBallScaler)
|
||||
local function throwBall(destX, destY, easingFunc, flyTimeMs, floaty, customBallScaler)
|
||||
ball.heldBy = nil
|
||||
throwMeter = 0
|
||||
|
||||
|
@ -181,7 +181,7 @@ end
|
|||
|
||||
---@param pitchFlyTimeMs number | nil
|
||||
---@param pitchTypeIndex number | nil
|
||||
function pitch(pitchFlyTimeMs, pitchTypeIndex)
|
||||
local function pitch(pitchFlyTimeMs, pitchTypeIndex)
|
||||
catcherThrownBall = false
|
||||
offenseMode = C.Offense.batting
|
||||
|
||||
|
@ -210,7 +210,7 @@ end
|
|||
---@param x number
|
||||
---@param y number
|
||||
---@return Base | nil
|
||||
function isTouchingBase(x, y)
|
||||
local function isTouchingBase(x, y)
|
||||
return utils.first(C.Bases, function(base)
|
||||
return utils.distanceBetween(x, y, base.x, base.y) < C.BaseHitbox
|
||||
end)
|
||||
|
@ -220,20 +220,20 @@ end
|
|||
---@param x number
|
||||
---@param y number
|
||||
---@return boolean, number
|
||||
function isTouchingBall(x, y)
|
||||
local function isTouchingBall(x, y)
|
||||
local ballDistance = utils.distanceBetween(x, y, ball.x, ball.y)
|
||||
return ballDistance < C.BallCatchHitbox, ballDistance
|
||||
end
|
||||
|
||||
---@param base Base
|
||||
---@return Runner | nil
|
||||
function getRunnerWithNextBase(base)
|
||||
local function getRunnerWithNextBase(base)
|
||||
return utils.first(runners, function(runner)
|
||||
return runner.nextBase == base
|
||||
end)
|
||||
end
|
||||
|
||||
function updateForcedRunners()
|
||||
local function updateForcedRunners()
|
||||
local stillForced = true
|
||||
for _, base in ipairs(C.Bases) do
|
||||
local runnerTargetingBase = getRunnerWithNextBase(base)
|
||||
|
@ -250,7 +250,7 @@ function updateForcedRunners()
|
|||
end
|
||||
|
||||
---@param runner integer | Runner
|
||||
function outRunner(runner, message)
|
||||
local function outRunner(runner, message)
|
||||
if type(runner) ~= "number" then
|
||||
for i, maybe in ipairs(runners) do
|
||||
if runner == maybe then
|
||||
|
@ -299,7 +299,7 @@ function outRunner(runner, message)
|
|||
end
|
||||
|
||||
---@param runnerIndex number
|
||||
function score(runnerIndex)
|
||||
local function score(runnerIndex)
|
||||
outRunners[#outRunners + 1] = runners[runnerIndex]
|
||||
table.remove(runners, runnerIndex)
|
||||
battingTeam.score = battingTeam.score + 1
|
||||
|
@ -307,7 +307,7 @@ function score(runnerIndex)
|
|||
end
|
||||
|
||||
---@return Base[]
|
||||
function getForcedOutTargets()
|
||||
local function getForcedOutTargets()
|
||||
local targets = {}
|
||||
for _, base in ipairs(C.Bases) do
|
||||
local runnerTargetingBase = getRunnerWithNextBase(base)
|
||||
|
@ -322,7 +322,7 @@ end
|
|||
|
||||
--- Returns the position,distance of the basest closest to the runner furthest from a base
|
||||
---@return Base | nil, number | nil
|
||||
function getBaseOfStrandedRunner()
|
||||
local function getBaseOfStrandedRunner()
|
||||
local farRunnersBase, farDistance
|
||||
for _, runner in pairs(runners) do
|
||||
if runner ~= batter then
|
||||
|
@ -339,7 +339,7 @@ end
|
|||
|
||||
--- Returns x,y of the out target
|
||||
---@return number|nil, number|nil
|
||||
function getNextOutTarget()
|
||||
local function getNextOutTarget()
|
||||
-- TODO: Handle missed throws, check for fielders at target, etc.
|
||||
local targets = getForcedOutTargets()
|
||||
if #targets ~= 0 then
|
||||
|
@ -353,7 +353,7 @@ function getNextOutTarget()
|
|||
end
|
||||
|
||||
---@param fielder Fielder
|
||||
function tryToMakeAnOut(fielder)
|
||||
local function tryToMakeAnOut(fielder)
|
||||
local targetX, targetY = getNextOutTarget()
|
||||
if targetX ~= nil and targetY ~= nil then
|
||||
local nearestFielder = utils.getNearestOf(Field.fielders, targetX, targetY)
|
||||
|
@ -366,7 +366,7 @@ function tryToMakeAnOut(fielder)
|
|||
end
|
||||
end
|
||||
|
||||
function readThrow()
|
||||
local function readThrow()
|
||||
if throwMeter > C.ThrowMeterMax then
|
||||
return (C.PitchFlyMs / (throwMeter / C.ThrowMeterMax))
|
||||
end
|
||||
|
@ -376,7 +376,7 @@ end
|
|||
---@param thrower Fielder
|
||||
---@param throwFlyMs number
|
||||
---@return boolean didThrow
|
||||
function buttonControlledThrow(thrower, throwFlyMs, forbidThrowHome)
|
||||
local function buttonControlledThrow(thrower, throwFlyMs, forbidThrowHome)
|
||||
local targetBase
|
||||
if playdate.buttonIsPressed(playdate.kButtonLeft) then
|
||||
targetBase = C.Bases[C.Third]
|
||||
|
@ -402,7 +402,7 @@ function buttonControlledThrow(thrower, throwFlyMs, forbidThrowHome)
|
|||
return true
|
||||
end
|
||||
|
||||
function outEligibleRunners(fielder)
|
||||
local function outEligibleRunners(fielder)
|
||||
local touchedBase = isTouchingBase(fielder.x, fielder.y)
|
||||
local didOutRunner = false
|
||||
for i, runner in pairs(runners) do
|
||||
|
@ -423,7 +423,7 @@ function outEligibleRunners(fielder)
|
|||
return didOutRunner
|
||||
end
|
||||
|
||||
function updateNpcFielder(fielder, outedSomeRunner)
|
||||
local function updateNpcFielder(fielder, outedSomeRunner)
|
||||
if offenseMode ~= C.Offense.running then
|
||||
return
|
||||
end
|
||||
|
@ -437,7 +437,7 @@ function updateNpcFielder(fielder, outedSomeRunner)
|
|||
end
|
||||
|
||||
---@param fielder Fielder
|
||||
function updateFielder(fielder)
|
||||
local function updateFielder(fielder)
|
||||
if fielder.target ~= nil then
|
||||
if not utils.moveAtSpeed(fielder, fielder.speed * deltaSeconds, fielder.target) then
|
||||
fielder.target = nil
|
||||
|
@ -465,7 +465,7 @@ end
|
|||
---@param runnerIndex integer | nil May only be nil if runner == batter
|
||||
---@param appliedSpeed number
|
||||
---@return boolean
|
||||
function updateRunner(runner, runnerIndex, appliedSpeed)
|
||||
local function updateRunner(runner, runnerIndex, appliedSpeed)
|
||||
local autoRunSpeed = 20 * deltaSeconds
|
||||
--autoRunSpeed = 140
|
||||
|
||||
|
@ -515,7 +515,7 @@ function updateRunner(runner, runnerIndex, appliedSpeed)
|
|||
return prevX ~= runner.x or prevY ~= runner.y
|
||||
end
|
||||
|
||||
function nextBatter()
|
||||
local function nextBatter()
|
||||
batter = nil
|
||||
playdate.timer.new(2000, function()
|
||||
pitchTracker:reset()
|
||||
|
@ -525,7 +525,7 @@ function nextBatter()
|
|||
end)
|
||||
end
|
||||
|
||||
function walk()
|
||||
local function walk()
|
||||
announcer:say("Walk!")
|
||||
Field.fielders.first.target = C.Bases[C.First]
|
||||
batter.nextBase = C.Bases[C.First]
|
||||
|
@ -536,7 +536,7 @@ function walk()
|
|||
nextBatter()
|
||||
end
|
||||
|
||||
function strikeOut()
|
||||
local function strikeOut()
|
||||
local outBatter = batter
|
||||
batter = nil
|
||||
outRunner(outBatter --[[@as Runner]], "Strike out!")
|
||||
|
@ -544,7 +544,7 @@ function strikeOut()
|
|||
end
|
||||
|
||||
---@param batDeg number
|
||||
function updateBatting(batDeg, batSpeed)
|
||||
local function updateBatting(batDeg, batSpeed)
|
||||
if ball.y < C.BallOffscreen then
|
||||
ball.y = ballAnimatorY:currentValue() + ballFloatAnimator:currentValue()
|
||||
ball.size = C.SmallestBallRadius -- ballFloatAnimator:currentValue()
|
||||
|
@ -595,7 +595,7 @@ end
|
|||
--- Returns true only if at least one of the given runners moved during this update
|
||||
---@param appliedSpeed number
|
||||
---@return boolean
|
||||
function updateRunning(appliedSpeed, forcedOnly)
|
||||
local function updateRunning(appliedSpeed, forcedOnly)
|
||||
ball.size = ballSizeAnimator:currentValue()
|
||||
|
||||
local runnerMoved = false
|
||||
|
@ -610,7 +610,7 @@ function updateRunning(appliedSpeed, forcedOnly)
|
|||
return runnerMoved
|
||||
end
|
||||
|
||||
function walkAwayOutRunners()
|
||||
local function walkAwayOutRunners()
|
||||
for i, runner in ipairs(outRunners) do
|
||||
if runner.x < C.Screen.W + 50 and runner.y < C.Screen.H + 50 then
|
||||
runner.x = runner.x + (deltaSeconds * 25)
|
||||
|
@ -621,7 +621,7 @@ function walkAwayOutRunners()
|
|||
end
|
||||
end
|
||||
|
||||
function playerPitch(throwFly)
|
||||
local function playerPitch(throwFly)
|
||||
local aButton = playdate.buttonIsPressed(playdate.kButtonA)
|
||||
local bButton = playdate.buttonIsPressed(playdate.kButtonB)
|
||||
if not aButton and not bButton then
|
||||
|
@ -635,7 +635,7 @@ function playerPitch(throwFly)
|
|||
end
|
||||
end
|
||||
|
||||
function updateGameState()
|
||||
local function updateGameState()
|
||||
deltaSeconds = playdate.getElapsedTime() or 0
|
||||
playdate.resetElapsedTime()
|
||||
local crankChange = playdate.getCrankChange() --[[@as number]]
|
||||
|
@ -802,7 +802,7 @@ function playdate.update()
|
|||
announcer:draw(C.Center.x, 10)
|
||||
end
|
||||
|
||||
function init()
|
||||
local function init()
|
||||
playdate.display.setRefreshRate(50)
|
||||
gfx.setBackgroundColor(gfx.kColorWhite)
|
||||
playdate.setMenuImage(gfx.image.new("images/game/menu-image.png"))
|
||||
|
|
Loading…
Reference in New Issue