Tiled.hx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import hxd.res.TiledMap.TiledMapData;
  2. // Renders a Tiled tile map directly from a .tmx source file
  3. // Tiled Options Required in your map:
  4. // Tile Layer Format: Base64 (zlib compressed)
  5. // Tile Render Order: Left Down
  6. class Tiled extends hxd.App {
  7. inline static var TILE_SIZE:Int = 16;
  8. var tiles:h2d.TileGroup;
  9. var obj:h2d.Object;
  10. override function init() {
  11. var tiledMapData = hxd.Res.tileMap.toMap(); // .tmx file
  12. var tiles = hxd.Res.tiles.toTile(); // tile sheet used in .tmx file above
  13. // Specify the layers in the order they appear in the .tmx file
  14. // Currently the layer id and name attribute or not stored in the TiledMapData format
  15. drawLayer(tiledMapData, tiles, 0, TILE_SIZE);
  16. drawLayer(tiledMapData, tiles, 1, TILE_SIZE);
  17. drawLayer(tiledMapData, tiles, 2, TILE_SIZE);
  18. drawLayer(tiledMapData, tiles, 3, TILE_SIZE);
  19. }
  20. function drawLayer(map:TiledMapData, tiles:h2d.Tile, layer:Int, size:Int) {
  21. var tileGroup = new h2d.TileGroup(tiles);
  22. var tileSetArray = tiles.gridFlatten(TILE_SIZE, 0, 0);
  23. if( map.layers.length > 0 && layer < map.layers.length ) {
  24. var tileX = 0;
  25. var tileY = 0;
  26. for( tileId in map.layers[layer].data ) {
  27. // 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
  28. if( tileId > 0 && tileId < tileSetArray.length ) tileGroup.add(tileX, tileY, tileSetArray[tileId - 1]);
  29. tileX += size;
  30. if( tileX >= map.width * size ) {
  31. tileX = 0;
  32. tileY += size;
  33. }
  34. }
  35. }
  36. s2d.addChild(tileGroup);
  37. }
  38. static function main() {
  39. hxd.Res.initEmbed();
  40. new Tiled();
  41. }
  42. }