import "CoreLibs/keyboard" import "CoreLibs/animation" import "CoreLibs/math" local gfx = playdate.graphics local prevtext = "" local levelname = nil local inverted = false gfx.setLineWidth(5) local tileTable = gfx.imagetable.new("gfx/tiles") local tilemapEditor = gfx.tilemap.new() tilemapEditor:setImageTable(tileTable) local tiles = {} tilemapEditor:setSize(1000,1000) local imgtiles = gfx.sprite.new(tilemapEditor) local w, h = imgtiles:getSize() imgtiles:moveTo(w/2,h/2) local bool2int = {[true]=1,[false]=0} local menu = playdate.getSystemMenu() local toolTipButton local altClrButton local editor = "main" local imgFloppy = gfx.imagetable.new("gfx/floppyT") local imgAdd = gfx.image.new("gfx/add") local imgMus = gfx.image.new("gfx/music") local imgLine = gfx.image.new("gfx/line") local imgSpin = gfx.image.new("gfx/spin") local imgCursor = gfx.image.new("gfx/cursor2") local imgCant = gfx.image.new("gfx/nothing") local imgSquare = gfx.image.new("gfx/square") local imgRocket = gfx.image.new("gfx/start") local imgExit = gfx.image.new("gfx/exit") local imgEye = gfx.image.new("gfx/eye") local imgPlayer = gfx.image.new("gfx/rocket") local imgPlayerExit = gfx.image.new("gfx/bigrocket") local imgB = gfx.image.new("gfx/bButton") local imgExitMen = gfx.image.new("gfx/leave") assert(imgFloppy) assert(imgAdd) assert(imgMus) assert(imgLine) assert(imgSpin) assert(imgCursor) assert(imgSquare) assert(imgRocket) assert(imgEye) assert(imgPlayer) assert(imgPlayerExit) assert(imgB) local animFloppy = gfx.animation.loop.new(100, imgFloppy) local saveTime = 0 local playerPos = {x=1562,y=1564} local exitPos = {x = 1616, y = 1536} local position = {x=100,y=100} local positionLerp = {x=0,y=0} local powers = {} function newProject() mode = "newproj" playdate.keyboard.show("NEW LEVEL") end function updateNewproj() gfx.clear() gfx.drawText("LEVEL NAME:", 0, 105) gfx.drawTextInRect(playdate.keyboard.text:upper(),0,125, 210, 80) end function editLoadName(name) song:stop() playdate.wait(0.1) local data = json.decodeFile("levels/"..name) playdate.wait(0.1) powers = data.fuel playerPos = {x = data.rocket.x, y = data.rocket.y} exitPos = {x = data.bigrocket.x, y = data.bigrocket.y} position = {x = (data.rocket.x + 6) / 16, y = (data.rocket.y + 4)/16} levelname = name:match("(.+)%..+$") inverted = data.inverted playdate.display.setInverted(data.inverted) saveTime = 0 mode = "editor" playdate.wait(0.1) -- loading tiles for tile = 1, #tiles, 1 do tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0) end playdate.wait(0.1) for tile = 1, #data.tiles, 1 do tilemapEditor:setTileAtPosition(data.tiles[tile].x,data.tiles[tile].y,data.tiles[tile].t) tiles[tile] = {x=data.tiles[tile].x,y=data.tiles[tile].y,t=data.tiles[tile].t} end playdate.wait(0.1) altClrButton = menu:addCheckmarkMenuItem("alt colours",inverted, function(value) playdate.display.setInverted(value) inverted = value end) toolTipButton = menu:addMenuItem("save", function() editSave() end) imgtiles:add() end function playdate.keyboard.keyboardWillHideCallback(ok) if ok == false then page = 0 playdate.wait(0.3) editLoad() else song:stop() position = {x=89,y=93} playerPos = {x=1562,y=1564} exitPos = {x = 1616, y = 1536} for tile = 1, #tiles, 1 do tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0) end inverted = false playdate.display.setInverted(false) saveTime = 0 mode = "editor" altClrButton, error = menu:addCheckmarkMenuItem("alt colours",false, function(value) playdate.display.setInverted(value) inverted = value end) toolTipButton, error = menu:addMenuItem("save",false, function() editSave() end) playdate.wait(0.1) levelname = playdate.keyboard.text:upper() imgtiles:add() playdate.wait(0.1) end end function playdate.keyboard.textChangedCallback() if #playdate.keyboard.text > 24 then playdate.keyboard.text = prevtext end prevtext = playdate.keyboard.text end local tileIndex = 0.0 local curY = math.floor( tileIndex ) * 28 + 3 local curYlerp = -32 function editSave() playdate.datastore.delete("levels/"..levelname..".json") saveTime = 5 playdate.datastore.write({tiles = tiles, inverted = inverted, rocket=playerPos, bigrocket=exitPos, fuel = powers,saws={},rotators={}},"levels/"..levelname) end function editUpdate() --print(math.floor( tileIndex ) ) local change, aChange = playdate.getCrankChange() tileIndex += change * 0.01 if editor == "main" then if tileIndex > 7.9 then tileIndex = 0 end if tileIndex < 0 then tileIndex = 7.9 end curY = math.floor( tileIndex ) * 28 +7 elseif editor == "tiles" then if tileIndex < 0 then tileIndex = #tileTable-.1 end if tileIndex > #tileTable then tileIndex = 0 end curY = (math.floor( tileIndex ) % 8) * 28 +7 end curYlerp = playdate.math.lerp(curYlerp, curY, 0.4) positionLerp.x = playdate.math.lerp(positionLerp.x, position.x * 16, 0.5) positionLerp.y = playdate.math.lerp(positionLerp.y, position.y * 16, 0.5) if playdate.buttonJustPressed(playdate.kButtonA) then if editor == "tiles" then if math.floor( tileIndex ) == 0 then editor = "main" else tilemapEditor:setTileAtPosition(position.x+12,position.y+8,math.floor( tileIndex ) + 1) tiles[#tiles+1] = {x=position.x + 12, y=position.y + 8, t=math.floor( tileIndex ) + 1} imgtiles = gfx.sprite.new(tilemapEditor) end elseif editor == "main" then if math.floor( tileIndex ) == 0 then --[[ tilemapEditor:setTileAtPosition(position.x+12,position.y+8,2) tiles[#tiles+1] = {x=position.x + 12, y=position.y + 8, t=2} imgtiles = gfx.sprite.new(tilemapEditor) ]] editor = "tiles" elseif math.floor(tileIndex) == 1 then powers[#powers+1] = {x=((position.x + 11) * 16)-7, y=((position.y + 7) * 16)-5} elseif math.floor(tileIndex) == 4 then playerPos = {x = ((position.x + 11) * 16) - 6, y = ((position.y + 7) * 16) - 4} elseif math.floor(tileIndex) == 5 then exitPos = {x = (position.x + 11) * 16, y = (position.y + 7) * 16} elseif math.floor(tileIndex) == 7 then editor = "view" end end end if playdate.buttonJustPressed(playdate.kButtonB) then if editor == "tiles" then tilemapEditor:setTileAtPosition(position.x+12,position.y+8,0) for tile = 1, #tiles, 1 do if tiles[tile].x == position.x + 12 and tiles[tile].y == position.y+8 then table.remove(tiles,tile) break end end elseif editor == "main" then --[[if math.floor( tileIndex ) == 0 then tilemapEditor:setTileAtPosition(position.x+12,position.y+8,0) for tile = 1, #tiles, 1 do if tiles[tile].x == position.x + 12 and tiles[tile].y == position.y+8 then table.remove(tiles,tile) break end end imgtiles = gfx.sprite.new(tilemapEditor)]] if math.floor(tileIndex) == 1 then for power = 1, #powers, 1 do if powers[power].x == ((position.x + 11) * 16)-7 and powers[power].y == ((position.y + 7) * 16)-5 then table.remove(powers,power) break end end end elseif editor == "view" then editor = "main" tileIndex = 7.5 end end if playdate.buttonJustPressed(playdate.kButtonLeft) then position.x -= 1 elseif playdate.buttonJustPressed(playdate.kButtonRight) then position.x += 1 end if playdate.buttonJustPressed(playdate.kButtonUp) then position.y -= 1 elseif playdate.buttonJustPressed(playdate.kButtonDown) then position.y += 1 end if position.x + 11 < 0 then position.x = -11 end if position.y + 7 < 0 then position.y = -7 end gfx.clear() -- draw map gfx.setDrawOffset(-positionLerp.x, -positionLerp.y) imgtiles:update() imgPlayerExit:draw(exitPos.x, exitPos.y) imgPlayer:draw(playerPos.x,playerPos.y) for power = 1, #powers, 1 do imgSquare:draw(powers[power].x,powers[power].y) end if editor ~= "view" then imgCursor:draw((position.x + 11) * 16, (position.y + 7) * 16) end --[[ gfx.setColor(playdate.graphics.kColorXOR) gfx.fillRect(((position.x + 11) * 16) + 3, ((position.y + 7) * 16) + 3,10,10) ]] if position.y < 1 then imgCant:drawTiled((position.x-1) * 16,-112,400,112) end if position.x < 1 then imgCant:drawTiled(-176,(position.y-1) * 16,176,272) end -- draw ui/ if editor ~= "view" then gfx.setDrawOffset(0,0) gfx.setColor(gfx.kColorBlack) gfx.fillRect(362,0,400,240) gfx.setColor(gfx.kColorWhite) gfx.drawLine(360,0,360,240) -- draw changes if editor == "tiles" then gfx.drawText("A TO PLACE \nB TO DELETE",0,0) local ind = math.floor( tileIndex / 8 ) * 8 if tileIndex < 8 then imgExitMen:draw(366,8) else tileTable[1+ind]:draw(373,13) end tileTable[2+ind]:draw(373,41) tileTable[3+ind]:draw(373,69) tileTable[4+ind]:draw(373,97) tileTable[5+ind]:draw(373,125) tileTable[6+ind]:draw(373,153) tileTable[7+ind]:draw(373,181) tileTable[8+ind]:draw(373,209) elseif editor == "main" then imgMus:draw(366, 176) imgAdd:draw(366,8) imgLine:draw(366,64) imgSpin:draw(366,92) imgSquare:draw(366,36) imgRocket:draw(366, 120) imgExit:draw(366,148) imgEye:draw(366,204) if math.floor( tileIndex ) == 0 then gfx.drawText("TILES",0,0) elseif math.floor( tileIndex ) == 1 then gfx.drawText("POWER",0,0) elseif math.floor( tileIndex ) == 2 then gfx.drawText("MOVERS",0,0) elseif math.floor( tileIndex ) == 3 then gfx.drawText("SPINNERS",0,0) elseif math.floor( tileIndex ) == 4 then gfx.drawText("PLAYER",0,0) elseif math.floor( tileIndex ) == 5 then gfx.drawText("EXIT",0,0) elseif math.floor( tileIndex ) == 6 then gfx.drawText("MUSIC",0,0) elseif math.floor( tileIndex ) == 7 then gfx.drawText("VIEW",0,0) end end gfx.setColor(playdate.graphics.kColorXOR) gfx.fillRect(365,curYlerp,33,28) end if saveTime > 0 then saveTime -= 0.1 animFloppy:draw(2,210) end end function editClose() for tile = 1, #tiles, 1 do tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0) tiles[tile] = nil end powers = {} editor = "main" tileIndex = 0 playdate.display.setInverted(false) imgtiles = gfx.sprite.new(tilemapEditor) imgtiles:remove() playdate.getSystemMenu():removeMenuItem(toolTipButton) playdate.getSystemMenu():removeMenuItem(altClrButton) end