Rocket-Bytes/Source/Menu.lua
2022-05-21 20:06:58 -06:00

237 lines
No EOL
6 KiB
Lua

local gfx <const> = playdate.graphics
local controlY = 0
local controlX = 0
local sidebar <const> = gfx.image.new("gfx/sidebar")
local barpos = 0
local barposLerp = 160
local logopos = 0
local logposLerp = -122
local index = 0
local menuitems = {}
local menu = null
page = 0
local imgCursor = gfx.image.new("gfx/cursor")
local imgBytes = gfx.image.new("gfx/bytes")
assert(imgCursor)
assert(imgBytes)
mainmenu = {}
function mainMenuCreation()
barposLerp = 160
logposLerp = -122
mainmenu = {}
mainmenu[4],mainmenu[3],mainmenu[2], mainmenu[1], mainmenu[0] = "START NEW GAME", "USER LEVELS", "LEVEL EDITOR", "EXTRAS", "mainNoPlay"
local g = playdate.datastore.read("savegame")
if g then
map = g.savedLevel
deaths = g.savedDeaths
mainmenu[5], mainmenu[0] = "CONTINUE", "mainPlay"
end
end
function createMenu(items, invert)
song:setRate(1.0)
killBlades()
killPlayer()
if invert then playdate.display.setInverted(true) else playdate.display.setInverted(false) end
controlX = -80
index = 0.5
menuitems = {}
local _y =220
menu = items[0]
for i=1, #items, 1 do
menuitems[i] = {name=items[i],y=_y}
_y -= 20
end
if mode == "game" or mode == "play" then
song:stop()
song:load("sfx/song1")
song:play(0)
end
mode = "menu"
end
function updateMenu()
gfx.clear(gfx.kColorBlack)
sidebar:draw(barposLerp,0)
-- input
local change, aChange = playdate.getCrankChange()
if playdate.buttonJustPressed(playdate.kButtonUp) then
index -= 1
elseif playdate.buttonJustPressed(playdate.kButtonDown) then
index += 1
end
index += change * 0.01
if math.floor(index) < 0 then index = #menuitems -0.01 end
if math.floor(index) > #menuitems - 1 then index = 0 end
controlX = playdate.math.lerp(controlX, 20, 0.3)
controlY = playdate.math.lerp(controlY, (20 * math.floor(index)) - 1, 0.5)
barposLerp = playdate.math.lerp(barposLerp, barpos, 0.2)
logposLerp = playdate.math.lerp(logposLerp, logopos, 0.3)
for i = 1, #menuitems, 1 do
local item = menuitems[i]
if item.name:match("(.+)%..+$") then
gfx.drawText(item.name:match("(.+)%..+$"),controlX,item.y)
else
gfx.drawText(item.name,controlX,item.y)
end
end
imgCursor:draw(controlX - 17,controlY + (243 - (20 * #menuitems)))
if playdate.buttonJustPressed(playdate.kButtonA) or playdate.buttonJustPressed(playdate.kButtonRight) then
menuButtonPress(menuitems[#menuitems - math.floor(index)].name)
elseif playdate.buttonJustPressed(playdate.kButtonB) or playdate.buttonJustPressed(playdate.kButtonLeft) then
menuButtonPress("BACK")
end
imgBytes:draw(logposLerp,0)
if dev then
small:drawText(index, 0, 209)
end
end
local function dataLoad()
barpos = 160
logopos = -112
local lvls = playdate.file.listFiles("data")
local m = {}
m[0], m[1] = "data", "EXTRAS"
if #lvls - ((10*page)) <= 0 then page = 0 end
if #lvls <= 11 then
for i = 1, #lvls, 1 do
m[i+1] = lvls[i]:upper()
end
elseif lvls[1 + (10*page)] then
m[2] = "NEXT PAGE"
for i = 1, 10, 1 do
if lvls[i+(10*page)] then
m[i+2] = lvls[i+(10*page)]:upper()
end
end
end
createMenu(m)
end
local function customLoad()
barpos = 160
logopos = -112
local lvls = playdate.file.listFiles("levels")
local m = {}
m[0], m[1] = "customs", "MENU"
if #lvls - ((10*page)) <= 0 then page = 0 end
if #lvls <= 11 then
for i = 1, #lvls, 1 do
m[i+1] = lvls[i]:upper()
end
elseif lvls[1 + (10*page)] then
m[2] = "NEXT PAGE"
for i = 1, 10, 1 do
if lvls[i+(10*page)] then
m[i+2] = lvls[i+(10*page)]:upper()
end
end
end
createMenu(m)
end
function editLoad()
logopos = -112
barpos = 160
local lvls = playdate.file.listFiles("levels")
local m = {}
m[0], m[1], m[2] = "edits", "MENU", "CREATE NEW"
if #lvls - (1 + (9*page)) <= 0 then page = 0 end
if #lvls <= 10 then
for i = 1, #lvls, 1 do
m[i+2] = lvls[i]:upper()
end
elseif lvls[1 + (9*page)] then
m[3] = "NEXT PAGE"
for i = 1, 9, 1 do
if lvls[i+(9*page)] then
m[i+3] = lvls[i+(9*page)]:upper()
end
end
end
createMenu(m)
end
function menuButtonPress(name)
logopos = 0
barpos = 0
if name == "START NEW GAME" and menu == "mainPlay" then
local m = {}
m[0], m[1], m[2] = "newgame", "CANCEL", "CONFIRM"
createMenu(m)
elseif name == "START NEW GAME" or name == "CONFIRM" then
deaths = 0
map = "data/level.rocketbyte"
addMapSave("data/level.rocketbyte")
elseif name == "CONTINUE" then
addMap(map)
mode = "game"
elseif name == "EXTRAS" then
local m = {}
--barpos = 160
--logopos = -112
m[0],m[1], m[2], m[3], m[4], m[5] = "extras", "MENU", "MUSIC BOX", "END CUTSCENE", "START CUTSCENE", "LEVEL SELECT"
if playdate.file.exists("bonusLevels.rocketbytes") then
m[6] = "EXTRA LEVELS"
end
createMenu(m)
elseif name == "USER LEVELS" then
page = 0
customLoad()
elseif name == "LEVEL EDITOR" then
page = 0
editLoad()
elseif name == "LEVEL SELECT" then
dataLoad()
elseif name == "CREATE NEW" then
newProject()
elseif name == "NEXT PAGE" then
page += 1
if menu == "customs" then
customLoad()
else
editLoad()
end
elseif name == "BACK" or name == "CANCEL" or name == "MENU" then
createMenu(mainmenu)
elseif menu == "customs" then
addMap("levels/"..name, true)
mode = "play"
elseif menu == "data" then
addMap("data/"..name, true)
mode = "play"
end
end