Fix inning switching.

Add onThirdOut() as a baserunning dependency instead of relying on a wrapper function in main
Reset pitchTracker on side-switch
This commit is contained in:
Sage Vaillancourt 2025-02-10 12:47:24 -05:00
parent fc4e12eddd
commit 534a16ad67
2 changed files with 16 additions and 15 deletions

View File

@ -14,11 +14,12 @@
---@field batter Runner | nil ---@field batter Runner | nil
---@field outs number ---@field outs number
---@field announcer Announcer ---@field announcer Announcer
---@field onThirdOut fun()
Baserunning = {} Baserunning = {}
---@param announcer any ---@param announcer any
---@return Baserunning ---@return Baserunning
function Baserunning.new(announcer) function Baserunning.new(announcer, onThirdOut)
local o = setmetatable({ local o = setmetatable({
runners = {}, runners = {},
outRunners = {}, outRunners = {},
@ -28,6 +29,7 @@ function Baserunning.new(announcer)
--- it seems sensible to store the value here. --- it seems sensible to store the value here.
outs = 0, outs = 0,
announcer = announcer, announcer = announcer,
onThirdOut = onThirdOut,
}, { __index = Baserunning }) }, { __index = Baserunning })
o.batter = o:newRunner() o.batter = o:newRunner()
@ -37,7 +39,7 @@ end
---@param runner integer | Runner ---@param runner integer | Runner
---@param message string | nil ---@param message string | nil
---@return boolean wasFinalOut ---@return boolean wasThirdOut
function Baserunning:outRunner(runner, message) function Baserunning:outRunner(runner, message)
self.outs = self.outs + 1 self.outs = self.outs + 1
if type(runner) ~= "number" then if type(runner) ~= "number" then
@ -59,6 +61,7 @@ function Baserunning:outRunner(runner, message)
if self.outs < 3 then if self.outs < 3 then
return false return false
end end
self.onThirdOut()
self.outs = 0 self.outs = 0
-- TODO: outRunners/scoredRunners split -- TODO: outRunners/scoredRunners split
@ -69,6 +72,8 @@ function Baserunning:outRunner(runner, message)
return true return true
end end
---@param fielder Fielder
---@return boolean outedSomeRunner
function Baserunning:outEligibleRunners(fielder) function Baserunning:outEligibleRunners(fielder)
local touchedBase = utils.isTouchingBase(fielder.x, fielder.y) local touchedBase = utils.isTouchingBase(fielder.x, fielder.y)
local didOutRunner = false local didOutRunner = false
@ -82,8 +87,8 @@ function Baserunning:outEligibleRunners(fielder)
-- Tag out -- Tag out
or not runnerOnBase and utils.distanceBetween(runner.x, runner.y, fielder.x, fielder.y) < C.TagDistance or not runnerOnBase and utils.distanceBetween(runner.x, runner.y, fielder.x, fielder.y) < C.TagDistance
then then
local wasFinalOut = self:outRunner(i) local wasThirdOut = self:outRunner(i)
if wasFinalOut then if wasThirdOut then
return true -- Don't keep running up self.outs after it's been reset return true -- Don't keep running up self.outs after it's been reset
end end
didOutRunner = true didOutRunner = true
@ -171,6 +176,7 @@ function Baserunning:updateRunner(runner, runnerIndex, appliedSpeed, deltaSecond
local nb = runner.nextBase local nb = runner.nextBase
local x, y, distance = utils.normalizeVector(runner.x, runner.y, nb.x, nb.y) local x, y, distance = utils.normalizeVector(runner.x, runner.y, nb.x, nb.y)
-- TODO: Do a better job drifting runners toward their bases when appliedSpeed is low/zero
if distance < 2 then if distance < 2 then
runner.nextBase = C.NextBaseMap[runner.nextBase] runner.nextBase = C.NextBaseMap[runner.nextBase]
runner.forcedTo = nil runner.forcedTo = nil

View File

@ -40,9 +40,7 @@ import 'draw/fielder.lua'
local gfx <const>, C <const> = playdate.graphics, C local gfx <const>, C <const> = playdate.graphics, C
local announcer = Announcer.new() local announcer = Announcer.new()
local baserunning = Baserunning.new(announcer)
local fielding = Fielding.new() local fielding = Fielding.new()
local npc = Npc.new(baserunning.runners, fielding.fielders)
---@alias SimpleAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) } ---@alias SimpleAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) }
@ -170,14 +168,8 @@ local function pitch(pitchFlyTimeMs, pitchTypeIndex)
secondsSincePitchAllowed = 0 secondsSincePitchAllowed = 0
end end
---@param runner integer | Runner local function nextHalfInning()
---@param message string | nil pitchTracker:reset()
local function outRunner(runner, message)
local wasFinalOut = baserunning:outRunner(runner, message)
if not wasFinalOut then
return
end
local currentlyFieldingTeam = battingTeam == teams.home and teams.away or teams.home local currentlyFieldingTeam = battingTeam == teams.home and teams.away or teams.home
local gameOver = inning == 9 and teams.away.score ~= teams.home.score local gameOver = inning == 9 and teams.away.score ~= teams.home.score
if not gameOver then if not gameOver then
@ -202,6 +194,9 @@ local function outRunner(runner, message)
end) end)
end end
local baserunning = Baserunning.new(announcer, nextHalfInning)
local npc = Npc.new(baserunning.runners, fielding.fielders)
---@param scoredRunCount number ---@param scoredRunCount number
local function score(scoredRunCount) local function score(scoredRunCount)
battingTeam.score = battingTeam.score + scoredRunCount battingTeam.score = battingTeam.score + scoredRunCount
@ -264,7 +259,7 @@ end
local function strikeOut() local function strikeOut()
local outBatter = baserunning.batter local outBatter = baserunning.batter
baserunning.batter = nil baserunning.batter = nil
outRunner(outBatter --[[@as Runner]], "Strike out!") baserunning:outRunner(outBatter --[[@as Runner]], "Strike out!")
nextBatter() nextBatter()
end end