Extract draw/ball.lua
Tweak pointIsSquarelyAboveLine() -> pointIsAboveLine()
This commit is contained in:
parent
ce9a2d335e
commit
876f828117
|
@ -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
|
|
@ -145,7 +145,8 @@ end
|
||||||
|
|
||||||
local newStats = stats
|
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
|
if
|
||||||
newStats.homeScore ~= homeScore
|
newStats.homeScore ~= homeScore
|
||||||
or newStats.awayScore ~= awayScore
|
or newStats.awayScore ~= awayScore
|
||||||
|
|
|
@ -96,7 +96,7 @@ local function updateFielderPosition(deltaSeconds, fielder, ball)
|
||||||
local currentTarget = fielder.targets[#fielder.targets]
|
local currentTarget = fielder.targets[#fielder.targets]
|
||||||
local willMove = utils.moveAtSpeed(nextFielderPos, fielder.speed * deltaSeconds, currentTarget)
|
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
|
local targetCount = #fielder.targets
|
||||||
-- Back up a little
|
-- Back up a little
|
||||||
fielder.targets[targetCount + 2] = utils.xy(fielder.x, fielder.y + 5)
|
fielder.targets[targetCount + 2] = utils.xy(fielder.x, fielder.y + 5)
|
||||||
|
|
18
src/main.lua
18
src/main.lua
|
@ -48,6 +48,7 @@ import 'pitching.lua'
|
||||||
import 'statistics.lua'
|
import 'statistics.lua'
|
||||||
import 'user-input.lua'
|
import 'user-input.lua'
|
||||||
|
|
||||||
|
import 'draw/ball.lua'
|
||||||
import 'draw/box-score.lua'
|
import 'draw/box-score.lua'
|
||||||
import 'draw/fans.lua'
|
import 'draw/fans.lua'
|
||||||
import 'draw/characters.lua'
|
import 'draw/characters.lua'
|
||||||
|
@ -439,7 +440,7 @@ function Game:updateBatting(offenseHandler)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local isHomeRun = utils.pointIsSquarelyAboveLine(ballDest, C.OutfieldWall)
|
local isHomeRun = utils.pointIsAboveLine(ballDest, C.OutfieldWall)
|
||||||
if isHomeRun then
|
if isHomeRun then
|
||||||
playdate.timer.new(flyTimeMs, function()
|
playdate.timer.new(flyTimeMs, function()
|
||||||
-- Verify that the home run wasn't intercepted
|
-- Verify that the home run wasn't intercepted
|
||||||
|
@ -591,22 +592,14 @@ function Game:update()
|
||||||
fans.draw()
|
fans.draw()
|
||||||
GrassBackground:draw(-400, -720)
|
GrassBackground:draw(-400, -720)
|
||||||
|
|
||||||
local ball = state.ball
|
local ballHeldBy = self.characters:drawAll(self.fielding, self.baserunning, state, state.battingTeam, state.ball)
|
||||||
|
|
||||||
local ballHeldBy = self.characters:drawAll(self.fielding, self.baserunning, state, state.battingTeam, ball)
|
|
||||||
|
|
||||||
if self:userIsOn("defense") then
|
if self:userIsOn("defense") then
|
||||||
throwMeter:drawNearFielder(ballHeldBy)
|
throwMeter:drawNearFielder(ballHeldBy)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not ballHeldBy then
|
if not ballHeldBy then
|
||||||
gfx.setLineWidth(2)
|
state.ball:draw()
|
||||||
|
|
||||||
gfx.setColor(gfx.kColorWhite)
|
|
||||||
gfx.fillCircleAtPoint(ball.x, ball.y, ball.size)
|
|
||||||
|
|
||||||
gfx.setColor(gfx.kColorBlack)
|
|
||||||
gfx.drawCircleAtPoint(ball.x, ball.y, ball.size)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
gfx.setDrawOffset(0, 0)
|
gfx.setDrawOffset(0, 0)
|
||||||
|
@ -614,8 +607,7 @@ function Game:update()
|
||||||
drawMinimap(self.baserunning.runners, self.fielding.fielders)
|
drawMinimap(self.baserunning.runners, self.fielding.fielders)
|
||||||
end
|
end
|
||||||
|
|
||||||
local homeScore, awayScore = utils.totalScores(state.stats)
|
drawScoreboard(0, C.Screen.H * 0.77, state.stats, self.baserunning.outs, state.battingTeam, state.inning)
|
||||||
drawScoreboard(0, C.Screen.H * 0.77, homeScore, awayScore, self.baserunning.outs, state.battingTeam, state.inning)
|
|
||||||
drawBallsAndStrikes(290, C.Screen.H - 20, pitchTracker.balls, pitchTracker.strikes)
|
drawBallsAndStrikes(290, C.Screen.H - 20, pitchTracker.balls, pitchTracker.strikes)
|
||||||
self.announcer:draw(C.Center.x, 10)
|
self.announcer:draw(C.Center.x, 10)
|
||||||
|
|
||||||
|
|
|
@ -90,14 +90,6 @@ function utils.moveAtSpeed(mover, speed, target, tau)
|
||||||
return true
|
return true
|
||||||
end
|
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
|
---@generic T
|
||||||
---@param array T[]
|
---@param array T[]
|
||||||
---@param condition fun(T): boolean
|
---@param condition fun(T): boolean
|
||||||
|
@ -194,11 +186,15 @@ function utils.pointDirectlyUnderLine(point, line1, line2, bottomBound)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns true if the given point is anywhere above the given line, with no upper bound.
|
--- 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 point XyPair
|
||||||
---@param linePoints XyPair[]
|
---@param linePoints XyPair[]
|
||||||
---@return boolean
|
---@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
|
for i = 2, #linePoints do
|
||||||
local prev = linePoints[i - 1]
|
local prev = linePoints[i - 1]
|
||||||
local next = linePoints[i]
|
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)
|
return not utils.pointUnderLine(point.x, point.y, prev.x, prev.y, next.x, next.y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if point.x > linePoints[#linePoints].x and point.y < linePoints[#linePoints].y then
|
||||||
|
return true
|
||||||
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue