Store animators directly on the `ball` object

This commit is contained in:
Sage Vaillancourt 2025-02-09 11:26:24 -05:00
parent 9c0d263a29
commit d74332f685
1 changed files with 36 additions and 36 deletions

View File

@ -42,35 +42,25 @@ local announcer = Announcer:new()
local baserunning = Baserunning.new(announcer) local baserunning = Baserunning.new(announcer)
local fielding = Fielding.new() local fielding = Fielding.new()
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 PseudoAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) } ---@alias PseudoAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) }
---@alias Pitch { x: PseudoAnimator, y: PseudoAnimator, z: PseudoAnimator | 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 deltaSeconds = 0
local BatterHandPos <const> = utils.xy(10, 25)
local batBase <const> = utils.xy(C.Center.x - 34, 215)
local batTip <const> = utils.xy(0, 0)
local ball <const> = { local ball <const> = {
x = C.Center.x --[[@as number]], x = C.Center.x --[[@as number]],
y = C.Center.y --[[@as number]], y = C.Center.y --[[@as number]],
z = 0, z = 0,
size = C.SmallestBallRadius, size = C.SmallestBallRadius,
heldBy = nil --[[@type Runner | nil]], 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 } ---@alias Team { score: number, benchPosition: XYPair }
@ -102,8 +92,18 @@ local secondsSinceLastRunnerMove = 0
local secondsSincePitchAllowed = -5 local secondsSincePitchAllowed = -5
local catcherThrownBall = false local catcherThrownBall = false
local BatterHandPos <const> = utils.xy(10, 25)
local batBase <const> = utils.xy(C.Center.x - 34, 215)
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
---@type Pitch[] ---@type Pitch[]
local Pitches <const> = { local Pitches <const> = {
-- Fastball -- Fastball
@ -125,7 +125,7 @@ local Pitches <const> = {
{ {
x = { x = {
currentValue = function() 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, end,
reset = function() end, reset = function() end,
}, },
@ -160,15 +160,15 @@ local function throwBall(destX, destY, easingFunc, flyTimeMs, floaty, customBall
end end
if customBallScaler then if customBallScaler then
ballSizeAnimator = customBallScaler ball.sizeAnimator = customBallScaler
else else
-- TODO? Scale based on distance? -- 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 end
ballAnimatorY = gfx.animator.new(flyTimeMs, ball.y, destY, easingFunc) ball.yAnimator = gfx.animator.new(flyTimeMs, ball.y, destY, easingFunc)
ballAnimatorX = gfx.animator.new(flyTimeMs, ball.x, destX, easingFunc) ball.xAnimator = gfx.animator.new(flyTimeMs, ball.x, destX, easingFunc)
if floaty then if floaty then
ballFloatAnimator:reset(flyTimeMs) ball.floatAnimator:reset(flyTimeMs)
end end
end end
@ -179,21 +179,21 @@ local function pitch(pitchFlyTimeMs, pitchTypeIndex)
offenseState = C.Offense.batting offenseState = C.Offense.batting
local current = Pitches[pitchTypeIndex] local current = Pitches[pitchTypeIndex]
ballAnimatorX = current.x ball.xAnimator = current.x
ballAnimatorY = current.y or Pitches[1].y ball.yAnimator = current.y or Pitches[1].y
-- TODO: This would need to be sanely replaced in throwBall() etc. -- TODO: This would need to be sanely replaced in throwBall() etc.
-- if current.z then -- if current.z then
-- ballFloatAnimator = current.z -- ball.floatAnimator = current.z
-- ballFloatAnimator:reset() -- ball.floatAnimator:reset()
-- end -- end
if pitchFlyTimeMs then if pitchFlyTimeMs then
ballAnimatorX:reset(pitchFlyTimeMs) ball.xAnimator:reset(pitchFlyTimeMs)
ballAnimatorY:reset(pitchFlyTimeMs) ball.yAnimator:reset(pitchFlyTimeMs)
else else
ballAnimatorX:reset() ball.xAnimator:reset()
ballAnimatorY:reset() ball.yAnimator:reset()
end end
secondsSincePitchAllowed = 0 secondsSincePitchAllowed = 0
@ -376,10 +376,10 @@ local function updateGameState()
ball.y = ball.heldBy.y ball.y = ball.heldBy.y
ball.size = C.SmallestBallRadius ball.size = C.SmallestBallRadius
else else
ball.x = ballAnimatorX:currentValue() ball.x = ball.xAnimator:currentValue()
ball.z = ballFloatAnimator:currentValue() ball.z = ball.floatAnimator:currentValue()
ball.y = ballAnimatorY:currentValue() + ball.z ball.y = ball.yAnimator:currentValue() + ball.z
ball.size = ballSizeAnimator:currentValue() ball.size = ball.sizeAnimator:currentValue()
end end
local playerOnOffense, playerOnDefense = playerIsOn(C.Sides.offense) local playerOnOffense, playerOnDefense = playerIsOn(C.Sides.offense)