Extract runner's onArrive to a dedicated function.

This commit is contained in:
Sage Vaillancourt 2025-02-02 18:30:35 -05:00
parent 5fca82bb3a
commit aa571dab4e
2 changed files with 44 additions and 28 deletions

View File

@ -7,10 +7,31 @@ import 'CoreLibs/object.lua'
import 'CoreLibs/timer.lua' import 'CoreLibs/timer.lua'
import 'CoreLibs/ui.lua' import 'CoreLibs/ui.lua'
--- @alias XYPair { x: number, y: number } --- @alias XYPair {
--- @alias Base { x: number, y: number } --- x: number,
--- @alias Runner { x: number, y: number, nextBase: Base, prevBase: Base | nil, forcedTo: Base } --- y: number,
--- @alias Fielder { onArrive: fun() | nil, x: number | nil, y: number | nil, target: XYPair | nil, speed: number } --- }
--- @alias Base {
--- x: number,
--- y: number,
--- }
--- @alias Runner {
--- x: number,
--- y: number,
--- nextBase: Base,
--- prevBase: Base | nil,
--- forcedTo: Base | nil,
--- }
--- @alias Fielder {
--- onArrive: fun() | nil,
--- x: number | nil,
--- y: number | nil,
--- target: XYPair | nil,
--- speed: number,
--- }
import 'graphics.lua' import 'graphics.lua'
import 'scoreboard.lua' import 'scoreboard.lua'
@ -34,7 +55,7 @@ local PlayerImageBlipper <const> = blipper.new(100, "images/game/player.png", "i
local DanceBounceMs <const> = 500 local DanceBounceMs <const> = 500
local DanceBounceCount <const> = 4 local DanceBounceCount <const> = 4
local FielderDanceAnimator <const> = gfx.animator.new(DanceBounceMs, 10, 0, easingHill) --, -1 * DanceBounceMs * DanceBounceCount) local FielderDanceAnimator <const> = gfx.animator.new(DanceBounceMs, 10, 0, easingHill)
FielderDanceAnimator.repeatCount = DanceBounceCount - 1 FielderDanceAnimator.repeatCount = DanceBounceCount - 1
local BallOffscreen <const> = 999 local BallOffscreen <const> = 999
@ -200,14 +221,6 @@ function playdate.AButtonDown()
pitch() pitch()
end end
function playdate.upButtonDown() end
function playdate.downButtonDown() end
function playdate.rightButtonDown() end
function playdate.leftButtonDown() end
local elapsedSec = 0 local elapsedSec = 0
local crankChange = 0 local crankChange = 0
local acceleratedChange local acceleratedChange
@ -282,7 +295,7 @@ function updateFielders()
else else
fielder.target = nil fielder.target = nil
if fielder.onArrive then if fielder.onArrive then
fielder.onArrive() fielder:onArrive()
end end
end end
end end
@ -436,6 +449,20 @@ function init()
playdate.getSystemMenu():addMenuItem("Restart game", function() end) playdate.getSystemMenu():addMenuItem("Restart game", function() end)
end end
function tryToThrowOut(self)
self.onArrive = nil
local targetX, targetY = getNextThrowTarget()
if targetX ~= nil and targetY ~= nil then
local nearestFielder = getNearestOf(fielders, targetX, targetY)
nearestFielder.target = xy(targetX, targetY)
if nearestFielder == self then
ball.heldBy = self
else
throwBall(targetX, targetY, playdate.easingFunctions.linear, nil, true)
end
end
end
function updateBatting() function updateBatting()
secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds
if secondsSincePitchAllowed > PitchAfterSeconds then if secondsSincePitchAllowed > PitchAfterSeconds then
@ -482,19 +509,7 @@ function updateBatting()
local chasingFielder = getNearestOf(fielders, ballDestX, ballDestY) local chasingFielder = getNearestOf(fielders, ballDestX, ballDestY)
chasingFielder.target = { x = ballDestX, y = ballDestY } chasingFielder.target = { x = ballDestX, y = ballDestY }
chasingFielder.onArrive = function() chasingFielder.onArrive = tryToThrowOut
chasingFielder.onArrive = nil
local targetX, targetY = getNextThrowTarget()
if targetX ~= nil and targetY ~= nil then
local nearestFielder = getNearestOf(fielders, targetX, targetY)
nearestFielder.target = xy(targetX, targetY)
if nearestFielder == chasingFielder then
ball.heldBy = chasingFielder
else
throwBall(targetX, targetY, playdate.easingFunctions.linear, nil, true)
end
end
end
end end
end end

View File

@ -29,7 +29,8 @@ function drawScoreboard(x, y, teams, outs)
gfx.setColor(gfx.kColorWhite) gfx.setColor(gfx.kColorWhite)
function circleParams(i) function circleParams(i)
return (x + ScoreboardMarginX + OutBubbleRadius) + (i * 2.5 * OutBubbleRadius), y + 46, OutBubbleRadius local circleOffset = i * 2.5 * OutBubbleRadius
return (x + ScoreboardMarginX + OutBubbleRadius) + circleOffset, y + 46, OutBubbleRadius
end end
for i = outs, 2 do for i = outs, 2 do