Skip to content

Instantly share code, notes, and snippets.

@nodepond
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodepond/0798669598eb2a968e53 to your computer and use it in GitHub Desktop.
Save nodepond/0798669598eb2a968e53 to your computer and use it in GitHub Desktop.
Pyxel Tilemap to fuccboiGDX Parser in Ruby
# Usage:
# Run this script in the same folder, where you have exported the ’tilemap.json’ from Pyxel
# The tilemap is named ‘Layer 0′
# The collisionmap is named ‘Layer Collide’
# It generates lua-code ready to c&p paste. There is an more accurate usage example in the ruby-file itself.
# http://www.nodepond.com/blog/779-pyxel-tilemap-to-fuccboigdx-parser-in-ruby
require 'json'
tilemap_raw = File.read('tilemap.json')
tjson = JSON.parse tilemap_raw
@tileshigh = tjson["tileshigh"].to_i
@tileswide = tjson["tileswide"].to_i
@tilewidth = tjson["tilewidth"].to_i
@tileheight = tjson["tileheight"].to_i
layers = tjson["layers"]
@tilemapdata = Hash.new
@collidedata = Hash.new
layers.each do |l|
if l["name"].eql? "Layer 0"
@tilemapdata = l["tiles"]
end
if l["name"].eql? "Layer Collide"
@collidedata = l["tiles"]
end
end
def generateTileMap(varname, tileshash)
luastring = String.new
# init datastructure
lines = Array.new
@tileshigh.times do
lines << Array.new(@tileswide)
end
# parse tiles to correct places in datastructure
tileshash.each do |t|
lines[t["y"]][t["x"]] = t["tile"]+1
end
# generate lua-format
luastring += "local " + varname + " = { \n"
i = 0
lines.each do |line|
j = 0
luastring += "{"
lines[i].each do |tile|
luastring += lines[i][j].to_s
if (j+1) < lines[i].length
luastring += ", "
end
j+=1
end
luastring += "}"
if (i+1) < lines.length
luastring += ","
end
luastring += "\n"
i+=1
end
luastring += "}"
return luastring
end
def generateCollideMap(varname, tileshash)
luastring = String.new
# init datastructure
lines = Array.new
@tileshigh.times do
lines << Array.new(@tileswide)
end
tileshash.each do |t|
t["tile"] = t["tile"]+1 # correct offset from pyxel to fuccboi
if t["tile"] >= 1 # treat everything "solid" as 1
t["tile"] = 1
end
end
# parse tiles to correct places in datastructure
tileshash.each do |t|
lines[t["y"]][t["x"]] = t["tile"]
end
# generate lua-format
luastring += "local " + varname + " = { \n"
i = 0
lines.each do |line|
j = 0
luastring += "{"
lines[i].each do |tile|
luastring += lines[i][j].to_s
if (j+1) < lines[i].length
luastring += ", "
end
j+=1
end
luastring += "}"
if (i+1) < lines.length
luastring += ","
end
luastring += "\n"
i+=1
end
luastring += "}"
return luastring
end
puts "\nWelcome to Nodepond's Pyxel tilemap-json to fuccboiGDX parser.\n\n"
puts "2015 nodepond.com, m.wisniowski"
puts
puts "Expects tilemap with filename 'tilemap.json'"
puts "The tilemap is named 'Layer 0'"
puts "The collisionmap is named 'Layer Collide'"
puts
puts "Tileshigh: " + @tileshigh.to_s
puts "Tileswide: " + @tileswide.to_s
puts "Tilewidth: " + @tilewidth.to_s
puts "Tileheight: " + @tileheight.to_s
puts
puts generateTileMap("mapdata", @tilemapdata)
puts
puts generateCollideMap("collidedata", @collidedata)
=begin
-- Implementation Example
function tilemapTest()
local mapdata = {
{0, 0, 0, 0, 0, 0, 0, 0},
{4, 0, 1, 2, 2, 2, 4, 0},
{3, 0, 3, 9, 3, 3, 3, 0},
{6, 6, 7, 3, 3, 5, 6, 8},
{5, 6, 6, 6, 7, 3, 9, 3},
{3, 3, 3, 3, 3, 3, 3, 3}
}
local collidedata = {
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}
-- create a tilemap, parameters are: x, y (center) position, tile width and height, tileset image, tilemap data
tilemap = fg.Tilemap(400, 300, 16, 16, love.graphics.newImage('maps/second-spacey.png'), mapdata)
tilemap:setCollisionData( collidedata )
-- if using the engine then generate box2d collision solids
-- and add the tilemap to the 'Default' layer so it can be drawn
fg.world:generateCollisionSolids(tilemap)
fg.world:addToLayer('Default', tilemap)
end
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment