Start extracting ball.lua

PseudoAnimator -> SimpleAnimator
This commit is contained in:
Sage Vaillancourt 2025-02-09 11:49:15 -05:00
parent fed1151179
commit 476e0d54cb
3 changed files with 77 additions and 37 deletions

62
src/ball.lua Normal file
View File

@ -0,0 +1,62 @@
-- selene: allow(unscoped_variables)
---@class Ball
---@field private gfx pd_graphics_lib
---@field x number
---@field y number
---@field z number
---@field size number
---@field heldBy Fielder | nil
---@field xAnimator SimpleAnimator
---@field yAnimator SimpleAnimator
---@field sizeAnimator SimpleAnimator
---@field floatAnimator SimpleAnimator
Ball = {}
---@param g pd_graphics_lib
---@return Ball
function Ball.new(g)
return setmetatable({
gfx = g,
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 = g.animator.new(2000, -60, 0, utils.easingHill),
}, { __index = Ball })
end
--- Launches the ball from its current position to the given destination.
---@param destX number
---@param destY number
---@param easingFunc EasingFunc
---@param flyTimeMs number | nil
---@param floaty boolean | nil
---@param customBallScaler pd_animator | nil
function Ball:launch(destX, destY, easingFunc, flyTimeMs, floaty, customBallScaler)
self.heldBy = nil
if not flyTimeMs then
flyTimeMs = utils.distanceBetween(self.x, self.y, destX, destY) * 5
end
if customBallScaler then
self.sizeAnimator = customBallScaler
else
-- TODO? Scale based on distance?
self.sizeAnimator = self.gfx.animator.new(flyTimeMs, 9, C.SmallestBallRadius, utils.easingHill)
end
self.yAnimator = self.gfx.animator.new(flyTimeMs, self.y, destY, easingFunc)
self.xAnimator = self.gfx.animator.new(flyTimeMs, self.x, destX, easingFunc)
if floaty then
self.floatAnimator:reset(flyTimeMs)
end
end

View File

@ -17,7 +17,14 @@ import 'CoreLibs/ui.lua'
--- @alias EasingFunc fun(number, number, number, number): number --- @alias EasingFunc fun(number, number, number, number): number
--- @alias LaunchBall fun(destX: number, destY: number, easingFunc: EasingFunc, flyTimeMs: number | nil, floaty: boolean | nil, customBallScaler: pd_animator | nil) --- @alias LaunchBall fun(
--- destX: number,
--- destY: number,
--- easingFunc: EasingFunc,
--- flyTimeMs: number | nil,
--- floaty: boolean | nil,
--- customBallScaler: pd_animator | nil,
--- )
-- stylua: ignore start -- stylua: ignore start
import 'utils.lua' import 'utils.lua'
@ -26,6 +33,7 @@ import 'assets.lua'
import 'action-queue.lua' import 'action-queue.lua'
import 'announcer.lua' import 'announcer.lua'
import 'ball.lua'
import 'baserunning.lua' import 'baserunning.lua'
import 'dbg.lua' import 'dbg.lua'
import 'fielding.lua' import 'fielding.lua'
@ -38,30 +46,16 @@ import 'draw/fielder.lua'
-- selene: allow(shadowing) -- selene: allow(shadowing)
local gfx <const>, C <const> = playdate.graphics, C local gfx <const>, C <const> = playdate.graphics, C
local announcer = Announcer:new() local announcer = Announcer.new()
local baserunning = Baserunning.new(announcer) local baserunning = Baserunning.new(announcer)
local fielding = Fielding.new() local fielding = Fielding.new()
---@alias PseudoAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) } ---@alias SimpleAnimator { currentValue: fun(self): number; reset: fun(self, durationMs: number | nil) }
---@alias Pitch { x: PseudoAnimator, y: PseudoAnimator, z: PseudoAnimator | nil } ---@alias Pitch { x: SimpleAnimator, y: SimpleAnimator, z: SimpleAnimator | nil }
local deltaSeconds = 0 local deltaSeconds = 0
local ball <const> = { local ball = Ball.new(gfx)
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 } ---@alias Team { score: number, benchPosition: XyPair }
@ -152,24 +146,8 @@ end
---@param floaty boolean | nil ---@param floaty boolean | nil
---@param customBallScaler pd_animator | nil ---@param customBallScaler pd_animator | nil
local function launchBall(destX, destY, easingFunc, flyTimeMs, floaty, customBallScaler) local function launchBall(destX, destY, easingFunc, flyTimeMs, floaty, customBallScaler)
ball.heldBy = nil
throwMeter = 0 throwMeter = 0
ball:launch(destX, destY, easingFunc, flyTimeMs, floaty, customBallScaler)
if not flyTimeMs then
flyTimeMs = utils.distanceBetween(ball.x, ball.y, destX, destY) * 5
end
if customBallScaler then
ball.sizeAnimator = customBallScaler
else
-- TODO? Scale based on distance?
ball.sizeAnimator = gfx.animator.new(flyTimeMs, 9, C.SmallestBallRadius, utils.easingHill)
end
ball.yAnimator = gfx.animator.new(flyTimeMs, ball.y, destY, easingFunc)
ball.xAnimator = gfx.animator.new(flyTimeMs, ball.x, destX, easingFunc)
if floaty then
ball.floatAnimator:reset(flyTimeMs)
end
end end
---@param pitchFlyTimeMs number | nil ---@param pitchFlyTimeMs number | nil

View File

@ -25,7 +25,7 @@ end
--- Build an "animator" whose `:currentValue()` always returns the given value. --- Build an "animator" whose `:currentValue()` always returns the given value.
--- Essentially an "empty object" pattern for initial object positions. --- Essentially an "empty object" pattern for initial object positions.
---@param value number ---@param value number
---@return PseudoAnimator ---@return SimpleAnimator
function utils.staticAnimator(value) function utils.staticAnimator(value)
return { return {
currentValue = function(_) currentValue = function(_)