fpv/source/scenes/Assemble.lua

237 lines
6.5 KiB
Lua
Raw Permalink Normal View History

2024-06-04 21:21:40 +03:00
Assemble = {}
class("Assemble").extends(BaseScene)
local scene = Assemble
local font = Graphics.font.new('assets/fonts/Mini Sans 2X')
2024-06-10 01:50:27 +03:00
local fontMed = Graphics.font.new('assets/fonts/onyx_9')
2024-06-04 21:21:40 +03:00
local allButtons = { "A", "B", "DOWN", "LEFT", "RIGHT", "UP" }
function scene:popCode(button)
2024-06-12 01:22:12 +03:00
if #scene.code == 0 then
return
end
2024-06-10 01:50:27 +03:00
scene.menuConfirmSound:stop()
2024-06-04 21:21:40 +03:00
if scene.tickTimer.paused then
scene.droneParts = scene:loadDrone(1, #scene.code)
scene.tickTimer:start()
scene.progressBar:setVisible(true)
end
if button == scene.code[1] then
table.remove(scene.code, 1)
scene.tickTimer:reset()
scene.dronePartIndex = scene.dronePartIndex + 1
2024-06-10 01:50:27 +03:00
local particle = ParticlePoly(200, 185)
2024-06-04 21:21:40 +03:00
particle:setThickness(2)
particle:setSize(1, 2)
particle:setSpeed(1, 3)
particle:setColour(Graphics.kColorXOR)
particle:add(5)
2024-06-10 01:50:27 +03:00
scene.dY = 5
scene.menuConfirmSound:play(1)
2024-06-04 21:21:40 +03:00
else
self.buttonTimeout = self.buttonTimeout - 5
scene.code[1] = scene:randomButton()
screenShake(100, 5)
end
end
scene.inputHandler = {
2024-06-12 01:22:12 +03:00
AButtonDown = function()
if #scene.code == 0 then
scene.menuConfirmSound:play(1)
Noble.transition(Game, nil, Noble.Transition.SpotlightMask)
else
scene:popCode("A")
end
end,
2024-06-04 21:21:40 +03:00
BButtonDown = function() scene:popCode("B") end,
downButtonDown = function() scene:popCode("DOWN") end,
leftButtonDown = function() scene:popCode("LEFT") end,
rightButtonDown = function() scene:popCode("RIGHT") end,
upButtonDown = function() scene:popCode("UP") end,
}
function scene:randomButton()
local button = allButtons[math.random(1, #allButtons)]
return button
end
function scene:drawBackground()
end
local screwDriver = Graphics.image.new('assets/sprites/assemble/sd.png')
2024-06-10 01:50:27 +03:00
local solder = Graphics.image.new('assets/images/solder.png')
2024-06-04 21:21:40 +03:00
function scene:loadButtons()
local buttons = {}
for i = 1, #allButtons do
local image = Graphics.image.new('assets/sprites/buttons/' .. allButtons[i] .. '.png')
buttons[allButtons[i]] = image
end
return buttons
end
function scene:loadDrone(id, parts)
local drone = {}
for i = 1, parts do
local image = Graphics.image.new('assets/sprites/assemble/' .. id .. '/' .. i .. '.png')
if image ~= nil then
table.insert(drone, image)
end
end
return drone
end
function scene:setValues()
scene.code = {}
for i = 1, 9 do
table.insert(scene.code, scene:randomButton())
end
2024-06-10 01:50:27 +03:00
scene.difficulty = Noble.Settings.get("difficulty")
2024-06-04 21:21:40 +03:00
scene.buttons = scene:loadButtons()
scene.droneParts = {}
scene.dronePartIndex = 0
2024-06-10 01:50:27 +03:00
scene.dY = 5
scene.buttonTimeout = 100
scene.timeToClick = DifficultySettings[scene.difficulty].assebleTime
scene.menuConfirmSound = playdate.sound.fileplayer.new("assets/audio/confirm")
self.aKey = Graphics.image.new("assets/sprites/buttons/A")
2024-06-12 01:22:12 +03:00
scene.musicEnabled = Noble.Settings.get("music")
scene.levelAudio = playdate.sound.fileplayer.new("assets/audio/assemble")
scene.levelAudio:setVolume(0.7)
2024-06-04 21:21:40 +03:00
end
function scene:init()
scene.super.init(self)
scene:setValues()
2024-06-10 01:50:27 +03:00
scene.progressBar = ProgressBar(200, 175, 50, 5)
2024-06-04 21:21:40 +03:00
scene.progressBar:set(self.buttonTimeout)
scene.progressBar:setVisible(false)
2024-06-10 01:50:27 +03:00
scene.tickTimer = playdate.timer.new(scene.timeToClick, self.buttonTimeout, 0, playdate.easingFunctions.linear)
2024-06-04 21:21:40 +03:00
scene.tickTimer.updateCallback = function(timer)
scene.buttonTimeout = timer.value
scene.progressBar:set(scene.buttonTimeout)
end
scene.tickTimer.timerEndedCallback = function()
scene.buttonTimeout = 0
end
scene.tickTimer:pause()
end
function scene:start()
scene.super.start(self)
self.optionsMenu:addMenuItem("Main Menu", function() Noble.transition(Menu) end)
2024-06-12 01:22:12 +03:00
Noble.showFPS = false
2024-06-04 21:21:40 +03:00
end
function scene:enter()
scene.super.enter(self)
2024-06-12 01:22:12 +03:00
scene.buttonTimeout = 100
Noble.Input.setHandler(scene.inputHandler)
if scene.musicEnabled then
scene.levelAudio:play(0)
end
2024-06-04 21:21:40 +03:00
end
function round(number)
local formatted = string.format("%.2f", number)
return formatted
end
2024-06-10 01:50:27 +03:00
local elapsedTime = 0
2024-06-04 21:21:40 +03:00
function scene:update()
scene.super.update(self)
2024-06-10 01:50:27 +03:00
elapsedTime = elapsedTime + 1 / playdate.display.getRefreshRate()
local sddy = 4 * math.sin(10 * elapsedTime)
local sdy = 4 * math.sin(7 * elapsedTime)
local sddx = 2 * math.cos(5 * elapsedTime)
if scene.tickTimer.paused then
Noble.Text.draw("Assemble the drone!", 200, 110, Noble.Text.ALIGN_CENTER, false, font)
end
2024-06-04 21:21:40 +03:00
if #scene.code == 0 then
2024-06-10 01:50:27 +03:00
local dy = 1 * math.sin(10 * elapsedTime)
Noble.Transition.setDefaultProperties(Noble.Transition.SpotlightMask, {
x = 325,
y = 95,
xEnd = 96,
yEnd = 100,
invert = false
})
self.aKey:draw(200, 170 + dy)
Noble.Text.draw("Start Mission", 218, 175, Noble.Text.ALIGN_LEFT, false, fontMed)
2024-06-04 21:21:40 +03:00
self.progressBar:remove()
self.tickTimer:remove()
return
end
if scene.buttonTimeout <= 0 then
Noble.Text.draw("LOSE!", 200, 110, Noble.Text.ALIGN_CENTER, false, font)
self.progressBar:remove()
self.tickTimer:remove()
screenShake(100, 5)
2024-06-10 01:50:27 +03:00
Noble.Input.setEnabled(false)
2024-06-04 21:21:40 +03:00
return
end
2024-06-10 01:50:27 +03:00
screwDriver:draw(300 + sddx, 100 + sddy)
solder:draw(0, 100 + sdy)
2024-06-04 21:21:40 +03:00
if scene.droneParts ~= nil and scene.dronePartIndex ~= nil and scene.dronePartIndex > 0 and #scene.droneParts > 0 then
scene.droneParts[scene.dronePartIndex]:draw(100, 20)
end
2024-06-10 01:50:27 +03:00
Graphics.drawLine(180, 185, 192, 185)
Graphics.drawLine(208, 185, 220, 185)
if scene.dY > 0 then
scene.dY = self.dY - 0.5
end
2024-06-04 21:21:40 +03:00
for i = 1, #scene.code do
local button = scene.buttons[scene.code[i]]
local first = 1
if #scene.code == 1 then
first = 0
end
local t = (i - 1) / (#scene.code - first)
local opaque = playdate.math.lerp(1, 0.3, t) --TODO: not the best solution
2024-06-10 01:50:27 +03:00
local y = 185 + ((i - 1) * 16) - 8 + scene.dY
button:drawFaded(192, y, opaque, playdate.graphics.image.kDitherTypeBayer2x2)
2024-06-04 21:21:40 +03:00
end
end
function scene:exit()
scene.super.exit(self)
2024-06-12 01:22:12 +03:00
scene.levelAudio:stop()
2024-06-04 21:21:40 +03:00
Noble.showFPS = false
end
function scene:finish()
scene.super.finish(self)
playdate.display.setScale(1)
end