I wrote a script to load Tiled maps the other week I only did Orthogonal, right down but it only took an hour or two to write from scratch, the below loads a tiled map saved as a json file and turns it into a series of images, one per layer - there are a few specifics that would need to be changed for more general use but as a basic model it works nicely.
/*Load a map file:
- Maps for this should be made with Tiled
- Must be Orthogonal & right-down
- must only use one tile set
- image for tileset should be in the maps folder
- map should also be in maps folder
- map should be saved in JSON format
- Set a custom property called "ground" to be a boolean for all layers
- set it to true for layers to draw underneath sprites
- set it to false for layers to draw above sprites
- Make a layer called "obstructions" to have obstructions in (and not be drawn):
** tile 1 = obstructed/impassable
** tile 2 = 1 AP movement cost
** tile 3 = 2 AP etc*/
function load_map(name)
{
var map = new Object();
map.obs = [];//obstruction map
map.p_m = [];//array to hold player obstructions when map is in use
map.c_m = [];//collision map (combination of the above two, when map is in use
map.ground = [];//images to draw under the player
map.sky = [];//images to draw over the player
//load the map file and parse it
var m_file = OpenRawFile("../maps/"+name+".json", false);
var m_data = CreateStringFromByteArray(m_file.read(m_file.getSize()));
var m_raw = JSON.parse(m_data);
m_file.close();
//load the tile set
var t_surface = LoadSurface("../maps/"+m_raw.tilesets[0].image);
//temporary variables for this function to use
m_raw.m_layers = [];
var temp_x = 0;
var temp_y = 0;
var t = 1;
var tiles = [];
var mask = CreateColor(0,0,0);
//build our tile set
tiles[0] = t_surface.cloneSection(0, 0, 32, 32);
for(temp_y = 0; temp_y < 52; ++temp_y)
{
for(temp_x = 0; temp_x < 20; ++temp_x, ++t)
{
tiles[t] = t_surface.cloneSection(temp_x * 32, temp_y * 32, 32, 32);
}
}
//read the map layer data - generate an image per layer and an obstruction map
for(var i = 0, j = 0, k = 0, l = 0; k < m_raw.layers.length; ++ k, l = 0)
{
if(m_raw.layers[k].name != "Obstructions")
{
m_raw.m_layers[k] = CreateSurface(m_raw.layers[k].width * m_raw.tilesets[0].tilewidth, m_raw.layers[k].height * m_raw.tilesets[0].tileheight,CreateColor(0,0,0,0));
mask = CreateColor(255,255,255,Math.floor(m_raw.layers[k].opacity * 255));
for(i = 0; i < m_raw.layers[0].height; ++ i)
{
for(j = 0; j < m_raw.layers[0].width; ++ j, ++l)
{
m_raw.m_layers[k].blitMaskSurface(tiles[m_raw.layers[k].data[l]], j*32, i * 32,mask);
}
}
if(m_raw.layers[k].properties.ground)
{
map.ground.push(m_raw.m_layers[k].createImage());
}
else
{
map.sky.push(m_raw.m_layers[k].createImage());
}
}
else
{
for(i = 0; i < m_raw.layers[0].height; ++ i)
{
map.obs[i] = [];
map.p_m[i] = [];
map.c_m[i] = [];
for(j = 0; j < m_raw.layers[0].width; ++ j, ++l)
{
map.obs[i][j] = m_raw.layers[k].data[l] -1;
map.p_m[i][j] = -1;
map.c_m[i][j] = 1;
}
}
}
}
return map;
}