Browse Source

Tiled Sample (#594)

Justin Walsh 6 years ago
parent
commit
7e7ee9b81a
3 changed files with 76 additions and 0 deletions
  1. 50 0
      samples/Tiled.hx
  2. 26 0
      samples/tiled_res/tileMap.tmx
  3. BIN
      samples/tiled_res/tiles.png

+ 50 - 0
samples/Tiled.hx

@@ -0,0 +1,50 @@
+import hxd.res.TiledMap.TiledMapData;
+
+// Renders a Tiled tile map directly from a .tmx source file
+// Tiled Options Required in your map:
+//  Tile Layer Format: Base64 (zlib compressed)
+//  Tile Render Order: Left Down
+
+class Tiled extends hxd.App {
+
+	inline static var TILE_SIZE:Int = 16;
+	var tiles:h2d.TileGroup;
+	var obj:h2d.Object;
+
+	override function init() {
+		var tiledMapData = hxd.Res.tileMap.toMap(); // .tmx file
+		var tiles = hxd.Res.tiles.toTile();			// tile sheet used in .tmx file above
+
+		// Specify the layers in the order they appear in the .tmx file
+		// Currently the layer id and name attribute or not stored in the TiledMapData format
+		drawLayer(tiledMapData, tiles, 0, TILE_SIZE);
+		drawLayer(tiledMapData, tiles, 1, TILE_SIZE);
+		drawLayer(tiledMapData, tiles, 2, TILE_SIZE);
+		drawLayer(tiledMapData, tiles, 3, TILE_SIZE);
+	}
+		
+	function drawLayer(map:TiledMapData, tiles:h2d.Tile, layer:Int, size:Int) {
+		var tileGroup = new h2d.TileGroup(tiles);
+		var tileSetArray = tiles.gridFlatten(TILE_SIZE, 0, 0);
+		if( map.layers.length > 0 && layer < map.layers.length ) {
+			var tileX = 0;
+			var tileY = 0;
+			for( tileId in map.layers[layer].data ) {
+				// Tiled stores empty tiles as 0 and offsets the tileId by 1 so we must skip empty tiles and adjust the tileId back to the proper index 
+				if( tileId > 0 && tileId < tileSetArray.length ) tileGroup.add(tileX, tileY, tileSetArray[tileId - 1]);
+				tileX += size;
+				if( tileX >= map.width * size ) {
+					tileX = 0; 
+					tileY += size;
+				}
+			}
+		}
+
+		s2d.addChild(tileGroup);
+	}
+
+	static function main() {
+		hxd.Res.initEmbed();
+		new Tiled();
+	}
+}

+ 26 - 0
samples/tiled_res/tileMap.tmx

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="left-down" width="20" height="30" tilewidth="16" tileheight="16" infinite="0" nextlayerid="5" nextobjectid="1">
+ <tileset firstgid="1" name="tilemap" tilewidth="16" tileheight="16" tilecount="486" columns="27">
+  <image source="tiles.png" width="432" height="288"/>
+ </tileset>
+ <layer id="1" name="Roads" width="20" height="30">
+  <data encoding="base64" compression="zlib">
+   eJxTZWBgUAViNSA+x8jAcB6ILwCxNpAvSwZWHcHmbQaaswWIt1JgngMUO6K5Tx3IdyYDT2dE4CtAvAuILzOiiqPjaXjkNpGBN+KRO0sGPoPE3gnEV4F4B5TPxUA+5kYLcwGk+CUHY0sTlJqHnmZHzRs1b9S8UfNGzRuc5gEAzcisrw==
+  </data>
+ </layer>
+ <layer id="2" name="Buildings" width="20" height="30">
+  <data encoding="base64" compression="zlib">
+   eJzt1LFuQVEcBnB3kWgnrdVQia14hZrQUU1qKkY1YTJgwdQatEPbd9AO6DNgwFt4CZ/kk9ycnHvu6bk3HcQ/+eUk31++/GO4gYD77CBk6dGZoGaXbt8NfhfTZDJRhfOc9sRd5OFBomDYV4NnibqirwiPghJ3fRhIDBV9DWgKWtyF4QqubSLMnfpe4FUw4u4WspCzuWfu1NeGDnSpx+ywy0ACkjYp5k597/ABn/TFzPS+H5jCjObMjvfJ+lT3rWANG9oyO+yeoAwVmypz2dx5kJb0vVlmxny3wjf52zIz4fsr9C0tMwu+c6HvwoNLyf/n9j1QkY1qpxqdvr/c5Vef3/f9d98e+35l3A==
+  </data>
+ </layer>
+ <layer id="3" name="Features" width="20" height="30">
+  <data encoding="base64" compression="zlib">
+   eJxjYBgFo2AUjALiQTYjJqYEtDFiYlLdQCnAZ38OIyYmBAIZUTGloBloRisjgkYBUL4lIyp/FIwCaoLVQLyGAF5LgnnHgPg4AXxiiJsHAJyoLE8=
+  </data>
+ </layer>
+ <layer id="4" name="Props" width="20" height="30">
+  <data encoding="base64" compression="zlib">
+   eJxjYBgFlAIjRkyx2VjEKAEHqGvcsASzaBTmIgTM9WPEThMDXpLqKAJuINX+gQTfB9oBQOBK5bCqxmOeBAV2eQL1ekGx9xCJ36EOZtKpDJckwx5i9YgC1ekBsS4Q60MxiC86wGnIgwT7kwfArdRyHwD+bQxy
+  </data>
+ </layer>
+</map>

BIN
samples/tiled_res/tiles.png