Skip to content

Instantly share code, notes, and snippets.

@nodepond
Created September 30, 2014 17:47
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/5e71171ffbe2ec09aa03 to your computer and use it in GitHub Desktop.
Save nodepond/5e71171ffbe2ec09aa03 to your computer and use it in GitHub Desktop.
Pyxel to Mogamett tilemap-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 generateMogamettMap(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
puts "\nWelcome to Nodepond's Pyxel json to Mogamett parser.\nThe world is a jungle... find the path and reach out for treasure."
puts "2014 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 generateMogamettMap("mapdata", @tilemapdata)
puts
puts generateMogamettMap("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 = mg.Tilemap(400, 300, 16, 16, love.graphics.newImage('maps/second-spacey.png'), mapdata)
collidemap = mg.Tilemap(400, 300, 16, 16, love.graphics.newImage('maps/second-spacey.png'), collidedata)
-- if using the engine then generate box2d collision solids
-- and add the tilemap to the 'Default' layer so it can be drawn
mg.world:generateCollisionSolids(collidemap)
mg.world:addToLayer('Default', tilemap)
end
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment