Better batter-outs, scoreboard, sounds and frowns.
A bunch more refactors, SCREAMING_SNAKE constants, graphics.lua, etc.
This commit is contained in:
parent
982b8220a6
commit
0b126919ac
|
@ -0,0 +1,22 @@
|
|||
local ballBuffer = 5
|
||||
|
||||
--- Assumes that background image is of size
|
||||
--- XXX
|
||||
--- XOX
|
||||
--- Where each character is the size of the screen, and 'O' is the default view.
|
||||
function getDrawOffset(screenW, screenH, ballX, ballY)
|
||||
local offsetX, offsetY
|
||||
if ballY < ballBuffer then
|
||||
offsetY = math.max(ballBuffer, -1 * (ballY - ballBuffer))
|
||||
else
|
||||
offsetY = 0
|
||||
end
|
||||
if ballX > 0 and ballX < (screenW - ballBuffer) then
|
||||
offsetX = 0
|
||||
elseif ballX < ballBuffer then
|
||||
offsetX = math.max(-1 * screenW, -1 * (ballX - ballBuffer))
|
||||
elseif ballX > (screenW - ballBuffer) then
|
||||
offsetX = math.min(screenW * 2, -1 * (ballX - ballBuffer))
|
||||
end
|
||||
return offsetX, offsetY
|
||||
end
|
Binary file not shown.
After Width: | Height: | Size: 712 B |
438
src/main.lua
438
src/main.lua
|
@ -4,22 +4,30 @@ import 'CoreLibs/easing.lua'
|
|||
import 'CoreLibs/graphics.lua'
|
||||
import 'CoreLibs/object.lua'
|
||||
import 'CoreLibs/ui.lua'
|
||||
|
||||
import 'graphics.lua'
|
||||
import 'utils.lua'
|
||||
|
||||
--- @alias Position { x: number, y: number }
|
||||
--- @alias XYPair { x: number, y: number }
|
||||
|
||||
--- @alias Base { x: number, y: number }
|
||||
|
||||
--- @alias Runner { x: number, y: number, nextBase: Base, prevBase: Base, forcedTo: Base }
|
||||
|
||||
--- @alias Fielder { onArrive: fun() | nil, x: number | nil, y: number | nil, target: Position | nil, speed: number }
|
||||
--- @alias Fielder { onArrive: fun() | nil, x: number | nil, y: number | nil, target: XYPair | nil, speed: number }
|
||||
|
||||
local gfx = playdate.graphics
|
||||
playdate.display.setRefreshRate(50)
|
||||
gfx.setBackgroundColor(gfx.kColorWhite)
|
||||
playdate.setMenuImage(gfx.image.new("images/game/menu-image.png"))
|
||||
|
||||
local grassBackground = gfx.image.new("images/game/grass.png") or {}
|
||||
local SCREEN = {
|
||||
W = playdate.display.getWidth(),
|
||||
H = playdate.display.getHeight()
|
||||
}
|
||||
|
||||
local CENTER = xy(SCREEN.W / 2, SCREEN.H / 2)
|
||||
|
||||
local batCrackSound = playdate.sound.sampleplayer.new("sounds/bat-crack-reverb.wav")
|
||||
local grassBackground = gfx.image.new("images/game/grass.png") --[[@as PlaydateGraphicsImage]]
|
||||
local playerFrown = gfx.image.new("images/game/player-frown.png") --[[@as PlaydateGraphicsImage]]
|
||||
|
||||
local playerImageBlipper = blipper.new(
|
||||
100,
|
||||
|
@ -27,92 +35,90 @@ local playerImageBlipper = blipper.new(
|
|||
"images/game/player-lowhat.png"
|
||||
)
|
||||
|
||||
---@type Position
|
||||
local backgroundPan = {
|
||||
x = 0,
|
||||
y = 0,
|
||||
}
|
||||
local BALL_OFFSCREEN = 999
|
||||
|
||||
local danceBounceMs = 500
|
||||
local danceBounceCount = 4
|
||||
local fielderDanceAnimator = gfx.animator.new(danceBounceMs, 10, 0, easingHill)--, -1 * danceBounceMs * danceBounceCount)
|
||||
fielderDanceAnimator.repeatCount = danceBounceCount - 1
|
||||
|
||||
local pitchFlyTimeMs = 2500
|
||||
local ballStartY, endY = 90, 250
|
||||
local easingFunction = playdate.easingFunctions.outQuint
|
||||
local pitchAnimator = gfx.animator.new(pitchFlyTimeMs, ballStartY, endY, easingFunction)
|
||||
local PITCH_START_X = 200
|
||||
local PITCH_START_Y, PITCH_END_Y = 90, 250
|
||||
local pitchAnimator = gfx.animator.new(pitchFlyTimeMs, PITCH_START_Y, PITCH_END_Y, playdate.easingFunctions.outQuint)
|
||||
|
||||
local ballSizeMs = 2000
|
||||
local ballSizeAnimator = gfx.animator.new(ballSizeMs, 9, 6, playdate.easingFunctions.outBounce)
|
||||
local CRANK_OFFSET_DEG = 90
|
||||
local batBase = xy(CENTER.x - 34, 215)
|
||||
local batTip = xy(0, 0)
|
||||
|
||||
local screenW, screenH = 400, 240
|
||||
local centerX, centerY = screenW / 2, screenH / 2
|
||||
local batBaseX, batBaseY = centerX - 34, 215
|
||||
local TAG_DISTANCE = 20
|
||||
|
||||
local batLength = 45
|
||||
local ball = {
|
||||
x = CENTER.x,
|
||||
y = BALL_OFFSCREEN,
|
||||
size = 6
|
||||
}
|
||||
|
||||
local tagDistance = 20
|
||||
|
||||
local ballY = 999 -- ballStartY
|
||||
local ballX = 200
|
||||
local ballSize = 6
|
||||
|
||||
local batTipX = 0
|
||||
local batTipY = 0
|
||||
local BAT_LENGTH = 45
|
||||
|
||||
local MODES = {
|
||||
batting = {},
|
||||
running = {}
|
||||
}
|
||||
|
||||
local currentMode = MODES.batting
|
||||
|
||||
local hitAnimatorY
|
||||
local hitAnimatorX
|
||||
local ballAnimatorY = gfx.animator.new(0, BALL_OFFSCREEN, BALL_OFFSCREEN, playdate.easingFunctions.linear)
|
||||
local ballAnimatorX = gfx.animator.new(0, BALL_OFFSCREEN, BALL_OFFSCREEN, playdate.easingFunctions.linear)
|
||||
-- TODO? Replace this AND ballSizeAnimator with a ballHeightAnimator
|
||||
-- ...that might lose some of the magic of both. Compromise available? idk
|
||||
local ballFloatAnimator = gfx.animator.new(2000, -60, 0, easingHill)
|
||||
local ballSizeMs = 2000
|
||||
local ballSizeAnimator = gfx.animator.new(ballSizeMs, 9, 6, easingHill)
|
||||
|
||||
local hitMult = 10
|
||||
local HIT_MULT = 10
|
||||
|
||||
local deltaTime = 0
|
||||
local deltaSeconds = 0
|
||||
|
||||
---@type table<string, Base>
|
||||
local FIRST, SECOND, THIRD, HOME = 1, 2, 3, 4
|
||||
|
||||
---@type Base[]
|
||||
local bases = {
|
||||
first = { x = screenW * 0.93, y = screenH * 0.52 },
|
||||
second = { x = screenW * 0.47, y = screenH * 0.19 },
|
||||
third = { x = screenW * 0.03, y = screenH * 0.52 },
|
||||
home = { x = screenW * 0.474, y = screenH * 0.79 }
|
||||
xy(SCREEN.W * 0.93, SCREEN.H * 0.52),
|
||||
xy(SCREEN.W * 0.47, SCREEN.H * 0.19),
|
||||
xy(SCREEN.W * 0.03, SCREEN.H * 0.52),
|
||||
xy(SCREEN.W * 0.474, SCREEN.H * 0.79)
|
||||
}
|
||||
|
||||
---@type table<Base, Base>
|
||||
local nextBaseMap = {
|
||||
[bases.first] = bases.second,
|
||||
[bases.second] = bases.third,
|
||||
[bases.third] = bases.home
|
||||
[bases[FIRST]] = bases[SECOND],
|
||||
[bases[SECOND]] = bases[THIRD],
|
||||
[bases[THIRD]] = bases[HOME]
|
||||
}
|
||||
|
||||
function newFielder(speed)
|
||||
return {
|
||||
speed = speed
|
||||
}
|
||||
end
|
||||
|
||||
---@type table<string, Fielder>
|
||||
local fielders = {
|
||||
first = {
|
||||
speed = 40,
|
||||
},
|
||||
second = {
|
||||
speed = 40,
|
||||
},
|
||||
shortstop = {
|
||||
speed = 40,
|
||||
},
|
||||
third = {
|
||||
speed = 40,
|
||||
},
|
||||
pitcher = {
|
||||
speed = 30,
|
||||
},
|
||||
catcher = {
|
||||
speed = 20,
|
||||
},
|
||||
left = {
|
||||
speed = 40,
|
||||
},
|
||||
center = {
|
||||
speed = 40,
|
||||
},
|
||||
right = {
|
||||
speed = 40,
|
||||
first = newFielder(40),
|
||||
second = newFielder(40),
|
||||
shortstop = newFielder(40),
|
||||
third = newFielder(40),
|
||||
pitcher = newFielder(30),
|
||||
catcher = newFielder(20),
|
||||
left = newFielder(40),
|
||||
center = newFielder(40),
|
||||
right = newFielder(40)
|
||||
}
|
||||
|
||||
local PITCHER_POS = {
|
||||
x = SCREEN.W * 0.48,
|
||||
y = SCREEN.H * 0.40
|
||||
}
|
||||
|
||||
--- Resets the target positions of all fielders to their defaults (at their field positions).
|
||||
|
@ -120,92 +126,103 @@ local fielders = {
|
|||
function resetFielderPositions(fromOffTheField)
|
||||
if fromOffTheField then
|
||||
for _,fielder in pairs(fielders) do
|
||||
fielder.x = centerX
|
||||
fielder.y = screenH
|
||||
fielder.x = CENTER.x
|
||||
fielder.y = SCREEN.H
|
||||
end
|
||||
end
|
||||
|
||||
fielders.first.target = { x = screenW - 65, y = screenH * 0.48 }
|
||||
fielders.second.target = { x = screenW * 0.70, y = screenH * 0.30 }
|
||||
fielders.shortstop.target = { x = screenW * 0.30, y = screenH * 0.30 }
|
||||
fielders.third.target = { x = screenW * 0.1, y = screenH * 0.48 }
|
||||
fielders.pitcher.target = { x = screenW * 0.48, y = screenH * 0.40 }
|
||||
fielders.catcher.target = { x = screenW * 0.475, y = screenH * 0.92 }
|
||||
fielders.left.target = { x = screenW * -1, y = screenH * -0.2 }
|
||||
fielders.center.target = { x = centerX, y = screenH * -0.4 }
|
||||
fielders.right.target = { x = screenW * 2, y = screenH * fielders.left.target.y }
|
||||
fielders.first.target = xy(SCREEN.W - 65, SCREEN.H * 0.48)
|
||||
fielders.second.target = xy(SCREEN.W * 0.70, SCREEN.H * 0.30)
|
||||
fielders.shortstop.target = xy(SCREEN.W * 0.30, SCREEN.H * 0.30)
|
||||
fielders.third.target = xy(SCREEN.W * 0.1, SCREEN.H * 0.48)
|
||||
fielders.pitcher.target = xy(PITCHER_POS.x, PITCHER_POS.y)
|
||||
fielders.catcher.target = xy(SCREEN.W * 0.475, SCREEN.H * 0.92)
|
||||
fielders.left.target = xy(SCREEN.W * -1, SCREEN.H * -0.2)
|
||||
fielders.center.target = xy(CENTER.x, SCREEN.H * -0.4)
|
||||
fielders.right.target = xy(SCREEN.W * 2, SCREEN.H * fielders.left.target.y)
|
||||
end
|
||||
resetFielderPositions(true)
|
||||
|
||||
local playerStartingX = bases.home.x - 40
|
||||
local playerStartingY = bases.home.y - 3
|
||||
local PLAYER_STARTING_X = bases[HOME].x - 40
|
||||
local PLAYER_STARTING_Y = bases[HOME].y - 3
|
||||
|
||||
--- @type Runner[]
|
||||
local runners = { }
|
||||
|
||||
--- @type Runner[]
|
||||
local outRunners = { }
|
||||
|
||||
local nameI = 1
|
||||
local runnerNames = {"Barbara", "Steve", "Jerry", "Beatrice"}
|
||||
|
||||
---@return Runner
|
||||
function newRunner()
|
||||
local new = {
|
||||
x = playerStartingX,
|
||||
y = playerStartingY,
|
||||
x = PLAYER_STARTING_X,
|
||||
y = PLAYER_STARTING_Y,
|
||||
nextBase = nil,
|
||||
prevBase = nil,
|
||||
name = runnerNames[nameI]
|
||||
}
|
||||
nameI = nameI + 1
|
||||
runners[#runners + 1] = new
|
||||
return new
|
||||
end
|
||||
|
||||
---@type Runner | nil
|
||||
local batter = newRunner()
|
||||
batter.nextBase = bases[FIRST]
|
||||
batter.forcedTo = bases[FIRST]
|
||||
|
||||
function throwBall(destX, destY, easingFunc, flyTimeMs)
|
||||
function throwBall(destX, destY, easingFunc, flyTimeMs, floaty)
|
||||
if not flyTimeMs then
|
||||
flyTimeMs = distanceBetween(ballX, ballY, destX, destY) * 5
|
||||
flyTimeMs = distanceBetween(ball.x, ball.y, destX, destY) * 5
|
||||
end
|
||||
ballSizeAnimator:reset(flyTimeMs)
|
||||
hitAnimatorY = gfx.animator.new(flyTimeMs, ballY, destY, easingFunc)
|
||||
hitAnimatorX = gfx.animator.new(flyTimeMs, ballX, destX, easingFunc)
|
||||
ballAnimatorY = gfx.animator.new(flyTimeMs, ball.y, destY, easingFunc)
|
||||
ballAnimatorX = gfx.animator.new(flyTimeMs, ball.x, destX, easingFunc)
|
||||
if floaty then
|
||||
ballFloatAnimator:reset(flyTimeMs)
|
||||
end
|
||||
end
|
||||
|
||||
function pitch()
|
||||
pitchAnimator:reset()
|
||||
ballY = ballStartY
|
||||
ballX = 200
|
||||
currentMode = MODES.batting
|
||||
|
||||
backgroundPan.y = 0
|
||||
backgroundPan.x = 0
|
||||
ballAnimatorX = gfx.animator.new(0, PITCH_START_X, PITCH_START_X, playdate.easingFunctions.linear)
|
||||
ballAnimatorY = pitchAnimator
|
||||
pitchAnimator:reset()
|
||||
end
|
||||
|
||||
function playdate.AButtonDown()
|
||||
if not batter then
|
||||
return
|
||||
end
|
||||
pitch()
|
||||
end
|
||||
|
||||
function playdate.upButtonDown()
|
||||
batBaseY = batBaseY - 1
|
||||
batBase.y = batBase.y - 1
|
||||
end
|
||||
|
||||
function playdate.downButtonDown()
|
||||
batBaseY = batBaseY + 1
|
||||
batBase.y = batBase.y + 1
|
||||
end
|
||||
|
||||
function playdate.rightButtonDown()
|
||||
batBaseX = batBaseX + 1
|
||||
batBase.x = batBase.x + 1
|
||||
end
|
||||
|
||||
function playdate.leftButtonDown()
|
||||
batBaseX = batBaseX - 1
|
||||
batBase.x = batBase.x - 1
|
||||
end
|
||||
|
||||
local pitchClockSec = 99
|
||||
local elapsedTime = 0
|
||||
local crankChange
|
||||
local elapsedSec = 0
|
||||
local crankChange = 0
|
||||
|
||||
local baseHitbox = 13
|
||||
local BASE_HITBOX = 13
|
||||
--- Returns the base being touched by the runner at (x,y), or nil, if no base is being touched
|
||||
function isTouchingBase(x, y)
|
||||
for _,base in pairs(bases) do
|
||||
if distanceBetween(x, y, base.x, base.y) < baseHitbox then
|
||||
for _,base in ipairs(bases) do
|
||||
if distanceBetween(x, y, base.x, base.y) < BASE_HITBOX then
|
||||
return base
|
||||
end
|
||||
end
|
||||
|
@ -213,18 +230,26 @@ function isTouchingBase(x, y)
|
|||
return nil
|
||||
end
|
||||
|
||||
local ballCatchHitbox = 3
|
||||
local BALL_CATCH_HITBOX = 3
|
||||
function isTouchingBall(x, y)
|
||||
local ballDistance = distanceBetween(x, y, ballX, ballY)
|
||||
return ballDistance < ballCatchHitbox
|
||||
local ballDistance = distanceBetween(x, y, ball.x, ball.y)
|
||||
return ballDistance < BALL_CATCH_HITBOX
|
||||
end
|
||||
|
||||
function updateForcedTos()
|
||||
end
|
||||
|
||||
local outs = 0
|
||||
local homeScore = 0
|
||||
local awayScore = 0
|
||||
|
||||
function outRunner(runnerIndex)
|
||||
print("You're out, runner" .. runnerIndex .. "!")
|
||||
outs = math.min(3, outs + 1)
|
||||
outRunners[#outRunners+1] = runners[runnerIndex]
|
||||
table.remove(runners, runnerIndex)
|
||||
updateForcedTos()
|
||||
fielderDanceAnimator:reset()
|
||||
end
|
||||
|
||||
function updateFielders()
|
||||
|
@ -241,8 +266,8 @@ function updateFielders()
|
|||
local x, y, distance = normalizeVector(fielder.x, fielder.y, fielder.target.x, fielder.target.y)
|
||||
|
||||
if distance > 1 then
|
||||
fielder.x = fielder.x - (x * fielder.speed * deltaTime)
|
||||
fielder.y = fielder.y - (y * fielder.speed * deltaTime)
|
||||
fielder.x = fielder.x - (x * fielder.speed * deltaSeconds)
|
||||
fielder.y = fielder.y - (y * fielder.speed * deltaSeconds)
|
||||
else
|
||||
if fielder.onArrive then
|
||||
fielder.onArrive()
|
||||
|
@ -251,15 +276,18 @@ function updateFielders()
|
|||
end
|
||||
end
|
||||
|
||||
if isTouchingBall(fielder.x, fielder.y) then
|
||||
if currentMode == MODES.running and isTouchingBall(fielder.x, fielder.y) then
|
||||
local touchedBase = isTouchingBase(fielder.x, fielder.y)
|
||||
for i,runner in pairs(runners) do
|
||||
local runnerOnBase = touchingBaseCache.get(runner)
|
||||
if touchedBase and runner.forcedTo == touchedBase and touchedBase ~= runnerOnBase then
|
||||
print("Force out of runner:")
|
||||
printTable(runner)
|
||||
outRunner(i)
|
||||
elseif not runnerOnBase then
|
||||
local fielderDistance = distanceBetween(runner.x, runner.y, fielder.x, fielder.y)
|
||||
if fielderDistance < tagDistance then
|
||||
if fielderDistance < TAG_DISTANCE then
|
||||
print("Tagged out!")
|
||||
outRunner(i)
|
||||
end
|
||||
end
|
||||
|
@ -291,7 +319,7 @@ function updateRunners()
|
|||
end
|
||||
local prevX, prevY = runner.x, runner.y
|
||||
-- TODO: Drift toward nearest base?
|
||||
local autoRun = nearestBaseDistance > 5 and mult * autoRunSpeed * deltaTime or 0
|
||||
local autoRun = nearestBaseDistance > 5 and mult * autoRunSpeed * deltaSeconds or 0
|
||||
mult = autoRun + (crankChange / 20)
|
||||
runner.x = runner.x - (x * mult)
|
||||
runner.y = runner.y - (y * mult)
|
||||
|
@ -328,7 +356,7 @@ end
|
|||
---@return Base[]
|
||||
function getForcedOutTargets()
|
||||
local targets = {}
|
||||
for _,base in pairs(bases) do
|
||||
for _,base in ipairs(bases) do
|
||||
local runnerTargetingBase = getRunnerTargeting(base)
|
||||
if runnerTargetingBase then
|
||||
targets[#targets+1] = base
|
||||
|
@ -337,10 +365,11 @@ function getForcedOutTargets()
|
|||
end
|
||||
end
|
||||
return targets
|
||||
-- return { bases.first }
|
||||
-- return { bases[FIRST] }
|
||||
end
|
||||
|
||||
--- Returns the position,distance of the basest closest to the runner furthest from a base
|
||||
---@return { x: number, y: number } | nil, number | nil
|
||||
function getBaseOfStrandedRunner()
|
||||
local farRunnersBase, farDistance
|
||||
for _,runner in pairs(runners) do
|
||||
|
@ -364,45 +393,47 @@ end
|
|||
function getNextThrowTarget()
|
||||
-- TODO: Handle missed throws, check for fielders at target, etc.
|
||||
local targets = getForcedOutTargets()
|
||||
print("forcedOutTargets:")
|
||||
printTable(targets)
|
||||
if #targets ~= 0 then
|
||||
return targets[1].x, targets[1].y
|
||||
end
|
||||
|
||||
-- local baseCloseToStrandedRunner = getBaseOfStrandedRunner()
|
||||
-- return baseCloseToStrandedRunner.x, baseCloseToStrandedRunner.y
|
||||
local baseCloseToStrandedRunner = getBaseOfStrandedRunner()
|
||||
if baseCloseToStrandedRunner then
|
||||
return baseCloseToStrandedRunner.x, baseCloseToStrandedRunner.y
|
||||
end
|
||||
end
|
||||
|
||||
local resetFieldersAfterSeconds = 5
|
||||
local resetFieldersAfterSeconds = 4
|
||||
local secondsSinceLastRunnerMove = 0
|
||||
|
||||
function updateGameState()
|
||||
deltaTime = playdate.getElapsedTime() or 0
|
||||
playdate.resetElapsedTime()
|
||||
elapsedTime = elapsedTime + deltaTime
|
||||
local pitchAfterSeconds = 4
|
||||
local secondsSincePitchAllowed = -5
|
||||
|
||||
-- if elapsedTime > pitchClockSec then
|
||||
-- elapsedTime = 0
|
||||
-- pitch()
|
||||
-- end
|
||||
|
||||
if currentMode == MODES.running then
|
||||
ballX = hitAnimatorX:currentValue()
|
||||
ballY = hitAnimatorY:currentValue()
|
||||
ballSize = ballSizeAnimator:currentValue()
|
||||
elseif ballY < 999 then
|
||||
ballY = pitchAnimator:currentValue()
|
||||
ballSize = 6
|
||||
function init()
|
||||
playdate.display.setRefreshRate(50)
|
||||
gfx.setBackgroundColor(gfx.kColorWhite)
|
||||
playdate.setMenuImage(gfx.image.new("images/game/menu-image.png"))
|
||||
resetFielderPositions(true)
|
||||
end
|
||||
|
||||
local batAngle = math.rad(playdate.getCrankPosition() + 90)
|
||||
batTipX = batBaseX + (batLength * math.sin(batAngle))
|
||||
batTipY = batBaseY + (batLength * math.cos(batAngle))
|
||||
function updateBatting()
|
||||
secondsSincePitchAllowed = secondsSincePitchAllowed + deltaSeconds
|
||||
if secondsSincePitchAllowed > pitchAfterSeconds then
|
||||
pitch()
|
||||
secondsSincePitchAllowed = 0
|
||||
end
|
||||
if ball.y < BALL_OFFSCREEN then
|
||||
ball.y = ballAnimatorY:currentValue()
|
||||
ball.size = 6
|
||||
end
|
||||
|
||||
crankChange, acceleratedChange = playdate.getCrankChange()
|
||||
if currentMode == MODES.batting and acceleratedChange >= 0 and
|
||||
pointDirectlyUnderLine(ballX, ballY, batBaseX, batBaseY, batTipX, batTipY, screenH) then
|
||||
local batAngle = math.rad(playdate.getCrankPosition() + CRANK_OFFSET_DEG)
|
||||
batTip.x = batBase.x + (BAT_LENGTH * math.sin(batAngle))
|
||||
batTip.y = batBase.y + (BAT_LENGTH * math.cos(batAngle))
|
||||
|
||||
if acceleratedChange >= 0 and
|
||||
pointDirectlyUnderLine(ball.x, ball.y, batBase.x, batBase.y, batTip.x, batTip.y, SCREEN.H) then
|
||||
batCrackSound:play()
|
||||
currentMode = MODES.running
|
||||
ballAngle = batAngle + math.rad(90)
|
||||
|
||||
|
@ -413,13 +444,13 @@ function updateGameState()
|
|||
ballVelX = ballVelX * -1
|
||||
ballVelY = ballVelY * -1
|
||||
end
|
||||
ballDestX = ballX + (ballVelX * hitMult)
|
||||
ballDestY = ballY + (ballVelY * hitMult)
|
||||
ballDestX = ball.x + (ballVelX * HIT_MULT)
|
||||
ballDestY = ball.y + (ballVelY * HIT_MULT)
|
||||
throwBall(ballDestX, ballDestY, playdate.easingFunctions.outQuint, 2000)
|
||||
|
||||
fielders.first.target = bases.first
|
||||
batter.nextBase = bases.first
|
||||
batter.forcedTo = bases.first
|
||||
fielders.first.target = bases[FIRST]
|
||||
batter.nextBase = bases[FIRST]
|
||||
batter.forcedTo = bases[FIRST]
|
||||
batter = nil -- Demote batter to a mere runner
|
||||
|
||||
local chasingFielder = getNearestOf(fielders, ballDestX, ballDestY)
|
||||
|
@ -427,80 +458,133 @@ function updateGameState()
|
|||
chasingFielder.onArrive = function()
|
||||
local targetX, targetY = getNextThrowTarget()
|
||||
if targetX ~= nil then
|
||||
throwBall(targetX, targetY, playdate.easingFunctions.linear)
|
||||
throwBall(targetX, targetY, playdate.easingFunctions.linear, nil, true)
|
||||
chasingFielder.onArrive = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if currentMode == MODES.running then
|
||||
function updateRunning()
|
||||
secondsSincePitchAllowed = 0
|
||||
ball.size = ballSizeAnimator:currentValue()
|
||||
if updateRunners() then
|
||||
secondsSinceLastRunnerMove = 0
|
||||
else
|
||||
secondsSinceLastRunnerMove = secondsSinceLastRunnerMove + deltaTime
|
||||
secondsSinceLastRunnerMove = secondsSinceLastRunnerMove + deltaSeconds
|
||||
if secondsSinceLastRunnerMove > resetFieldersAfterSeconds then
|
||||
throwBall(PITCH_START_X, PITCH_START_Y, playdate.easingFunctions.linear, nil, true)
|
||||
resetFielderPositions(false)
|
||||
currentMode = MODES.batting
|
||||
batter = newRunner()
|
||||
batter.nextBase = bases[FIRST]
|
||||
batter.forcedTo = bases[FIRST]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function updateOutRunners()
|
||||
for i, runner in ipairs(outRunners) do
|
||||
if runner.x < SCREEN.W + 50 and runner.y < SCREEN.H + 50 then
|
||||
runner.x = runner.x + (deltaSeconds * 25)
|
||||
runner.y = runner.y + (deltaSeconds * 25)
|
||||
else
|
||||
table.remove(outRunners, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function updateGameState()
|
||||
deltaSeconds = playdate.getElapsedTime() or 0
|
||||
playdate.resetElapsedTime()
|
||||
elapsedSec = elapsedSec + deltaSeconds
|
||||
crankChange, acceleratedChange = playdate.getCrankChange() --[[@as number, number]]
|
||||
|
||||
ball.x = ballAnimatorX:currentValue()
|
||||
ball.y = ballAnimatorY:currentValue() + ballFloatAnimator:currentValue()
|
||||
|
||||
if currentMode == MODES.batting then
|
||||
updateBatting()
|
||||
elseif currentMode == MODES.running then
|
||||
updateRunning()
|
||||
end
|
||||
|
||||
updateFielders()
|
||||
updateOutRunners()
|
||||
end
|
||||
|
||||
local OUT_BUBBLE_SIZE = 6
|
||||
|
||||
function drawScoreboard()
|
||||
gfx.setDrawOffset(0, 0)
|
||||
local y = SCREEN.H * 0.95
|
||||
local x = SCREEN.W * 0.05
|
||||
|
||||
gfx.setLineWidth(1)
|
||||
gfx.setColor(gfx.kColorBlack)
|
||||
gfx.fillRect(x - 15, y - 44, 73, 55)
|
||||
|
||||
gfx.setColor(gfx.kColorWhite)
|
||||
for i=outs,2 do
|
||||
gfx.drawCircleAtPoint(x + (i * 2.5 * OUT_BUBBLE_SIZE), y, OUT_BUBBLE_SIZE)
|
||||
end
|
||||
for i=0,(outs - 1) do
|
||||
gfx.fillCircleAtPoint(x + (i * 2.5 * OUT_BUBBLE_SIZE), y, OUT_BUBBLE_SIZE)
|
||||
end
|
||||
|
||||
local originalDrawMode = gfx.getImageDrawMode()
|
||||
gfx.setImageDrawMode(playdate.graphics.kDrawModeInverted)
|
||||
gfx.drawText("Home: " .. homeScore, x - 7, y - 40, 100, 20)
|
||||
gfx.drawText("Away: " .. awayScore, x - 7, y - 25, 100, 20)
|
||||
gfx.setImageDrawMode(originalDrawMode)
|
||||
end
|
||||
|
||||
function playdate.update()
|
||||
updateGameState()
|
||||
playdate.graphics.animation.blinker.updateAll()
|
||||
|
||||
-- TODO: Show baserunning minimap when panning?
|
||||
local ballBuffer = 5
|
||||
if ballY < ballBuffer then
|
||||
backgroundPan.y = math.max(ballBuffer, -1 * (ballY - ballBuffer))
|
||||
else
|
||||
backgroundPan.y = 0
|
||||
end
|
||||
if ballX < ballBuffer then
|
||||
backgroundPan.x = math.max(-400, -1 * (ballX - ballBuffer))
|
||||
elseif ballX > (screenW - ballBuffer) then
|
||||
backgroundPan.x = math.min(800, -1 * (ballX - ballBuffer))
|
||||
end
|
||||
|
||||
if ballX > 0 and ballX < (screenW - ballBuffer) then
|
||||
backgroundPan.x = 0
|
||||
end
|
||||
|
||||
gfx.clear()
|
||||
grassBackground:draw(backgroundPan.x - 400, backgroundPan.y - 240)
|
||||
|
||||
if ball.x < BALL_OFFSCREEN then
|
||||
-- TODO: Show baserunning minimap when panning?
|
||||
local offsetX, offsetY = getDrawOffset(SCREEN.W, SCREEN.H, ball.x, ball.y)
|
||||
gfx.setDrawOffset(offsetX, offsetY)
|
||||
end
|
||||
|
||||
grassBackground:draw(-400, -240)
|
||||
|
||||
gfx.setColor(gfx.kColorBlack)
|
||||
gfx.setLineWidth(2)
|
||||
gfx.drawCircleAtPoint(ballX + backgroundPan.x, ballY + backgroundPan.y, ballSize)
|
||||
|
||||
gfx.drawCircleAtPoint(ball.x, ball.y, ball.size)
|
||||
|
||||
local fielderDanceHeight = fielderDanceAnimator:currentValue()
|
||||
for _,fielder in pairs(fielders) do
|
||||
gfx.fillRect(fielder.x + backgroundPan.x, fielder.y + backgroundPan.y, 14, 25)
|
||||
gfx.fillRect(fielder.x, fielder.y - fielderDanceHeight, 14, 25)
|
||||
end
|
||||
|
||||
gfx.setLineWidth(5)
|
||||
|
||||
if currentMode == MODES.batting then
|
||||
gfx.setLineWidth(5)
|
||||
gfx.drawLine(
|
||||
batBaseX + backgroundPan.x, batBaseY + backgroundPan.y,
|
||||
batTipX + backgroundPan.x, batTipY + backgroundPan.y
|
||||
batBase.x, batBase.y,
|
||||
batTip.x, batTip.y
|
||||
)
|
||||
end
|
||||
|
||||
if playdate.isCrankDocked() or (crankChange < 2 and currentMode == MODES.running) then
|
||||
if playdate.isCrankDocked() then -- or (crankChange < 2 and currentMode == MODES.running) then
|
||||
playdate.ui.crankIndicator:draw()
|
||||
end
|
||||
|
||||
-- TODO? Change blip speed depending on runner speed?
|
||||
for _,runner in pairs(runners) do
|
||||
-- TODO? Scale sprites down as y increases
|
||||
playerImageBlipper:draw(
|
||||
false,
|
||||
runner.x + backgroundPan.x,
|
||||
runner.y + backgroundPan.y
|
||||
)
|
||||
playerImageBlipper:draw(false, runner.x, runner.y)
|
||||
end
|
||||
for _,runner in pairs(outRunners) do
|
||||
playerFrown:draw(runner.x, runner.y)
|
||||
end
|
||||
|
||||
drawScoreboard()
|
||||
end
|
||||
|
||||
init()
|
Binary file not shown.
|
@ -1,6 +1,30 @@
|
|||
import 'CoreLibs/animation.lua'
|
||||
import 'CoreLibs/graphics.lua'
|
||||
|
||||
-- number = ((number * 2) - 1)
|
||||
-- return number * number
|
||||
|
||||
-- c = c + 0.0 -- convert to float to prevent integer overflow
|
||||
-- return c * t / d + b
|
||||
|
||||
function easingHill(t, b, c, d)
|
||||
c = c + 0.0 -- convert to float to prevent integer overflow
|
||||
t = t / d
|
||||
t = ((t * 2) - 1)
|
||||
t = t * t
|
||||
return (c * t) + b
|
||||
end
|
||||
|
||||
---@param x number
|
||||
---@param y number
|
||||
---@return XYPair
|
||||
function xy(x, y)
|
||||
return {
|
||||
x = x,
|
||||
y = y
|
||||
}
|
||||
end
|
||||
|
||||
--- Returns the normalized vector as two values, plus the distance between the given points.
|
||||
---@return number, number, number
|
||||
function normalizeVector(x1, y1, x2, y2)
|
||||
|
|
Loading…
Reference in New Issue