diff --git a/src/draw/ball.lua b/src/draw/ball.lua new file mode 100644 index 0000000..07518a4 --- /dev/null +++ b/src/draw/ball.lua @@ -0,0 +1,11 @@ +local gfx = 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 diff --git a/src/draw/overlay.lua b/src/draw/overlay.lua index a510e60..3b8cd23 100644 --- a/src/draw/overlay.lua +++ b/src/draw/overlay.lua @@ -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 diff --git a/src/fielding.lua b/src/fielding.lua index c892415..234dec4 100644 --- a/src/fielding.lua +++ b/src/fielding.lua @@ -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) diff --git a/src/main.lua b/src/main.lua index 896499e..26b0ba3 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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) diff --git a/src/utils.lua b/src/utils.lua index ba0643c..f79486a 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -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