batter -> baserunning.batter

This commit is contained in:
Sage Vaillancourt 2025-02-09 10:12:56 -05:00
parent 1a68521bd4
commit c56cae6527
3 changed files with 23 additions and 24 deletions

View File

@ -117,6 +117,8 @@ function baserunning.newRunner(self)
return new return new
end end
baserunning.batter = baserunning:newRunner()
---@param self table ---@param self table
---@param runnerIndex integer ---@param runnerIndex integer
function baserunning.runnerScored(self, runnerIndex) function baserunning.runnerScored(self, runnerIndex)

View File

@ -71,8 +71,9 @@ C.SmallestBallRadius = 6
C.BatLength = 50 C.BatLength = 50
---@alias OffenseState table -- TODO: enums implemented this way are probably going to be difficult to serialize!
---@alias OffenseState table
--- An enum for what state the offense is in --- An enum for what state the offense is in
---@type table<string, OffenseState> ---@type table<string, OffenseState>
C.Offense = { C.Offense = {
@ -82,9 +83,8 @@ C.Offense = {
} }
---@alias Side table ---@alias Side table
---@type table<string, Side>
--- An enum for which side (offense or defense) a team is on. --- An enum for which side (offense or defense) a team is on.
---@type table<string, Side>
C.Sides = { C.Sides = {
offense = {}, offense = {},
defense = {}, defense = {},

View File

@ -87,9 +87,6 @@ local outs = 0
local inning = 1 local inning = 1
local offenseState = C.Offense.batting local offenseState = C.Offense.batting
---@type Runner | nil
local batter = baserunning:newRunner()
local throwMeter = 0 local throwMeter = 0
-- TODO: Replace with a timer, repeatedly reset, instead of setting to 0 -- TODO: Replace with a timer, repeatedly reset, instead of setting to 0
@ -268,28 +265,28 @@ local function buttonControlledThrow(throwFlyMs, forbidThrowHome)
end end
local function nextBatter() local function nextBatter()
batter = nil baserunning.batter = nil
playdate.timer.new(2000, function() playdate.timer.new(2000, function()
pitchTracker:reset() pitchTracker:reset()
if not batter then if not baserunning.batter then
batter = baserunning:newRunner() baserunning.batter = baserunning:newRunner()
end end
end) end)
end end
local function walk() local function walk()
announcer:say("Walk!") announcer:say("Walk!")
batter.nextBase = C.Bases[C.First] baserunning.batter.nextBase = C.Bases[C.First]
batter.prevBase = C.Bases[C.Home] baserunning.batter.prevBase = C.Bases[C.Home]
offenseState = C.Offense.walking offenseState = C.Offense.walking
batter = nil baserunning.batter = nil
baserunning:updateForcedRunners() baserunning:updateForcedRunners()
nextBatter() nextBatter()
end end
local function strikeOut() local function strikeOut()
local outBatter = batter local outBatter = baserunning.batter
batter = nil baserunning.batter = nil
outRunner(outBatter --[[@as Runner]], "Strike out!") outRunner(outBatter --[[@as Runner]], "Strike out!")
nextBatter() nextBatter()
end end
@ -298,8 +295,8 @@ end
local function updateBatting(batDeg, batSpeed) local function updateBatting(batDeg, batSpeed)
local batAngle = math.rad(batDeg) local batAngle = math.rad(batDeg)
-- TODO: animate bat-flip or something -- TODO: animate bat-flip or something
batBase.x = batter and (batter.x + BatterHandPos.x) or 0 batBase.x = baserunning.batter and (baserunning.batter.x + BatterHandPos.x) or 0
batBase.y = batter and (batter.y + BatterHandPos.y) or 0 batBase.y = baserunning.batter and (baserunning.batter.y + BatterHandPos.y) or 0
batTip.x = batBase.x + (C.BatLength * math.sin(batAngle)) batTip.x = batBase.x + (C.BatLength * math.sin(batAngle))
batTip.y = batBase.y + (C.BatLength * math.cos(batAngle)) batTip.y = batBase.y + (C.BatLength * math.cos(batAngle))
@ -325,11 +322,11 @@ local function updateBatting(batDeg, batSpeed)
local hitBallScaler = gfx.animator.new(2000, 9 + (mult * mult * 0.5), C.SmallestBallRadius, utils.easingHill) local hitBallScaler = gfx.animator.new(2000, 9 + (mult * mult * 0.5), C.SmallestBallRadius, utils.easingHill)
throwBall(ballDestX, ballDestY, playdate.easingFunctions.outQuint, 2000, nil, hitBallScaler) throwBall(ballDestX, ballDestY, playdate.easingFunctions.outQuint, 2000, nil, hitBallScaler)
batter.nextBase = C.Bases[C.First] baserunning.batter.nextBase = C.Bases[C.First]
batter.prevBase = C.Bases[C.Home] baserunning.batter.prevBase = C.Bases[C.Home]
baserunning:updateForcedRunners() baserunning:updateForcedRunners()
batter.forcedTo = C.Bases[C.First] baserunning.batter.forcedTo = C.Bases[C.First]
batter = nil -- Demote batter to a mere runner baserunning.batter = nil -- Demote batter to a mere runner
Field:haveSomeoneChase(ballDestX, ballDestY) Field:haveSomeoneChase(ballDestX, ballDestY)
end end
@ -422,7 +419,7 @@ local function updateGameState()
-- Walk batter to the plate -- Walk batter to the plate
-- TODO: Ensure batter can't be nil, here -- TODO: Ensure batter can't be nil, here
baserunning:updateRunner(batter, nil, crankLimited, deltaSeconds) baserunning:updateRunner(baserunning.batter, nil, crankLimited, deltaSeconds)
if secondsSincePitchAllowed > C.PitchAfterSeconds then if secondsSincePitchAllowed > C.PitchAfterSeconds then
if playerOnDefense then if playerOnDefense then
@ -444,8 +441,8 @@ local function updateGameState()
throwBall(C.PitchStartX, C.PitchStartY, playdate.easingFunctions.linear, nil, true) throwBall(C.PitchStartX, C.PitchStartY, playdate.easingFunctions.linear, nil, true)
Field:resetFielderPositions() Field:resetFielderPositions()
offenseState = C.Offense.batting offenseState = C.Offense.batting
if not batter then if not baserunning.batter then
batter = baserunning:newRunner() baserunning.batter = baserunning:newRunner()
end end
end end
end end
@ -504,7 +501,7 @@ function playdate.update()
-- TODO? Scale sprites down as y increases -- TODO? Scale sprites down as y increases
for _, runner in pairs(baserunning.runners) do for _, runner in pairs(baserunning.runners) do
if runner == batter then if runner == baserunning.batter then
if batAngleDeg > 50 and batAngleDeg < 200 then if batAngleDeg > 50 and batAngleDeg < 200 then
PlayerBack:draw(runner.x, runner.y) PlayerBack:draw(runner.x, runner.y)
else else