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

158 lines
No EOL
4.2 KiB
Lua

import "CoreLibs/keyboard"
import "CoreLibs/math"
local gfx <const> = playdate.graphics
local prevtext = ""
gfx.setLineWidth(5)
local tileTable <const> = gfx.imagetable.new("gfx/tiles")
local tilemapEditor = gfx.tilemap.new()
tilemapEditor:setImageTable(tileTable)
local tiles = {}
tilemapEditor:setSize(10,10)
local imgtiles = gfx.sprite.new(tilemapEditor)
local w, h = imgtiles:getSize()
imgtiles:moveTo(w/2,h/2)
local position = {x=0,y=0}
local positionLerp = {x=0,y=0}
local bool2int = {[true]=1,[false]=0}
local menu = playdate.getSystemMenu()
local toolTipButton, error
local editor = "main"
local imgSwitch = gfx.imagetable.new("gfx/switch")
local imgFloppy = gfx.image.new("gfx/floppy")
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")
assert(imgSwitch)
assert(imgFloppy)
assert(imgAdd)
assert(imgMus)
assert(imgLine)
assert(imgSpin)
assert(imgCursor)
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 playdate.keyboard.keyboardWillHideCallback(ok)
if ok == false then
page = 0
playdate.wait(0.3)
editLoad()
else
mode = "editor"
toolTipButton, error = menu:addCheckmarkMenuItem("show help",true, function(value)
print(value)
end)
imgtiles:add()
menuButton:setTitle("save & quit")
playdate.wait(0.1)
end
end
function playdate.keyboard.textChangedCallback()
print(#playdate.keyboard.text)
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 editUpdate()
local change, aChange = playdate.getCrankChange()
tileIndex += change * 0.01
if tileIndex > 7.9 then tileIndex = 0 end
if tileIndex < 0 then tileIndex = 7.9 end
curY = math.floor( tileIndex ) * 28 +7
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 math.floor( tileIndex ) == 1 then
playdate.display.setInverted(not playdate.display.getInverted())
elseif math.floor( tileIndex ) == 3 then
tilemapEditor:setTileAtPosition(position.x+12,position.y+8,2)
tiles[#tiles+1] = {x=position.x + 12, y=position.y + 8, 2}
imgtiles = gfx.sprite.new(tilemapEditor)
w, h = imgtiles:getSize()
imgtiles:moveTo(w/2,h/2)
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
gfx.clear()
-- draw map
gfx.setDrawOffset(-positionLerp.x, -positionLerp.y)
imgtiles:update()
imgCursor:draw((position.x + 11) * 16, (position.y + 7) * 16)
-- draw ui/
gfx.setDrawOffset(0,0)
gfx.setColor(gfx.kColorBlack)
gfx.fillRect(365,0,400,240)
gfx.setColor(gfx.kColorWhite)
gfx.drawLine(360,0,360,240)
-- draw changes
if editor == "main" then
imgMus:draw(366, 64)
imgAdd:draw(366,93)
imgSwitch:getImage(bool2int[playdate.display.getInverted()] + 1):draw(366,36)
imgFloppy:draw(366,8)
imgLine:draw(366,120)
imgSpin:draw(366,148)
end
gfx.setColor(playdate.graphics.kColorXOR)
gfx.fillRect(365,curYlerp,33,28)
end
function editClose()
for tile = 1, #tiles, 1 do
tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0)
end
imgtiles:remove()
playdate.getSystemMenu():removeMenuItem(toolTipButton)
menuButton:setTitle("game menu")
end