Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. import haxe.xml.Fast;
  2. import flash.utils.ByteArray;
  3. import starling.display.Image;
  4. import starling.display.Sprite;
  5. import starling.utils.AssetManager;
  6.  
  7. enum Orientation {
  8. Orthogonal;
  9. Isometric;
  10. IsometricStaggered;
  11. HexagonalStaggered;
  12. }
  13.  
  14. enum RenderOrder {
  15. RightDown;
  16. RightUp;
  17. LeftDown;
  18. LeftUp;
  19. }
  20.  
  21. private class Tile {
  22. public var id:Int;
  23. public var width:Int;
  24. public var height:Int;
  25. public var source:String;
  26. // properties
  27.  
  28. public function new() {
  29. }
  30. }
  31.  
  32. private class Layer {
  33. public var name:String;
  34. public var data:Array<Array<Tile>>;
  35. public var visible:Bool;
  36. // properties
  37.  
  38. public function new() {
  39. data = new Array<Array<Tile>>();
  40. }
  41. }
  42.  
  43. class Tilemap extends Sprite {
  44.  
  45. public var mapWidth(default, null):Int;
  46. public var mapHeight(default, null):Int;
  47. public var tileWidth(default, null):Int;
  48. public var tileHeight(default, null):Int;
  49. public var orientation(default, null):Orientation;
  50. public var renderOrder(default, null):RenderOrder;
  51. private var _tiles:Array<Tile>;
  52. private var _layers:Array<Layer>;
  53. private var _assets:AssetManager;
  54.  
  55. public function new(assets:AssetManager, xml:String) {
  56. super();
  57. _assets = assets;
  58.  
  59. var xml = Xml.parse(haxe.Resource.getString(xml));
  60. var source = new Fast(xml.firstElement());
  61.  
  62. var txt:String;
  63.  
  64. txt = source.att.orientation;
  65. if (txt == "") {
  66. orientation = Orientation.Orthogonal;
  67. } else if (txt == "orthogonal") {
  68. orientation = Orientation.Orthogonal;
  69. } else if (txt == "isometric") {
  70. orientation = Orientation.Isometric;
  71. } else if (txt == "isometric-staggered") {
  72. orientation = Orientation.IsometricStaggered;
  73. } else if (txt == "hexagonal-staggered") {
  74. orientation = Orientation.HexagonalStaggered;
  75. }
  76.  
  77. txt = source.att.renderorder;
  78. if (txt == "") {
  79. renderOrder = RenderOrder.RightDown;
  80. } else if (txt == "right-down") {
  81. renderOrder = RenderOrder.RightDown;
  82. } else if (txt == "right-up") {
  83. renderOrder = RenderOrder.RightUp;
  84. } else if (txt == "left-down") {
  85. renderOrder = RenderOrder.LeftDown;
  86. } else if (txt == "left-up") {
  87. renderOrder = RenderOrder.LeftUp;
  88. }
  89.  
  90. mapWidth = Std.parseInt(source.att.width);
  91. mapHeight = Std.parseInt(source.att.height);
  92.  
  93. tileWidth = Std.parseInt(source.att.tilewidth);
  94. tileHeight = Std.parseInt(source.att.tileheight);
  95.  
  96. _tiles = new Array<Tile>();
  97. for (tileset in source.nodes.tileset) {
  98. if (tileset.has.source) {
  99. throw "External tileset source not supported.";
  100. }
  101. if (tileset.has.margin || tileset.has.spacing) {
  102. throw "Only image collections are supported.";
  103. }
  104.  
  105. for (tile in tileset.nodes.tile) {
  106. if (tile.has.id) {
  107. var t = new Tile();
  108. t.id = Std.parseInt(tile.att.id);
  109. for (image in tile.nodes.image) {
  110. t.width = Std.parseInt(image.att.width);
  111. t.height = Std.parseInt(image.att.height);
  112. t.source = image.att.source;
  113. t.source = t.source.substr(0, t.source.length-4);
  114. }
  115. _tiles.push(t);
  116. }
  117. }
  118. }
  119.  
  120. _layers = new Array<Layer>();
  121. for (layer in source.nodes.layer) {
  122. var t = new Layer();
  123. t.name = layer.att.name;
  124. for (i in 0...mapHeight) {
  125. t.data.push(new Array<Tile>());
  126. for (j in 0...mapWidth) {
  127. t.data[i].push(null);
  128. }
  129. }
  130. var i = 0;
  131. for (data in layer.nodes.data) {
  132. for (tile in data.nodes.tile) {
  133. t.data[Std.int(i / mapWidth)][Std.int(i % mapWidth)] = _tiles[Std.parseInt(tile.att.gid)-1];
  134. i += 1;
  135. }
  136. }
  137. _layers.push(t);
  138. }
  139.  
  140.  
  141. for (layer in _layers) {
  142.  
  143. // The default is renderOrder == RenderOrder.RightDown
  144. var xi = 0;
  145. var xf = mapWidth;
  146. var dx = 1;
  147. var yi = 0;
  148. var yf = mapHeight;
  149. var dy = 1;
  150.  
  151. if (renderOrder == RenderOrder.RightUp) {
  152. xi = 0;
  153. xf = mapWidth;
  154. dx = 1;
  155. yi = mapHeight-1;
  156. yf = -1;
  157. dy = -1;
  158. }
  159.  
  160. if (renderOrder == RenderOrder.LeftDown) {
  161. xi = mapWidth-1;
  162. xf = -1;
  163. dx = -1;
  164. yi = 0;
  165. yf = mapHeight;
  166. dy = 1;
  167. }
  168.  
  169. if (renderOrder == RenderOrder.LeftUp) {
  170. xi = mapWidth-1;
  171. xf = -1;
  172. dx = -1;
  173. yi = mapHeight-1;
  174. yf = -1;
  175. dy = -1;
  176. }
  177.  
  178. var _x = 0;
  179. var _y = 0;
  180. while (_y != yf) {
  181. while (_x != xf) {
  182. var cell = layer.data[_y][_x];
  183. if (cell != null && cell.source != null && cell.source != "") {
  184. var img = new Image(_assets.getTexture(cell.source));
  185. img.pivotY = img.height;
  186. img.x = _x*tileWidth;
  187. img.y = _y*tileHeight + 32;
  188. addChild(img);
  189. }
  190. _x += dy;
  191. }
  192. _x = xi;
  193. _y += dy;
  194. }
  195. }
  196.  
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement