diff --git a/Makefile b/Makefile index bdd74f7..a56fb5b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -lint: - stylua src/ - all: pdc src BatterUp.pdx +lint: + stylua src/ + cat <(sed 's/^function/-- selene: allow(unused_variable)\nfunction/' ${PLAYDATE_SDK_PATH}/CoreLibs/__stub.lua) src/utils.lua src/graphics.lua src/main.lua | grep -v '^import' | selene - diff --git a/src/graphics.lua b/src/graphics.lua index 8421c22..81e0c77 100644 --- a/src/graphics.lua +++ b/src/graphics.lua @@ -4,7 +4,7 @@ local ballBuffer = 5 --- XXX --- XOX --- Where each character is the size of the screen, and 'O' is the default view. -function getDrawOffset(screenW, screenH, ballX, ballY) +function getDrawOffset(screenW, _screenH, ballX, ballY) local offsetX, offsetY if ballY < ballBuffer then offsetY = math.max(ballBuffer, -1 * (ballY - ballBuffer)) @@ -20,3 +20,22 @@ function getDrawOffset(screenW, screenH, ballX, ballY) end return offsetX, offsetY end + +-- selene: allow(unscoped_variables) +blipper = {} + +--- Build an object that simply "blips" between the given images at the given interval. +--- Expects `playdate.graphics.animation.blinker.updateAll()` to be called on every update. +function blipper.new(msInterval, imagePath1, imagePath2) + local blinker = playdate.graphics.animation.blinker.new(msInterval, msInterval, true) + blinker:start() + return { + blinker = blinker, + image1 = playdate.graphics.image.new(imagePath1), + image2 = playdate.graphics.image.new(imagePath2), + draw = function(self, disableBlipping, x, y) + local currentImage = (disableBlipping or self.blinker.on) and self.image2 or self.image1 + currentImage:draw(x, y) + end, + } +end diff --git a/src/main.lua b/src/main.lua index a148ce6..2013aa9 100644 --- a/src/main.lua +++ b/src/main.lua @@ -171,6 +171,7 @@ local batter = newRunner() batter.nextBase = bases[FIRST] batter.forcedTo = bases[FIRST] +--- "Throws" the ball from its current position to the given destination. function throwBall(destX, destY, easingFunc, flyTimeMs, floaty) if not flyTimeMs then flyTimeMs = distanceBetween(ball.x, ball.y, destX, destY) * 5 @@ -215,6 +216,7 @@ end local elapsedSec = 0 local crankChange = 0 +local acceleratedChange local BASE_HITBOX = 13 --- Returns the base being touched by the runner at (x,y), or nil, if no base is being touched @@ -249,6 +251,14 @@ function outRunner(runnerIndex) fielderDanceAnimator:reset() end +-- TODO: Away score +function score(runnerIndex) + outRunners[#outRunners + 1] = runners[runnerIndex] + table.remove(runners, runnerIndex) + updateForcedTos() + homeScore = homeScore + 1 +end + function updateFielders() local touchingBaseCache = buildCache(function(runner) return isTouchingBase(runner.x, runner.y) @@ -301,8 +311,11 @@ function updateRunners() end) local runnerMoved = false - for _, runner in pairs(nonPlayerRunners) do - local _, nearestBaseDistance = getNearestOf(bases, runner.x, runner.y) + for runnerIndex, runner in ipairs(nonPlayerRunners) do + local nearestBase, nearestBaseDistance = getNearestOf(bases, runner.x, runner.y) + if nearestBaseDistance < 5 and runner.nextBase == bases[HOME] and nearestBase == bases[HOME] then + score(runnerIndex) + end if runner.nextBase then local nb = runner.nextBase local x, y, distance = normalizeVector(runner.x, runner.y, nb.x, nb.y) @@ -329,16 +342,6 @@ function updateRunners() return runnerMoved end ----@return boolean -function ballIsBeingThrown() - return false -end - ----@return boolean -function throwArrivedBeforeRunner() - return false -end - function getRunnerTargeting(base) for _, runner in pairs(runners) do if runner.nextBase == base then @@ -368,14 +371,11 @@ end function getBaseOfStrandedRunner() local farRunnersBase, farDistance for _, runner in pairs(runners) do - local base, distance = getNearestOf(bases, runner.x, runner.y, function(base) + local nearestBase, distance = getNearestOf(bases, runner.x, runner.y, function(base) return runner.nextBase == base end) - if farRunnersBase == nil then - farRunnersBase = base - farDistance = distance - elseif farDistance < distance then - farRunnersBase = base + if farRunnersBase == nil or farDistance < distance then + farRunnersBase = nearestBase farDistance = distance end end @@ -432,17 +432,17 @@ function updateBatting() then batCrackSound:play() currentMode = MODES.running - ballAngle = batAngle + math.rad(90) + local ballAngle = batAngle + math.rad(90) - local mult = math.abs(acceleratedChange / 15) + local mult = math.abs(crankChange / 15) local ballVelX = mult * 10 * math.sin(ballAngle) local ballVelY = mult * 5 * math.cos(ballAngle) if ballVelY > 0 then ballVelX = ballVelX * -1 ballVelY = ballVelY * -1 end - ballDestX = ball.x + (ballVelX * HIT_MULT) - ballDestY = ball.y + (ballVelY * HIT_MULT) + local ballDestX = ball.x + (ballVelX * HIT_MULT) + local ballDestY = ball.y + (ballVelY * HIT_MULT) throwBall(ballDestX, ballDestY, playdate.easingFunctions.outQuint, 2000) fielders.first.target = bases[FIRST] diff --git a/src/utils.lua b/src/utils.lua index 5a693c3..712cc11 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -48,19 +48,6 @@ function filter(array, condition) return newArray end ----@generic TIn ----@generic TOut ----@param array TIn[] ----@param mapper fun(TIn): TOut ----@return TOut[] -function map(array, mapper) - local newArray = {} - for _, element in pairs(array) do - newArray[#newArray + 1] = mapper(element) - end - return newArray -end - ---@return number, number, number function distanceBetween(x1, y1, x2, y2) local a = x1 - x2 @@ -133,21 +120,3 @@ function buildCache(fetcher) end, } end - -blipper = {} - ---- Build an object that simply "blips" between the given images at the given interval. ---- Expects `playdate.graphics.animation.blinker.updateAll()` to be called on every update. -function blipper.new(msInterval, imagePath1, imagePath2) - local blinker = playdate.graphics.animation.blinker.new(msInterval, msInterval, true) - blinker:start() - return { - blinker = blinker, - image1 = playdate.graphics.image.new(imagePath1), - image2 = playdate.graphics.image.new(imagePath2), - draw = function(self, disableBlipping, x, y) - local currentImage = (disableBlipping or self.blinker.on) and self.image2 or self.image1 - currentImage:draw(x, y) - end, - } -end