Using tilemap editor for isomertic map

convert isometric map from TiledMapEditor to defold.go

in:
sring: name_of_map without “.json”
out:
for each tilelayer: <layer_name>.go
for each objects layer: <layer_name>.lua, <layer_name>.json --usefull for “defgraph” pathfindig lib
for each tileset: <tileset.name>.tilesource

limit:
only rect iso map is suported,
in tilesource image is generated as: “/assets/” + tileset.name + “.png”
tileset image is spreeteshet
object have to have properties(“locator” or “path”), type(point or polygon) in TiledMapEditor, and only that will exported
in *.go is sprite_component.name generated as “sprite_{col}_{row}”, coord is same as in TiledMapEditor

usage:
from TiledMapEditor export to json with csv data as my_map.json
run python isogen.py my_map

isoge.py

import json
import math

import sys


map_name = sys.argv[1]

f = open( map_name + ".json", "r")
data = f.read()
f.close()

dict = json.loads( data )

height = dict["height"]
tileheight = dict["tileheight"]
width = dict["width"]
tilewidth = dict["tilewidth"]
tilesets = dict["tilesets"]

#print( height ,tileheight ,width , tilewidth, tilesets )


top_y = height * tileheight * 0.5


nodes = []
paths = []

sprite_def = '''
embedded_components {{
  id: "tile_{1}_{0}"
  type: "sprite"
  data: "tile_set: \\"{6}"\\n"
  "default_animation: \\"{2}\\"\\n"
  "material: \\"/builtins/materials/sprite.material\\"\\n"
  "blend_mode: BLEND_MODE_ALPHA\\n"
  ""
  position {{
    x: {3}
    y: {4}
    z: {5}
  }}
  rotation {{
    x: 0.0
    y: 0.0
    z: 0.0
    w: 1.0
  }}
}}\n
'''



def add_node( x, y, name = ""):	
	y = top_y - y
	for i in range(len(nodes)):
		if abs( x - nodes[i]["x"] ) < 10 and abs( y - nodes[i]["y"] ) < 10 :
			return i		
	nodes.append( {"x": x, "y": y, "name":name } )
	return len(nodes) - 1

def export_objects( layer ) :
	objects = layer["objects"] 
	layername = layer["name"]
	for object in objects:
		if object["type"] == "locator":
			add_node( object["x"], object["y"], object["name"] )

	for object in objects:
		if object["type"] == "path":
			x = object["x"]
			y = object["y"]
			path = []
			for point in object["polygon"]:
				path.append( add_node( x + point["x"], y + point["y"] ) )
			paths.append(path)

	map = { "nodes": nodes, "paths": paths }
	map_json = json.dumps( map )

	f = open( layername + ".json", "w")
	f.write( map_json )
	f.close()

def export_objects_to_lua( layer ) :
	objects = layer["objects"] 
	layername = layer["name"]
	msg = "return {\n locators = {\n"
	for locator in nodes:
		msg += f'''  {{   x={locator["x"]}, y={locator["y"]} ''' 
		if "name" in locator and not( locator["name"] == ""):
			msg += f''', name = "{locator["name"]}" '''
		msg += ' }\n'
	msg += " }, \n"
	msg += " paths = { \n"
	for path in paths:
		msg += "  { "
		for node_id in path:
			msg += f'''{node_id + 1}, '''
		msg += "}, \n"

	msg += " }, \n}"
	f = open( layername + ".lua", "w")
	f.write( msg )
	f.close()


		
		



def get_tilesource(tile_id): 
	for tile in tilesets:
		first = tile["firstgid"] 
		tilecount = tile["tilecount"]
		if first <= tile_id and tile_id < (first+tilecount):
			return "/assets/" + tile["name"] +  ".tilesource\\"
	return ""



def export_tile_layer( layer ):
	odd = False
	message = ""
	id = 0
	col = 1
	row = 1
	x = 0
	y = 0
	z = 0

	if "data" in layer:	
		for tile_id in layer["data"]:
			if tile_id > 0 :
				tile_source = get_tilesource(tile_id)
				message = sprite_def.format(col,row,tile_id,x, top_y - y,z, tile_source  ) + message
			x += tilewidth 
			z = (x * 0.0001 + y * 0.01) * 0.1
			col += 1
			if col > width :
				col = 1
				row += 1
				y += tileheight * 0.5
				if odd :
					x = 0
				else:
					x = tilewidth * 0.5
				odd = not odd

	f = open( layer["name"] + ".go", "w")
	f.write( message )
	f.close()

	

def export_tilesets():
	header = ''' \n
	image: "/assets/{0}.png"
	tile_width: {1}
	tile_height: {2} 
	tile_margin: 0
	tile_spacing: 0
	collision: ""
	material_tag: "tile"
	collision_groups: "default"
	\n
	'''
	tail = '''\n
	 extrude_borders: 2
	 inner_padding: 0
	 sprite_trim_mode: SPRITE_TRIM_MODE_OFF
	'''


	for tile in tilesets:
		first = tile["firstgid"]
		message = header.format( tile["name"], tile["tilewidth"], tile["tileheight"] )
		for id in range(first, tile["tilecount"] + first):
			animations = (
				'\n animations {'
  				f'\n  id: "{id}"'
  				f'\n  start_tile: {id}'
  				f'\n  end_tile: {id}'
  				'\n  playback: PLAYBACK_NONE'
  				'\n  fps: 0'
  				'\n  flip_horizontal: 0'
  				'\n  flip_vertical: 0'
				'\n}')
			message += animations 
		message += tail
	
		f = open( tile["name"] + ".tilesource", "w")
		f.write( message )
		f.close()


	
for layer in dict["layers"]:
		
	if layer["type"] == "tilelayer":
		export_tile_layer( layer )


	if "objects" in layer:
		export_objects( layer ) 
		export_objects_to_lua( layer ) 
		
		

export_tilesets()

				
2 Likes

warning each existed file will by overwrited