From a46bac174c2ac25ad19775430eb1ab2b8172198e Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sun, 2 Feb 2025 20:20:53 -0500 Subject: [PATCH] Refactor to centralize some secondsSince var usage Prevent fielder dance as they take the field. --- src/main.lua | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/main.lua b/src/main.lua index be7b7f5..a19b4fb 100644 --- a/src/main.lua +++ b/src/main.lua @@ -55,9 +55,13 @@ local PlayerImageBlipper = blipper.new(100, "images/game/player.png", "i local DanceBounceMs = 500 local DanceBounceCount = 4 -local FielderDanceAnimator = gfx.animator.new(DanceBounceMs, 10, 0, easingHill) +local FielderDanceAnimator = gfx.animator.new(1, 10, 0, easingHill) FielderDanceAnimator.repeatCount = DanceBounceCount - 1 +function fieldersDance() + FielderDanceAnimator:reset(DanceBounceMs) +end + local BallOffscreen = 999 local PitchFlyMs = 2500 @@ -207,18 +211,16 @@ function throwBall(destX, destY, easingFunc, flyTimeMs, floaty) end end +local PitchAfterSeconds = 5 +-- TODO: Replace with a timer, repeatedly reset instead of setting to 0 +local secondsSincePitchAllowed = -5 + function pitch() currentMode = Modes.batting ballAnimatorX = gfx.animator.new(0, PitchStartX, PitchStartX, playdate.easingFunctions.linear) ballAnimatorY = PitchAnimator PitchAnimator:reset() -end - -function playdate.AButtonDown() - if not batter then - return - end - pitch() + secondsSincePitchAllowed = 0 end local elapsedSec = 0 @@ -260,7 +262,7 @@ function outRunner(runnerIndex) outs = outs + 1 outRunners[#outRunners + 1] = runners[runnerIndex] table.remove(runners, runnerIndex) - FielderDanceAnimator:reset() + fieldersDance() updateForcedRunners() announcer:say("YOU'RE OUT!") if outs == 3 then @@ -322,14 +324,10 @@ end --- Returns true if at least one runner is still moving ---@return boolean -function updateRunners() +function updateRunners(currentRunners) local autoRunSpeed = 20 * deltaSeconds --autoRunSpeed = 140 -- TODO: Filter for the runner closest to the currently-held direction button - local currentRunners = currentMode == Modes.batting and { batter } - or filter(runners, function(runner) - return runner ~= batter - end) local runnerMoved = false for runnerIndex, runner in ipairs(currentRunners) do @@ -436,11 +434,9 @@ function getNextThrowTarget() end local ResetFieldersAfterSeconds = 4 +-- TODO: Replace with a timer, repeatedly reset instead of setting to 0 local secondsSinceLastRunnerMove = 0 -local PitchAfterSeconds = 5 -local secondsSincePitchAllowed = -5 - function init() playdate.display.setRefreshRate(50) gfx.setBackgroundColor(gfx.kColorWhite) @@ -464,11 +460,6 @@ function tryToThrowOut(self) end function updateBatting() - secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds - if secondsSincePitchAllowed > PitchAfterSeconds then - pitch() - secondsSincePitchAllowed = 0 - end if ball.y < BallOffscreen then ball.y = ballAnimatorY:currentValue() + ballFloatAnimator:currentValue() ball.size = 6 @@ -514,9 +505,11 @@ function updateBatting() end function updateRunning() - secondsSincePitchAllowed = 0 + local nonBatterRunners = filter(runners, function (runner) + return runner ~= batter + end) ball.size = ballSizeAnimator:currentValue() - if updateRunners() then + if updateRunners(nonBatterRunners) then secondsSinceLastRunnerMove = 0 else secondsSinceLastRunnerMove = secondsSinceLastRunnerMove + deltaSeconds @@ -555,9 +548,14 @@ function updateGameState() end if currentMode == Modes.batting then + secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds + if secondsSincePitchAllowed > PitchAfterSeconds then + pitch() + end updateBatting() - updateRunners() + updateRunners({ batter }) elseif currentMode == Modes.running then + secondsSincePitchAllowed = 0 updateRunning() end