将hd TMX Tilemap转换为sd,我应该手动执行此操作吗?

时间:2012-06-21 14:26:30

标签: tmx

我有一个960 * 640像素的瓷砖地图,使用64 * 64瓷砖进行高清视网膜显示。现在,我需要一个用于sd显示的tilemap。(我猜它应该是使用32 * 32个tile的480 * 320像素)

那么,还有简单的方法吗?

在Mac应用商店中查找“Unretiner”等工具。

'hd img - > sd img'由Unretiner
'hd tmx - > sd tmx'by ??

谢谢你。

1 个答案:

答案 0 :(得分:1)

这是我用来从hd .tmx生成sd .tmx的Python脚本。 您可以通过以下shell命令来使用它。

>python change_tmx_numbers.py <directory_which_contains_hd_tmx_files> <output_directory>

剧本:

#!/usr/bin/python
import os
import shutil
import sys
import xml.etree.cElementTree as ET
import re



def npath(path):
  if path[-1] == '/':
    return path[:-1]
  return path;

def subdir(*paths):
  return '/'.join(paths)

def changeText(text):
  print text
  pointm = '\{(?P<x>-?\d+), (?P<y>-?\d+)\}'
  framem = '\{\{(?P<x>-?\d+), (?P<y>-?\d+)\}, \{(?P<width>\d+), (?P<height>\d+)\}\}'
  m = re.match(pointm, text)
  if m != None:
    rt = '{%d, %d}' % tuple([int(f) / 2 for f in m.groups()])
    print 'change %s -> %s' % (text, rt)
    return rt
  else:
    m = re.match(framem, text)
    if m != None:
      rt = '{{%d, %d}, {%d, %d}}' % tuple([int(f) / 2 for f in m.groups()])
      print 'change %s -> %s' % (text, rt)
      return rt
    else:
      print 'text: \"' + text + '\" is not matched'
      return text

def resizeElement(element):
  elems = list(element)
  if(len(elems) == 0 and element.text != None):
    element.text = changeText(element.text)
  else:
    for el in elems:
      resizeElement(el)

def resizeAttToHalf(attrib, keys):
  for key in keys:
    if key in attrib:
      attrib[key] = str(int(attrib[key]) / 2)
  return attrib

def resizeMap(map):
  map.attrib = resizeAttToHalf(map.attrib, ['tilewidth', 'tileheight'])


def resizeImage(im):
  im.attrib = resizeAttToHalf(im.attrib, ['width', 'height'])
  im.attrib['source'] = im.attrib['source'].replace('-hd', '')

def resizeTileset(ts):
  ts.attrib = resizeAttToHalf(ts.attrib, ['tilewidth', 'tileheight'])
  for image in ts.findall('image'): 
    resizeImage(image)

def resizeProperty(prop):
  if 'name' in prop.attrib and prop.attrib['name'] == 'points':
    points = prop.attrib['value'].split(' ')
    nps = [','.join([str(int(p.split(',')[0])/2), str(int(p.split(',')[1])/2)]) for p in points]
    prop.attrib['value'] = ' '.join(nps)

def resizePolygon(prop):
  points = prop.attrib['points'].split(' ')
  nps = [','.join([str(int(p.split(',')[0])/2), str(int(p.split(',')[1])/2)]) for p in points]
  prop.attrib['points'] = ' '.join(nps)

def resizeObject(obj):
  obj.attrib = resizeAttToHalf(obj.attrib, ['x', 'y', 'width', 'height'])
  for props in obj.findall('properties'):
    for prop in props.findall('property'):
      resizeProperty(prop)
  for poly in obj.findall('polygon'):
    resizePolygon(poly)

def resizeplist(plist, dest):
  doc = ET.parse(plist)
  map = doc.getroot()
  resizeMap(map)
  for tileset in map.findall('tileset'):
    resizeTileset(tileset)

  for objectg in map.findall('objectgroup'):
    for obj in objectg.findall('object'):
      resizeObject(obj)

  destf = open(dest, 'w')
  prologue = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
  destf.write(prologue)
  doc.write(destf)
  destf.close()


def walkdir(path, dest):
  files =  [f for f in os.listdir(path) if not os.path.isdir(subdir(path, f))]
  dirs = [d for d in os.listdir(path) if  os.path.isdir(subdir(path, d))]
  for f in files:
    if (f.endswith('-hd.tmx')):
      resizeplist(subdir(path, f), subdir(path, f.replace('-hd', '')))
#  for d in dirs:
#    walkdir(subdir(path, d), subdir(dest, d))



if __name__ == "__main__":
  path = npath(sys.argv[1])
  dest = npath(sys.argv[2])
  walkdir(path, dest)