浏览代码

added Tiled TMX support

Nicolas Cannasse 12 年之前
父节点
当前提交
792238c91b
共有 3 个文件被更改,包括 68 次插入0 次删除
  1. 4 0
      hxd/res/FileTree.hx
  2. 4 0
      hxd/res/Loader.hx
  3. 60 0
      hxd/res/TiledMap.hx

+ 4 - 0
hxd/res/FileTree.hx

@@ -75,6 +75,8 @@ class FileTree {
 			if( sys.FileSystem.isDirectory(path) ) {
 				if( ignoredDir.exists(f.toLowerCase()) )
 					continue;
+				if( f.charCodeAt(0) == "_".code )
+					continue;
 				var sub = embedDir(f, relPath + "/" + f, path);
 				if( sub != null )
 					Reflect.setField(data, f, sub);
@@ -309,6 +311,8 @@ class FileTree {
 			return { e : macro loader.loadBitmapFont($epath), t : macro : hxd.res.BitmapFont };
 		case "wav", "mp3":
 			return { e : macro loader.loadSound($epath), t : macro : hxd.res.Sound };
+		case "tmx":
+			return { e : macro loader.loadTiledMap($epath), t : macro : hxd.res.TiledMap };
 		default:
 			return { e : macro loader.loadData($epath), t : macro : hxd.res.Resource };
 		}

+ 4 - 0
hxd/res/Loader.hx

@@ -69,4 +69,8 @@ class Loader {
 		return new Resource(fs.get(path));
 	}
 	
+	function loadTiledMap( path : String ) {
+		return new TiledMap(fs.get(path));
+	}
+	
 }

+ 60 - 0
hxd/res/TiledMap.hx

@@ -0,0 +1,60 @@
+package hxd.res;
+
+typedef TiledMapLayer = {
+	var data : Array<Int>;
+	var name : String;
+	var opacity : Float;
+	var objects : Array<{ x: Int, y : Int, name : String, type : String }>;
+}
+
+typedef TiledMapData = {
+	var width : Int;
+	var height : Int;
+	var layers : Array<TiledMapLayer>;
+}
+
+class TiledMap extends Resource {
+	
+	public function toMap() : TiledMapData {
+		var data = entry.getBytes().toString();
+		var base = new haxe.crypto.BaseCode(haxe.io.Bytes.ofString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
+		var x = new haxe.xml.Fast(Xml.parse(data).firstElement());
+		var layers = [];
+		for( l in x.nodes.layer ) {
+			var data = StringTools.trim(l.node.data.innerData);
+			while( data.charCodeAt(data.length-1) == "=".code )
+				data = data.substr(0, data.length - 1);
+			var bytes = haxe.io.Bytes.ofString(data);
+			var bytes = base.decodeBytes(bytes);
+			bytes = format.tools.Inflate.run(bytes);
+			var input = new haxe.io.BytesInput(bytes);
+			var data = [];
+			for( i in 0...bytes.length >> 2 )
+				data.push(input.readInt32());
+			layers.push( {
+				name : l.att.name,
+				opacity : l.has.opacity ? Std.parseFloat(l.att.opacity) : 1.,
+				objects : [],
+				data : data,
+			});
+		}
+		for( l in x.nodes.objectgroup ) {
+			var objs = [];
+			for( o in l.nodes.object )
+				if( o.has.name )
+					objs.push( { name : o.att.name, type : o.has.type ? o.att.type : null, x : Std.parseInt(o.att.x), y : Std.parseInt(o.att.y) } );
+			layers.push( {
+				name : l.att.name,
+				opacity : 1.,
+				objects : objs,
+				data : null,
+			});
+		}
+		return {
+			width : Std.parseInt(x.att.width),
+			height : Std.parseInt(x.att.height),
+			layers : layers,
+		};
+	}
+	
+}