Move fielder-draw iteration and dance to fielding.lua

Move PlayerImageBlipper to graphics.lua
This commit is contained in:
Sage Vaillancourt 2025-02-10 22:36:17 -05:00
parent f67d6262ac
commit fbbfc3c2e7
7 changed files with 33 additions and 19 deletions

View File

@ -1,4 +1,4 @@
-- GENERATED FILE - DO NOT EDIT -- GENERATED FILE - DO NOT EDIT
-- Instead, edit the source file directly: assets.lua2p. -- Instead, edit the source file directly: assets.lua2p.
--selene: allow(unused_variable) --selene: allow(unused_variable)

View File

@ -16,8 +16,9 @@
return assetCode return assetCode
end end
function generatedFileWarning() function generatedFileWarning()
-- Only in a function to make clear that THIS .lua2p is not the generated file!
return "-- GENERATED FILE - DO NOT EDIT\n-- Instead, edit the source file directly: assets.lua2p." return "-- GENERATED FILE - DO NOT EDIT\n-- Instead, edit the source file directly: assets.lua2p."
end) !!(generatedFileWarning()) end)!!(generatedFileWarning())
!!(dirLookup('images/game', 'png', 'playdate.graphics.image.new')) !!(dirLookup('images/game', 'png', 'playdate.graphics.image.new'))
!!(dirLookup('sounds', 'wav', 'playdate.sound.sampleplayer.new')) !!(dirLookup('sounds', 'wav', 'playdate.sound.sampleplayer.new'))

View File

@ -85,6 +85,7 @@ C.BallCatchHitbox = 3
C.BaseHitbox = 10 C.BaseHitbox = 10
C.BattingPower = 20 C.BattingPower = 20
C.BatterHandPos = utils.xy(25, 15)
C.SmallestBallRadius = 6 C.SmallestBallRadius = 6

View File

@ -20,6 +20,9 @@ local function drawFielderGlove(ball, fielderX, fielderY)
end end
end end
---@param ball Point3d
---@param x number
---@param y number
---@return boolean isHoldingBall ---@return boolean isHoldingBall
function drawFielder(ball, x, y) function drawFielder(ball, x, y)
gfx.fillRect(x, y, 14, 25) gfx.fillRect(x, y, 14, 25)

View File

@ -11,6 +11,9 @@
---@field fielderTouchingBall Fielder | nil ---@field fielderTouchingBall Fielder | nil
Fielding = {} Fielding = {}
local FielderDanceAnimator <const> = playdate.graphics.animator.new(1, 10, 0, utils.easingHill)
FielderDanceAnimator.repeatCount = C.DanceBounceCount - 1
---@param name string ---@param name string
---@param speed number ---@param speed number
---@return Fielder ---@return Fielder
@ -148,3 +151,17 @@ function Fielding:userThrowTo(targetBase, launchBall, throwFlyMs)
return userThrowToImpl(self, targetBase, launchBall, throwFlyMs) return userThrowToImpl(self, targetBase, launchBall, throwFlyMs)
end) end)
end end
function Fielding:celebrate()
FielderDanceAnimator:reset(C.DanceBounceMs)
end
---@param ball Point3d
---@return boolean ballIsHeldByAFielder
function Fielding:drawFielders(ball)
local ballIsHeld = false
for _, fielder in pairs(self.fielders) do
ballIsHeld = drawFielder(ball, fielder.x, fielder.y + FielderDanceAnimator:currentValue()) or ballIsHeld
end
return ballIsHeld
end

View File

@ -38,3 +38,6 @@ function blipper.new(msInterval, image1, image2)
end, end,
} }
end end
--selene: allow(unscoped_variables)
PlayerImageBlipper = blipper.new(100, Player, PlayerLowHat)

View File

@ -23,6 +23,8 @@ import 'CoreLibs/ui.lua'
import 'utils.lua' import 'utils.lua'
import 'constants.lua' import 'constants.lua'
import 'assets.lua' import 'assets.lua'
import 'draw/overlay.lua'
import 'draw/fielder.lua'
import 'action-queue.lua' import 'action-queue.lua'
import 'announcer.lua' import 'announcer.lua'
@ -32,8 +34,6 @@ import 'dbg.lua'
import 'fielding.lua' import 'fielding.lua'
import 'graphics.lua' import 'graphics.lua'
import 'npc.lua' import 'npc.lua'
import 'draw/overlay.lua'
import 'draw/fielder.lua'
-- stylua: ignore end -- stylua: ignore end
-- selene: allow(shadowing) -- selene: allow(shadowing)
@ -73,18 +73,11 @@ local secondsSincePitchAllowed = -5
local catcherThrownBall = false local catcherThrownBall = false
local BatterHandPos <const> = utils.xy(25, 15)
local batBase <const> = utils.xy(C.Center.x - 34, 215) local batBase <const> = utils.xy(C.Center.x - 34, 215)
local batTip <const> = utils.xy(0, 0) local batTip <const> = utils.xy(0, 0)
local batAngleDeg = C.CrankOffsetDeg local batAngleDeg = C.CrankOffsetDeg
local PlayerImageBlipper <const> = blipper.new(100, Player, PlayerLowHat)
local FielderDanceAnimator <const> = gfx.animator.new(1, 10, 0, utils.easingHill)
FielderDanceAnimator.repeatCount = C.DanceBounceCount - 1
---@alias Pitch { x: SimpleAnimator, y: SimpleAnimator, z: SimpleAnimator | nil } ---@alias Pitch { x: SimpleAnimator, y: SimpleAnimator, z: SimpleAnimator | nil }
---@type Pitch[] ---@type Pitch[]
@ -171,7 +164,7 @@ local function nextHalfInning()
local currentlyFieldingTeam = battingTeam == teams.home and teams.away or teams.home local currentlyFieldingTeam = battingTeam == teams.home and teams.away or teams.home
local gameOver = inning == 9 and teams.away.score ~= teams.home.score local gameOver = inning == 9 and teams.away.score ~= teams.home.score
if not gameOver then if not gameOver then
FielderDanceAnimator:reset(C.DanceBounceMs) fielding:celebrate()
secondsSinceLastRunnerMove = -7 secondsSinceLastRunnerMove = -7
fielding:benchTo(currentlyFieldingTeam.benchPosition) fielding:benchTo(currentlyFieldingTeam.benchPosition)
announcer:say("SWITCHING SIDES...") announcer:say("SWITCHING SIDES...")
@ -259,8 +252,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 = baserunning.batter and (baserunning.batter.x + BatterHandPos.x) or 0 batBase.x = baserunning.batter and (baserunning.batter.x + C.BatterHandPos.x) or 0
batBase.y = baserunning.batter and (baserunning.batter.y + BatterHandPos.y) or 0 batBase.y = baserunning.batter and (baserunning.batter.y + C.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))
@ -458,11 +451,7 @@ function playdate.update()
GrassBackground:draw(-400, -240) GrassBackground:draw(-400, -240)
local fielderDanceHeight = FielderDanceAnimator:currentValue() local ballIsHeld = fielding:drawFielders(ball)
local ballIsHeld = false
for _, fielder in pairs(fielding.fielders) do
ballIsHeld = drawFielder(ball, fielder.x, fielder.y + fielderDanceHeight) or ballIsHeld
end
if offenseState == C.Offense.batting then if offenseState == C.Offense.batting then
gfx.setLineWidth(5) gfx.setLineWidth(5)