local gfx = playdate.graphics local controlY = 0 local controlX = 0 local sidebar = 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() mainmenu = {} mainmenu[4],mainmenu[3],mainmenu[2], mainmenu[1], mainmenu[0] = "START NEW GAME", "USER LEVELS", "LEVEL EDITOR", "ABOUT THE CREATOR", "mainNoPlay" if playdate.datastore.read("savegame") then map = playdate.datastore.read("savegame").savedLevel deaths = playdate.datastore.read("savegame").savedDeaths mainmenu[5], mainmenu[0] = "CONTINUE", "mainPlay" end end function createMenu(items) killBlades() killPlayer() controlX = -80 index = 0 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) end local function customLoad() barpos = 160 logopos = -112 local lvls = playdate.file.listFiles("levels") local m = {} m[0], m[1] = "customs", "BACK" 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", "BACK", "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 = "levels/level.rocketbyte" addMapSave("levels/level.rocketbyte") elseif name == "CONTINUE" then addMap(map) mode = "game" elseif name == "ABOUT THE CREATOR" then local m = {} m[0],m[1] = "about", "BACK" createMenu(m) elseif name == "USER LEVELS" then page = 0 customLoad() elseif name == "LEVEL EDITOR" then page = 0 editLoad() 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" then createMenu(mainmenu) elseif menu == "customs" then addMap("levels/"..name, true) mode = "play" end end