2012-06-21 50 views
0

我有一個960 * 640像素的地圖,使用64 * 64像素的高清視網膜顯示器。現在,我需要一個用於顯示sd的tilemap(我想這應該是480 * 320像素,使用32 * 32瓦片)將hd TMX Tilemap轉換爲sd,我應該手動執行此操作嗎?

那麼,有沒有簡單的方法可以去?

在Mac應用商店中查找類似'Unretiner'的工具。

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

謝謝你。

回答

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) 
+0

蟒蛇......我不知道什麼蟒蛇。 – bureaucoconut

相關問題