145 lines
4.6 KiB
Python
145 lines
4.6 KiB
Python
|
|
import os
|
|
import xml.etree.ElementTree as ET
|
|
from xml.dom import minidom
|
|
|
|
# Configuration
|
|
ASSETS_DIR_REL = 'assets/DEMO_FAZA1'
|
|
MAPS_DIR = 'assets/maps'
|
|
TSX_NAME = 'clean_assets.tsx'
|
|
TMX_NAME = 'game_map.tmx'
|
|
|
|
# Layer Structure
|
|
LAYERS = [
|
|
'Water',
|
|
'Ground_Dirt',
|
|
'Decorations',
|
|
'Buildings_Solid',
|
|
'Foreground_Top',
|
|
]
|
|
OBJECT_LAYERS = ['Collision_Logic']
|
|
|
|
BASE_DIR = os.getcwd()
|
|
|
|
def prettify(elem):
|
|
"""Return a pretty-printed XML string for the Element."""
|
|
rough_string = ET.tostring(elem, 'utf-8')
|
|
reparsed = minidom.parseString(rough_string)
|
|
return reparsed.toprettyxml(indent=" ")
|
|
|
|
def generate_tileset():
|
|
# Create <tileset> root
|
|
# version=1.10 tiledversion=1.10.2 name=clean_assets tilewidth=256 tileheight=256 tilecount=? columns=0
|
|
# Image Collection means columns=0
|
|
|
|
# We scan for images to determine max dimensions?
|
|
# Or just set arbitrary? For image collection, tilewidth/height in header is usually max width?
|
|
|
|
root = ET.Element('tileset', {
|
|
'version': '1.10',
|
|
'tiledversion': '1.11.0',
|
|
'name': 'clean_assets',
|
|
'tilewidth': '256',
|
|
'tileheight': '256',
|
|
'tilecount': '0',
|
|
'columns': '0'
|
|
})
|
|
|
|
grid = ET.SubElement(root, 'grid', {'orientation': 'orthogonal', 'width': '1', 'height': '1'})
|
|
|
|
# Scan files
|
|
assets_full_path = os.path.join(BASE_DIR, ASSETS_DIR_REL)
|
|
tile_id = 0
|
|
|
|
print(f"Scanning {assets_full_path}...")
|
|
|
|
for root_dir, dirs, files in os.walk(assets_full_path):
|
|
for f in files:
|
|
if f.lower().endswith('.png') or f.lower().endswith('.jpg'):
|
|
# Relative path from .tsx directory (assets/maps) to image
|
|
# .tsx is in assets/maps
|
|
# Image is in assets/DEMO_FAZA1/...
|
|
|
|
# Abs path of image
|
|
img_abs = os.path.join(root_dir, f)
|
|
|
|
# Rel path from BASE
|
|
# rel_from_base = os.path.relpath(img_abs, BASE_DIR)
|
|
|
|
# Rel path from MAPS dir
|
|
rel_path = os.path.relpath(img_abs, os.path.join(BASE_DIR, MAPS_DIR))
|
|
|
|
# Create <tile id="X">
|
|
tile_node = ET.SubElement(root, 'tile', {'id': str(tile_id)})
|
|
|
|
# <image width="W" height="H" source="PATH"/>
|
|
# We technically should read width/height, but Tiled often auto-detects if omitted or 0?
|
|
# Best to read it if possible, but for speed let's rely on Tiled.
|
|
# Actually, standard TMX requires image tag.
|
|
|
|
ET.SubElement(tile_node, 'image', {
|
|
'source': rel_path
|
|
# 'width': '?', 'height': '?'
|
|
})
|
|
|
|
tile_id += 1
|
|
|
|
root.set('tilecount', str(tile_id))
|
|
|
|
# Save
|
|
out_path = os.path.join(BASE_DIR, MAPS_DIR, TSX_NAME)
|
|
with open(out_path, 'w') as f:
|
|
f.write(prettify(root))
|
|
print(f"Generated Tileset: {out_path} with {tile_id} tiles.")
|
|
return tile_id
|
|
|
|
def generate_map(first_gid=1):
|
|
# <map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="50" height="50" tilewidth="16" tileheight="16" infinite="0" nextlayerid="7" nextobjectid="1">
|
|
|
|
root = ET.Element('map', {
|
|
'version': '1.10',
|
|
'tiledversion': '1.11.0',
|
|
'orientation': 'orthogonal',
|
|
'renderorder': 'right-down',
|
|
'width': '50',
|
|
'height': '50',
|
|
'tilewidth': '16',
|
|
'tileheight': '16', # User requested 16x16
|
|
'infinite': '0'
|
|
})
|
|
|
|
# <tileset firstgid="1" source="clean_assets.tsx"/>
|
|
ET.SubElement(root, 'tileset', {'firstgid': '1', 'source': TSX_NAME})
|
|
|
|
# Data is empty CSV
|
|
# 50*50 = 2500 zeros
|
|
empty_csv = "0," * 2499 + "0"
|
|
|
|
layer_id = 1
|
|
for layer_name in LAYERS:
|
|
layer = ET.SubElement(root, 'layer', {
|
|
'id': str(layer_id),
|
|
'name': layer_name,
|
|
'width': '50',
|
|
'height': '50'
|
|
})
|
|
data = ET.SubElement(layer, 'data', {'encoding': 'csv'})
|
|
data.text = '\n' + empty_csv + '\n'
|
|
layer_id += 1
|
|
|
|
for obj_layer_name in OBJECT_LAYERS:
|
|
ET.SubElement(root, 'objectgroup', {
|
|
'id': str(layer_id),
|
|
'name': obj_layer_name
|
|
})
|
|
layer_id += 1
|
|
|
|
out_path = os.path.join(BASE_DIR, MAPS_DIR, TMX_NAME)
|
|
with open(out_path, 'w') as f:
|
|
f.write(prettify(root))
|
|
print(f"Generated Map: {out_path}")
|
|
|
|
if __name__ == "__main__":
|
|
count = generate_tileset()
|
|
generate_map()
|