From fbbfc3c2e764d40bc3f07f76ce61afd1c8027fb3 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 10 Feb 2025 22:36:17 -0500 Subject: [PATCH] Move fielder-draw iteration and dance to fielding.lua Move PlayerImageBlipper to graphics.lua --- src/assets.lua | 2 +- src/assets.lua2p | 3 ++- src/constants.lua | 1 + src/draw/fielder.lua | 3 +++ src/fielding.lua | 17 +++++++++++++++++ src/graphics.lua | 3 +++ src/main.lua | 23 ++++++----------------- 7 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/assets.lua b/src/assets.lua index c59bfab..5789cd2 100644 --- a/src/assets.lua +++ b/src/assets.lua @@ -1,4 +1,4 @@ - -- GENERATED FILE - DO NOT EDIT +-- GENERATED FILE - DO NOT EDIT -- Instead, edit the source file directly: assets.lua2p. --selene: allow(unused_variable) diff --git a/src/assets.lua2p b/src/assets.lua2p index 5460604..37459b3 100644 --- a/src/assets.lua2p +++ b/src/assets.lua2p @@ -16,8 +16,9 @@ return assetCode end 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." -end) !!(generatedFileWarning()) +end)!!(generatedFileWarning()) !!(dirLookup('images/game', 'png', 'playdate.graphics.image.new')) !!(dirLookup('sounds', 'wav', 'playdate.sound.sampleplayer.new')) diff --git a/src/constants.lua b/src/constants.lua index 1687e13..7eb59f9 100644 --- a/src/constants.lua +++ b/src/constants.lua @@ -85,6 +85,7 @@ C.BallCatchHitbox = 3 C.BaseHitbox = 10 C.BattingPower = 20 +C.BatterHandPos = utils.xy(25, 15) C.SmallestBallRadius = 6 diff --git a/src/draw/fielder.lua b/src/draw/fielder.lua index a365742..e908d9f 100644 --- a/src/draw/fielder.lua +++ b/src/draw/fielder.lua @@ -20,6 +20,9 @@ local function drawFielderGlove(ball, fielderX, fielderY) end end +---@param ball Point3d +---@param x number +---@param y number ---@return boolean isHoldingBall function drawFielder(ball, x, y) gfx.fillRect(x, y, 14, 25) diff --git a/src/fielding.lua b/src/fielding.lua index 3c88bea..12c7227 100644 --- a/src/fielding.lua +++ b/src/fielding.lua @@ -11,6 +11,9 @@ ---@field fielderTouchingBall Fielder | nil Fielding = {} +local FielderDanceAnimator = playdate.graphics.animator.new(1, 10, 0, utils.easingHill) +FielderDanceAnimator.repeatCount = C.DanceBounceCount - 1 + ---@param name string ---@param speed number ---@return Fielder @@ -148,3 +151,17 @@ function Fielding:userThrowTo(targetBase, launchBall, throwFlyMs) return userThrowToImpl(self, targetBase, launchBall, throwFlyMs) 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 diff --git a/src/graphics.lua b/src/graphics.lua index 0115c23..773dbf3 100644 --- a/src/graphics.lua +++ b/src/graphics.lua @@ -38,3 +38,6 @@ function blipper.new(msInterval, image1, image2) end, } end + +--selene: allow(unscoped_variables) +PlayerImageBlipper = blipper.new(100, Player, PlayerLowHat) diff --git a/src/main.lua b/src/main.lua index 01664ea..0aa61a9 100644 --- a/src/main.lua +++ b/src/main.lua @@ -23,6 +23,8 @@ import 'CoreLibs/ui.lua' import 'utils.lua' import 'constants.lua' import 'assets.lua' +import 'draw/overlay.lua' +import 'draw/fielder.lua' import 'action-queue.lua' import 'announcer.lua' @@ -32,8 +34,6 @@ import 'dbg.lua' import 'fielding.lua' import 'graphics.lua' import 'npc.lua' -import 'draw/overlay.lua' -import 'draw/fielder.lua' -- stylua: ignore end -- selene: allow(shadowing) @@ -73,18 +73,11 @@ local secondsSincePitchAllowed = -5 local catcherThrownBall = false -local BatterHandPos = utils.xy(25, 15) - local batBase = utils.xy(C.Center.x - 34, 215) local batTip = utils.xy(0, 0) local batAngleDeg = C.CrankOffsetDeg -local PlayerImageBlipper = blipper.new(100, Player, PlayerLowHat) - -local FielderDanceAnimator = gfx.animator.new(1, 10, 0, utils.easingHill) -FielderDanceAnimator.repeatCount = C.DanceBounceCount - 1 - ---@alias Pitch { x: SimpleAnimator, y: SimpleAnimator, z: SimpleAnimator | nil } ---@type Pitch[] @@ -171,7 +164,7 @@ local function nextHalfInning() local currentlyFieldingTeam = battingTeam == teams.home and teams.away or teams.home local gameOver = inning == 9 and teams.away.score ~= teams.home.score if not gameOver then - FielderDanceAnimator:reset(C.DanceBounceMs) + fielding:celebrate() secondsSinceLastRunnerMove = -7 fielding:benchTo(currentlyFieldingTeam.benchPosition) announcer:say("SWITCHING SIDES...") @@ -259,8 +252,8 @@ end local function updateBatting(batDeg, batSpeed) local batAngle = math.rad(batDeg) -- TODO: animate bat-flip or something - batBase.x = baserunning.batter and (baserunning.batter.x + BatterHandPos.x) or 0 - batBase.y = baserunning.batter and (baserunning.batter.y + BatterHandPos.y) or 0 + batBase.x = baserunning.batter and (baserunning.batter.x + C.BatterHandPos.x) 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.y = batBase.y + (C.BatLength * math.cos(batAngle)) @@ -458,11 +451,7 @@ function playdate.update() GrassBackground:draw(-400, -240) - local fielderDanceHeight = FielderDanceAnimator:currentValue() - local ballIsHeld = false - for _, fielder in pairs(fielding.fielders) do - ballIsHeld = drawFielder(ball, fielder.x, fielder.y + fielderDanceHeight) or ballIsHeld - end + local ballIsHeld = fielding:drawFielders(ball) if offenseState == C.Offense.batting then gfx.setLineWidth(5)