From d74332f685c1a1079d9af3dda7aa4c0faf64aeb5 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sun, 9 Feb 2025 11:26:24 -0500 Subject: [PATCH] Store animators directly on the `ball` object --- src/main.lua | 72 ++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/main.lua b/src/main.lua index 364704b..316d241 100644 --- a/src/main.lua +++ b/src/main.lua @@ -42,35 +42,25 @@ local announcer = Announcer:new() local baserunning = Baserunning.new(announcer) local fielding = Fielding.new() -local PlayerImageBlipper = blipper.new(100, Player, PlayerLowHat) - -local FielderDanceAnimator = gfx.animator.new(1, 10, 0, utils.easingHill) -FielderDanceAnimator.repeatCount = C.DanceBounceCount - 1 - ---@alias PseudoAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) } ---@alias Pitch { x: PseudoAnimator, y: PseudoAnimator, z: PseudoAnimator | nil } -local ballAnimatorX = utils.staticAnimator(C.BallOffscreen) -local ballAnimatorY = utils.staticAnimator(C.BallOffscreen) -local ballSizeAnimator = utils.staticAnimator(C.SmallestBallRadius) - --- TODO? Replace this AND ballSizeAnimator with a ballAnimatorZ? --- ...that might lose some of the magic of both. Compromise available? idk -local ballFloatAnimator = gfx.animator.new(2000, -60, 0, utils.easingHill) - local deltaSeconds = 0 -local BatterHandPos = utils.xy(10, 25) - -local batBase = utils.xy(C.Center.x - 34, 215) -local batTip = utils.xy(0, 0) - local ball = { x = C.Center.x --[[@as number]], y = C.Center.y --[[@as number]], z = 0, size = C.SmallestBallRadius, heldBy = nil --[[@type Runner | nil]], + + xAnimator = utils.staticAnimator(C.BallOffscreen), + yAnimator = utils.staticAnimator(C.BallOffscreen), + + -- TODO? Replace these with a ballAnimatorZ? + -- ...that might lose some of the magic of both. Compromise available? idk + sizeAnimator = utils.staticAnimator(C.SmallestBallRadius), + floatAnimator = gfx.animator.new(2000, -60, 0, utils.easingHill), } ---@alias Team { score: number, benchPosition: XYPair } @@ -102,8 +92,18 @@ local secondsSinceLastRunnerMove = 0 local secondsSincePitchAllowed = -5 local catcherThrownBall = false +local BatterHandPos = utils.xy(10, 25) + +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 + ---@type Pitch[] local Pitches = { -- Fastball @@ -125,7 +125,7 @@ local Pitches = { { x = { currentValue = function() - return C.PitchStartX + (10 * math.sin((ballAnimatorY:currentValue() - C.PitchStartY) / 10)) + return C.PitchStartX + (10 * math.sin((ball.yAnimator:currentValue() - C.PitchStartY) / 10)) end, reset = function() end, }, @@ -160,15 +160,15 @@ local function throwBall(destX, destY, easingFunc, flyTimeMs, floaty, customBall end if customBallScaler then - ballSizeAnimator = customBallScaler + ball.sizeAnimator = customBallScaler else -- TODO? Scale based on distance? - ballSizeAnimator = gfx.animator.new(flyTimeMs, 9, C.SmallestBallRadius, utils.easingHill) + ball.sizeAnimator = gfx.animator.new(flyTimeMs, 9, C.SmallestBallRadius, utils.easingHill) end - ballAnimatorY = gfx.animator.new(flyTimeMs, ball.y, destY, easingFunc) - ballAnimatorX = gfx.animator.new(flyTimeMs, ball.x, destX, easingFunc) + ball.yAnimator = gfx.animator.new(flyTimeMs, ball.y, destY, easingFunc) + ball.xAnimator = gfx.animator.new(flyTimeMs, ball.x, destX, easingFunc) if floaty then - ballFloatAnimator:reset(flyTimeMs) + ball.floatAnimator:reset(flyTimeMs) end end @@ -179,21 +179,21 @@ local function pitch(pitchFlyTimeMs, pitchTypeIndex) offenseState = C.Offense.batting local current = Pitches[pitchTypeIndex] - ballAnimatorX = current.x - ballAnimatorY = current.y or Pitches[1].y + ball.xAnimator = current.x + ball.yAnimator = current.y or Pitches[1].y -- TODO: This would need to be sanely replaced in throwBall() etc. -- if current.z then - -- ballFloatAnimator = current.z - -- ballFloatAnimator:reset() + -- ball.floatAnimator = current.z + -- ball.floatAnimator:reset() -- end if pitchFlyTimeMs then - ballAnimatorX:reset(pitchFlyTimeMs) - ballAnimatorY:reset(pitchFlyTimeMs) + ball.xAnimator:reset(pitchFlyTimeMs) + ball.yAnimator:reset(pitchFlyTimeMs) else - ballAnimatorX:reset() - ballAnimatorY:reset() + ball.xAnimator:reset() + ball.yAnimator:reset() end secondsSincePitchAllowed = 0 @@ -376,10 +376,10 @@ local function updateGameState() ball.y = ball.heldBy.y ball.size = C.SmallestBallRadius else - ball.x = ballAnimatorX:currentValue() - ball.z = ballFloatAnimator:currentValue() - ball.y = ballAnimatorY:currentValue() + ball.z - ball.size = ballSizeAnimator:currentValue() + ball.x = ball.xAnimator:currentValue() + ball.z = ball.floatAnimator:currentValue() + ball.y = ball.yAnimator:currentValue() + ball.z + ball.size = ball.sizeAnimator:currentValue() end local playerOnOffense, playerOnDefense = playerIsOn(C.Sides.offense)