开发者

convert a XML file to lua table?

I have an XML file generated from Tiled (format: http://sourceforge.net/apps/mediawiki/tiled/index.php?title=TMX_Map_Format ) and example:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="isometric" width="100" height="100" tilewidth="64" tileheight="32">
 <tileset firstgid="1" name="grass_and_water_0" tilewidth="64" tileheight="64">
  <image source="tiles/grass_and_water_0.png" trans="ff00ff" width="64" height="64"/>
 </tileset>
 <layer name="Tile Layer 1" width="100" height="100">
  <data>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
  </data>
 </layer>
</map>

It's a snippet of it, as the actual file is over 10,000 lines.

Due to performance reasons I am trying to convert this from an XML file into a Lua Table something like

variables = [tilewidth=64,tileheight=64,width=100,height=100]

tileset = [1=[source="tiles/grass_and_water_0.png",width=64,height=64],....]

tile_map[][] = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

Where the 1's would be replaced with the tile gid value. The rest of the data (variables would need to be added to another table, that I could use)

And the reasons I want to do this is because using a native Lua table would be faster then XML Parsing and then using the resulting data.

Extra garbage rambling:

And then I could simply use the for command at Drawing Isometric game worlds to generate the display.

And it would also allo开发者_运维技巧w me to generate more layers for objects ontop, for example

objects_map[][] = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,3,3,3,3,4,4,5,5,5,5,0,0],
[0,0,3,3,3,3,3,4,6,6,6,5,0,0],
[0,0,3,3,3,3,4,1,6,5,5,6,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

And then I could display it in one draw call combining the two arrays and replacing 0's with the table data from tile_map and the objects table to create something like

result_map[][] = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,3,3,3,3,4,4,5,5,5,5,1,1],
[1,1,3,3,3,3,3,4,6,6,6,5,1,1],
[1,1,3,3,3,3,4,1,6,5,5,6,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

Seems simple enough, all I need to do is figure out how to convert the XML into the tilemap.

Any help would be wonderful, or direction as to where I can get some help, I looked at the lua users and didn't really find anything that would help me.


I suggest LuaExpat. I've used it in production code and it worked great. You would just write some callback functions which generate the Lua tables you need when various parts of the input XML structure are encountered. Should be a snap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜