Extract draw/ball.lua

Tweak pointIsSquarelyAboveLine() -> pointIsAboveLine()
This commit is contained in:
Sage Vaillancourt 2025-02-26 14:20:36 -05:00
parent ce9a2d335e
commit 876f828117
5 changed files with 28 additions and 25 deletions

11
src/draw/ball.lua Normal file
View File

@ -0,0 +1,11 @@
local gfx <const> = playdate.graphics
function Ball:draw()
gfx.setLineWidth(2)
gfx.setColor(gfx.kColorWhite)
gfx.fillCircleAtPoint(self.x, self.y, self.size)
gfx.setColor(gfx.kColorBlack)
gfx.drawCircleAtPoint(self.x, self.y, self.size)
end

View File

@ -145,7 +145,8 @@ end
local newStats = stats
function drawScoreboard(x, y, homeScore, awayScore, outs, battingTeam, inning)
function drawScoreboard(x, y, statistics, outs, battingTeam, inning)
local homeScore, awayScore = utils.totalScores(statistics)
if
newStats.homeScore ~= homeScore
or newStats.awayScore ~= awayScore

View File

@ -96,7 +96,7 @@ local function updateFielderPosition(deltaSeconds, fielder, ball)
local currentTarget = fielder.targets[#fielder.targets]
local willMove = utils.moveAtSpeed(nextFielderPos, fielder.speed * deltaSeconds, currentTarget)
if willMove and utils.pointIsSquarelyAboveLine(nextFielderPos, C.BottomOfOutfieldWall) then
if willMove and utils.pointIsAboveLine(nextFielderPos, C.BottomOfOutfieldWall) then
local targetCount = #fielder.targets
-- Back up a little
fielder.targets[targetCount + 2] = utils.xy(fielder.x, fielder.y + 5)

View File

@ -48,6 +48,7 @@ import 'pitching.lua'
import 'statistics.lua'
import 'user-input.lua'
import 'draw/ball.lua'
import 'draw/box-score.lua'
import 'draw/fans.lua'
import 'draw/characters.lua'
@ -439,7 +440,7 @@ function Game:updateBatting(offenseHandler)
return
end
local isHomeRun = utils.pointIsSquarelyAboveLine(ballDest, C.OutfieldWall)
local isHomeRun = utils.pointIsAboveLine(ballDest, C.OutfieldWall)
if isHomeRun then
playdate.timer.new(flyTimeMs, function()
-- Verify that the home run wasn't intercepted
@ -591,22 +592,14 @@ function Game:update()
fans.draw()
GrassBackground:draw(-400, -720)
local ball = state.ball
local ballHeldBy = self.characters:drawAll(self.fielding, self.baserunning, state, state.battingTeam, ball)
local ballHeldBy = self.characters:drawAll(self.fielding, self.baserunning, state, state.battingTeam, state.ball)
if self:userIsOn("defense") then
throwMeter:drawNearFielder(ballHeldBy)
end
if not ballHeldBy then
gfx.setLineWidth(2)
gfx.setColor(gfx.kColorWhite)
gfx.fillCircleAtPoint(ball.x, ball.y, ball.size)
gfx.setColor(gfx.kColorBlack)
gfx.drawCircleAtPoint(ball.x, ball.y, ball.size)
state.ball:draw()
end
gfx.setDrawOffset(0, 0)
@ -614,8 +607,7 @@ function Game:update()
drawMinimap(self.baserunning.runners, self.fielding.fielders)
end
local homeScore, awayScore = utils.totalScores(state.stats)
drawScoreboard(0, C.Screen.H * 0.77, homeScore, awayScore, self.baserunning.outs, state.battingTeam, state.inning)
drawScoreboard(0, C.Screen.H * 0.77, state.stats, self.baserunning.outs, state.battingTeam, state.inning)
drawBallsAndStrikes(290, C.Screen.H - 20, pitchTracker.balls, pitchTracker.strikes)
self.announcer:draw(C.Center.x, 10)

View File

@ -90,14 +90,6 @@ function utils.moveAtSpeed(mover, speed, target, tau)
return true
end
---@param acceptableGap number
---@param n1 number
---@param n2 number
---@return boolean n1 is within acceptableGap of n2
function utils.within(acceptableGap, n1, n2)
return math.abs(n1 - n2) < acceptableGap
end
---@generic T
---@param array T[]
---@param condition fun(T): boolean
@ -194,11 +186,15 @@ function utils.pointDirectlyUnderLine(point, line1, line2, bottomBound)
end
--- Returns true if the given point is anywhere above the given line, with no upper bound.
--- This, if used for home run calculations, would not take into account balls that curve around the foul poles.
--- This, used for home run calculations, does not *precesely* take into account balls that curve around the foul poles.
--- If left of first linePoint and above it, returns true. Similarly if right of the last linePoint.
---@param point XyPair
---@param linePoints XyPair[]
---@return boolean
function utils.pointIsSquarelyAboveLine(point, linePoints)
function utils.pointIsAboveLine(point, linePoints)
if point.x < linePoints[1].x and point.y < linePoints[1].y then
return true
end
for i = 2, #linePoints do
local prev = linePoints[i - 1]
local next = linePoints[i]
@ -206,6 +202,9 @@ function utils.pointIsSquarelyAboveLine(point, linePoints)
return not utils.pointUnderLine(point.x, point.y, prev.x, prev.y, next.x, next.y)
end
end
if point.x > linePoints[#linePoints].x and point.y < linePoints[#linePoints].y then
return true
end
return false
end