Rocket-Bytes/Source/Menu.lua
2022-04-26 20:40:39 -06:00

167 lines
No EOL
4.2 KiB
Lua

local gfx <const> = playdate.graphics
local controlY = 0
local controlX = 0
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
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" then
song:stop()
song:load("sfx/song1")
song:play(0)
end
mode = "menu"
end
function updateMenu()
gfx.clear(gfx.kColorBlack)
-- 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)
for i = 1, #menuitems, 1 do
local item = menuitems[i]
gfx.drawText(item.name,controlX,item.y)
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(0,0)
end
local function customLoad()
local lvls = playdate.file.listFiles("levels")
local m = {}
m[0], m[1] = "customs", "BACK"
if #lvls - ((7*page)) <= 0 then page = 0 end
if #lvls < 7 then
for i = 1, #lvls, 1 do
m[i+1] = lvls[i]:upper()
end
elseif lvls[1 + (7*page)] then
m[2] = "NEXT PAGE"
for i = 1, 7, 1 do
if lvls[i+(7*page)] then
m[i+2] = lvls[i+(7*page)]:upper()
end
end
end
createMenu(m)
index = #lvls
end
function editLoad()
local lvls = playdate.file.listFiles("levels")
local m = {}
m[0], m[1], m[2] = "edits", "BACK", "CREATE NEW"
if #lvls - ((6*page)) <= 0 then page = 0 end
if #lvls < 6 then
for i = 1, #lvls, 1 do
m[i+2] = lvls[i]:upper()
end
elseif lvls[1 + (6*page)] then
m[2] = "NEXT PAGE"
for i = 1, 6, 1 do
if lvls[i+(6*page)] then
m[i+3] = lvls[i+(6*page)]:upper()
end
end
end
createMenu(m)
index = #lvls
end
function menuButtonPress(name)
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
addMapSave("levels/level.json")
elseif name == "CONTINUE" then
addMap(map)
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)
end
end