Advertisement
Guest User

Aseprite Tilemap to 2D-Array of Tileset-Numbers

a guest
Dec 29th, 2021
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. --  Script for Aseprite to export a Tilemap to a 2D-Array of Tileset-Numbers
  2. --  Please don't hate on me that was the first lua-Script I have ever written in my whole life I really hate interpreter languages
  3. --  License: Bro just use it
  4.  
  5.  
  6. if TilesetMode == nil then return app.alert "Use Aseprite 1.3" end
  7. local spr = app.activeSprite
  8.  
  9. if not spr then return end
  10.  
  11. local d = Dialog("Export Tilemap as .txt File")
  12. d:label{id="lab1",label="",text="Export Tilemap as .txt File for your own GameEngine"}
  13.  :file{id = "path", label="Export Path", filename="",open=false,filetypes={"txt"}, save=true, focus=true}
  14.  :number{id="vbegin",label="Index of first Tileset (Default 1): ", text="1",focus=true}
  15.  :number{id="Size",label="Tiles per Row (Layer-Width / Tile-Width): ",text="16"}
  16.  :entry{id="seperator",label="Seperator",text=";"}
  17.  :separator{}
  18.  :label{id="lab2", label="",text="In the last row of the tilemap-layer there has to be at least one Tile \"colored\" to fully export the whole Tilemap"}
  19.  :button{id="ok",text="&OK",focus=true}
  20.  :button{text="&Cancel" }
  21.  :show()
  22.  
  23. local data = d.data
  24. if not data.ok then return end
  25.     local lay = app.activeLayer
  26.     if(#data.path<=0)then app.alert("No path selected") end
  27.     if not lay.isTilemap then return app.alert("Layer is not tilemap") end
  28.     pc = app.pixelColor
  29.     mapFile = io.open(data.path,"w")
  30.  
  31.   for _,c in ipairs(lay.cels) do
  32.     local img = c.image
  33.     local i = 0
  34.     for p in img:pixels() do
  35.       if(p ~= nil) then
  36.         i=i+1
  37.         if(data.vbegin==1) then
  38.           mapFile:write(pc.tileI(p()))
  39.           if(i+1<=data.Size and #data.seperator > 0)then mapFile:write(data.seperator) end
  40.         else
  41.           mapFile:write(pc.tileI(p()+data.vbegin-1))
  42.           if(i+1<=data.Size and #data.seperator > 0)then mapFile:write(data.seperator) end
  43.         end
  44.         if(i==data.Size) then
  45.           mapFile:write("\n")
  46.           i=0      
  47.         end
  48.       end
  49.     end
  50.   end
  51.  
  52.     mapFile:close()
  53.      
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement