2022-04-27 02:40:39 +00:00
|
|
|
import "CoreLibs/keyboard"
|
2022-05-22 17:27:01 +00:00
|
|
|
import "CoreLibs/animation"
|
2022-05-20 17:01:59 +00:00
|
|
|
import "CoreLibs/math"
|
2022-04-27 02:40:39 +00:00
|
|
|
|
|
|
|
local gfx <const> = playdate.graphics
|
2022-05-08 05:40:21 +00:00
|
|
|
local prevtext = ""
|
2022-05-22 17:27:01 +00:00
|
|
|
local levelname = nil
|
|
|
|
local inverted = false
|
2022-05-20 17:01:59 +00:00
|
|
|
gfx.setLineWidth(5)
|
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
local tileTable <const> = gfx.imagetable.new("gfx/tiles")
|
|
|
|
local tilemapEditor = gfx.tilemap.new()
|
|
|
|
tilemapEditor:setImageTable(tileTable)
|
|
|
|
local tiles = {}
|
|
|
|
|
2022-05-22 20:40:42 +00:00
|
|
|
tilemapEditor:setSize(1000,1000)
|
2022-05-22 02:06:58 +00:00
|
|
|
local imgtiles = gfx.sprite.new(tilemapEditor)
|
|
|
|
local w, h = imgtiles:getSize()
|
|
|
|
imgtiles:moveTo(w/2,h/2)
|
|
|
|
|
2022-05-22 17:27:01 +00:00
|
|
|
local position = {x=100,y=100}
|
2022-05-22 02:06:58 +00:00
|
|
|
local positionLerp = {x=0,y=0}
|
2022-05-20 17:01:59 +00:00
|
|
|
|
|
|
|
local bool2int = {[true]=1,[false]=0}
|
2022-05-22 02:06:58 +00:00
|
|
|
local menu = playdate.getSystemMenu()
|
2022-05-22 20:40:42 +00:00
|
|
|
local toolTipButton
|
|
|
|
local altClrButton
|
2022-05-22 02:06:58 +00:00
|
|
|
|
|
|
|
local editor = "main"
|
2022-05-20 17:01:59 +00:00
|
|
|
|
2022-05-22 17:27:01 +00:00
|
|
|
local imgFloppy = gfx.imagetable.new("gfx/floppyT")
|
2022-05-22 02:06:58 +00:00
|
|
|
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")
|
2022-05-22 17:27:01 +00:00
|
|
|
local imgCant = gfx.image.new("gfx/nothing")
|
2022-05-20 17:01:59 +00:00
|
|
|
assert(imgSwitch)
|
|
|
|
assert(imgFloppy)
|
2022-05-22 02:06:58 +00:00
|
|
|
assert(imgAdd)
|
|
|
|
assert(imgMus)
|
|
|
|
assert(imgLine)
|
|
|
|
assert(imgSpin)
|
|
|
|
assert(imgCursor)
|
2022-04-27 02:40:39 +00:00
|
|
|
|
2022-05-22 17:27:01 +00:00
|
|
|
local animFloppy = gfx.animation.loop.new(100, imgFloppy)
|
|
|
|
|
|
|
|
local saveTime = 0
|
|
|
|
|
2022-04-27 02:40:39 +00:00
|
|
|
function newProject()
|
|
|
|
mode = "newproj"
|
2022-05-08 05:40:21 +00:00
|
|
|
playdate.keyboard.show("NEW LEVEL")
|
2022-04-27 02:40:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function updateNewproj()
|
|
|
|
gfx.clear()
|
|
|
|
gfx.drawText("LEVEL NAME:", 0, 105)
|
2022-05-08 05:40:21 +00:00
|
|
|
gfx.drawTextInRect(playdate.keyboard.text:upper(),0,125, 210, 80)
|
2022-04-27 02:40:39 +00:00
|
|
|
end
|
|
|
|
|
2022-05-22 20:40:42 +00:00
|
|
|
function editLoadName(name)
|
|
|
|
position = {x=100,y=100}
|
|
|
|
local data = json.decodeFile("levels/"..name)
|
|
|
|
levelname = name:match("(.+)%..+$")
|
|
|
|
inverted = data.inverted
|
|
|
|
printTable(data)
|
|
|
|
playdate.display.setInverted(data.inverted)
|
|
|
|
saveTime = 0
|
|
|
|
mode = "editor"
|
|
|
|
|
|
|
|
-- loading tiles
|
|
|
|
for tile = 1, #tiles, 1 do
|
|
|
|
tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
toolTipButton = menu:addMenuItem("save",data.inverted, function()
|
|
|
|
editSave()
|
|
|
|
end)
|
|
|
|
altClrButton = menu:addCheckmarkMenuItem("alt colours",inverted, function(value)
|
|
|
|
playdate.display.setInverted(value)
|
|
|
|
inverted = value
|
|
|
|
end)
|
|
|
|
imgtiles:add()
|
|
|
|
menuButton:setTitle("save & quit")
|
|
|
|
playdate.wait(0.1)
|
|
|
|
end
|
|
|
|
|
2022-04-27 02:40:39 +00:00
|
|
|
function playdate.keyboard.keyboardWillHideCallback(ok)
|
|
|
|
if ok == false then
|
|
|
|
page = 0
|
|
|
|
playdate.wait(0.3)
|
|
|
|
editLoad()
|
2022-05-20 17:01:59 +00:00
|
|
|
else
|
2022-05-22 20:40:42 +00:00
|
|
|
position = {x=100,y=100}
|
|
|
|
for tile = 1, #tiles, 1 do
|
|
|
|
tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0)
|
|
|
|
end
|
|
|
|
inverted = false
|
|
|
|
playdate.display.setInverted(false)
|
|
|
|
saveTime = 0
|
2022-05-20 17:01:59 +00:00
|
|
|
mode = "editor"
|
2022-05-22 17:27:01 +00:00
|
|
|
toolTipButton, error = menu:addMenuItem("save",false, function()
|
|
|
|
editSave()
|
2022-05-22 02:06:58 +00:00
|
|
|
end)
|
2022-05-22 20:40:42 +00:00
|
|
|
playdate.wait(0.1)
|
|
|
|
altClrButton, error = menu:addCheckmarkMenuItem("alt colours",false, function(value)
|
2022-05-22 17:27:01 +00:00
|
|
|
playdate.display.setInverted(value)
|
|
|
|
end)
|
|
|
|
levelname = playdate.keyboard.text:upper()
|
2022-05-22 02:06:58 +00:00
|
|
|
imgtiles:add()
|
|
|
|
menuButton:setTitle("save & quit")
|
2022-05-20 17:01:59 +00:00
|
|
|
playdate.wait(0.1)
|
2022-04-27 02:40:39 +00:00
|
|
|
end
|
2022-05-08 05:40:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function playdate.keyboard.textChangedCallback()
|
|
|
|
print(#playdate.keyboard.text)
|
|
|
|
|
|
|
|
if #playdate.keyboard.text > 24 then
|
|
|
|
playdate.keyboard.text = prevtext
|
|
|
|
end
|
|
|
|
|
|
|
|
prevtext = playdate.keyboard.text
|
2022-05-20 17:01:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local tileIndex = 0.0
|
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
local curY = math.floor( tileIndex ) * 28 + 3
|
2022-05-20 17:01:59 +00:00
|
|
|
local curYlerp = -32
|
|
|
|
|
2022-05-22 17:27:01 +00:00
|
|
|
function editSave()
|
2022-05-22 20:40:42 +00:00
|
|
|
playdate.datastore.delete("levels/"..levelname..".json")
|
2022-05-22 17:27:01 +00:00
|
|
|
saveTime = 5
|
2022-05-22 20:40:42 +00:00
|
|
|
playdate.datastore.write({tiles = tiles, inverted = inverted},"levels/"..levelname)
|
2022-05-22 17:27:01 +00:00
|
|
|
end
|
|
|
|
|
2022-05-20 17:01:59 +00:00
|
|
|
function editUpdate()
|
|
|
|
|
|
|
|
local change, aChange = playdate.getCrankChange()
|
|
|
|
|
|
|
|
tileIndex += change * 0.01
|
|
|
|
|
2022-05-22 17:27:01 +00:00
|
|
|
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
|
|
|
|
end
|
2022-05-20 17:01:59 +00:00
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
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)
|
2022-05-20 17:01:59 +00:00
|
|
|
|
|
|
|
if playdate.buttonJustPressed(playdate.kButtonA) then
|
2022-05-22 17:27:01 +00:00
|
|
|
if 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)
|
2022-05-22 20:40:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if playdate.buttonJustPressed(playdate.kButtonB) then
|
|
|
|
if 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)
|
2022-05-22 17:27:01 +00:00
|
|
|
end
|
2022-05-20 17:01:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if playdate.buttonJustPressed(playdate.kButtonLeft) then
|
|
|
|
position.x -= 1
|
|
|
|
elseif playdate.buttonJustPressed(playdate.kButtonRight) then
|
|
|
|
position.x += 1
|
|
|
|
end
|
2022-05-22 02:06:58 +00:00
|
|
|
if playdate.buttonJustPressed(playdate.kButtonUp) then
|
|
|
|
position.y -= 1
|
|
|
|
elseif playdate.buttonJustPressed(playdate.kButtonDown) then
|
|
|
|
position.y += 1
|
|
|
|
end
|
|
|
|
|
2022-05-22 17:27:01 +00:00
|
|
|
if position.x + 11 < 0 then position.x = -11 end
|
|
|
|
if position.y + 7 < 0 then position.y = -7 end
|
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
gfx.clear()
|
2022-05-20 17:01:59 +00:00
|
|
|
|
|
|
|
-- draw map
|
2022-05-22 02:06:58 +00:00
|
|
|
gfx.setDrawOffset(-positionLerp.x, -positionLerp.y)
|
2022-05-22 20:40:42 +00:00
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
imgtiles:update()
|
|
|
|
imgCursor:draw((position.x + 11) * 16, (position.y + 7) * 16)
|
2022-05-22 20:40:42 +00:00
|
|
|
--gfx.setColor(playdate.graphics.kColorXOR)
|
|
|
|
--gfx.fillRect((position.x + 11) * 16, (position.y + 7) * 16,16,16)
|
2022-05-22 17:27:01 +00:00
|
|
|
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
|
2022-05-20 17:01:59 +00:00
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
-- draw ui/
|
2022-05-20 17:01:59 +00:00
|
|
|
gfx.setDrawOffset(0,0)
|
2022-05-22 02:06:58 +00:00
|
|
|
gfx.setColor(gfx.kColorBlack)
|
2022-05-22 17:27:01 +00:00
|
|
|
gfx.fillRect(362,0,400,240)
|
2022-05-20 17:01:59 +00:00
|
|
|
|
|
|
|
gfx.setColor(gfx.kColorWhite)
|
|
|
|
gfx.drawLine(360,0,360,240)
|
|
|
|
|
2022-05-22 02:06:58 +00:00
|
|
|
-- draw changes
|
|
|
|
if editor == "main" then
|
2022-05-22 17:27:01 +00:00
|
|
|
imgMus:draw(366, 128)
|
|
|
|
imgAdd:draw(366,8)
|
|
|
|
imgLine:draw(366,36)
|
|
|
|
imgSpin:draw(366,64)
|
2022-05-22 02:06:58 +00:00
|
|
|
end
|
|
|
|
|
2022-05-20 17:01:59 +00:00
|
|
|
gfx.setColor(playdate.graphics.kColorXOR)
|
2022-05-22 02:06:58 +00:00
|
|
|
gfx.fillRect(365,curYlerp,33,28)
|
2022-05-22 17:27:01 +00:00
|
|
|
|
|
|
|
if saveTime > 0 then
|
|
|
|
saveTime -= 0.1
|
|
|
|
animFloppy:draw(2,2)
|
|
|
|
end
|
2022-05-20 17:01:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function editClose()
|
2022-05-22 17:27:01 +00:00
|
|
|
editSave()
|
2022-05-22 02:06:58 +00:00
|
|
|
for tile = 1, #tiles, 1 do
|
|
|
|
tilemapEditor:setTileAtPosition(tiles[tile].x,tiles[tile].y,0)
|
2022-05-22 20:40:42 +00:00
|
|
|
tiles[tile] = nil
|
2022-05-22 02:06:58 +00:00
|
|
|
end
|
2022-05-22 20:40:42 +00:00
|
|
|
playdate.display.setInverted(false)
|
|
|
|
imgtiles = gfx.sprite.new(tilemapEditor)
|
2022-05-22 02:06:58 +00:00
|
|
|
imgtiles:remove()
|
|
|
|
playdate.getSystemMenu():removeMenuItem(toolTipButton)
|
2022-05-22 20:40:42 +00:00
|
|
|
playdate.getSystemMenu():removeMenuItem(altClrButton)
|
2022-05-22 02:06:58 +00:00
|
|
|
menuButton:setTitle("game menu")
|
2022-04-27 02:40:39 +00:00
|
|
|
end
|