JuiceBoxx

TiledImport Class

Dec 3rd, 2015
387
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import openfl.Assets;
  4. import haxe.io.Path;
  5. import haxe.xml.Parser;
  6. import flixel.FlxG;
  7. import flixel.FlxObject;
  8. import flixel.FlxSprite;
  9. import flixel.group.FlxGroup;
  10. import flixel.tile.FlxTilemap;
  11. import flixel.addons.editors.tiled.TiledMap;
  12. import flixel.addons.editors.tiled.TiledObject;
  13. import flixel.addons.editors.tiled.TiledObjectGroup;
  14. import flixel.addons.editors.tiled.TiledTileSet;
  15.  
  16. class TiledImport extends TiledMap
  17. {
  18.     //The path to your tilemaps. Edit this if you do not have your maps in "assets/data/maps".
  19.     private inline static var c_PATH_LEVEL_TILESHEETS = "assets/data/maps/";
  20.    
  21.     // The three layers that this script will load.
  22.     public var foregroundTiles:FlxGroup;
  23.     public var backgroundTiles:FlxGroup;
  24.     public var collisionTiles:FlxGroup;
  25.    
  26.     //Collidable tiles (Don't worry about this)
  27.     private var collidableTileLayers:Array<FlxTilemap>;
  28.    
  29.     public function new(tiledLevel:Dynamic)
  30.     {
  31.         super(tiledLevel);
  32.        
  33.        
  34.         //The three layers that this script will load.
  35.        
  36.         foregroundTiles = new FlxGroup();
  37.         backgroundTiles = new FlxGroup();
  38.         collisionTiles = new FlxGroup();
  39.        
  40.         //The collision tiles should only be visible in Tiled, if you want them to be visible in-game, delete the line of code under this comment.
  41.         collisionTiles.visible = false;
  42.        
  43.        
  44.         //Set World bounds and camera bounds. You do not need to set camera bounds if you don't want to.
  45.        
  46.         FlxG.camera.setBounds(0, 0, fullWidth, fullHeight, true);
  47.         FlxG.worldBounds.set(0, 0, fullWidth, fullHeight);
  48.        
  49.         // Load Tile Maps
  50.         for (tileLayer in layers)
  51.         {
  52.             /*
  53.              * These are custom variables you need to set within your layers inside of the editor.
  54.              * So a layer, prehaps "Tile Layer 1", would require the custom variables "tileset" and "layer"
  55.              * The "tileset" variable should be set to whichever tileset the layer is using, this limits your layers to one tileset.
  56.              * We will get to the "layer" variable later.
  57.              */
  58.             var tileSheetName:String = tileLayer.properties.get("tileset");
  59.             var tileLayerName:String = tileLayer.properties.get("layer");
  60.            
  61.             /*
  62.              * This part is used for splashing errors. Nothing in this part really matters (for beginners, at least.), so just go on to the next comment.
  63.              */
  64.            
  65.             if (tileSheetName == null)
  66.                 throw "'tileset' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
  67.            
  68.             if (tileLayerName == null)
  69.                 throw "'layer' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
  70.            
  71.            
  72.             var tileSet:TiledTileSet = null;
  73.             for (ts in tilesets)
  74.             {
  75.                 if (ts.name == tileSheetName)
  76.                 {
  77.                     tileSet = ts;
  78.                     break;
  79.                 }
  80.             }
  81.            
  82.             if (tileSet == null)
  83.                 throw "Tileset '" + tileSheetName + " not found. Did you mispell the 'tilesheet' property in " + tileLayer.name + "' layer?";
  84.                
  85.             var imagePath       = new Path(tileSet.imageSource);
  86.             var processedPath   = c_PATH_LEVEL_TILESHEETS + imagePath.file + "." + imagePath.ext;
  87.            
  88.             var tilemap:FlxTilemap = new FlxTilemap();
  89.             tilemap.widthInTiles = width;
  90.             tilemap.heightInTiles = height;
  91.             tilemap.loadMap(tileLayer.tileArray, processedPath, tileSet.tileWidth, tileSet.tileHeight, 0, tileSet.firstGID, 1, 1);
  92.            
  93.             /*
  94.              * This is for loading layers. You can add more if you'd like, but I have set up some simple layers here.
  95.              * To set the layer in Tiled, click on the layer you want to set. Then add a custom variable titled "layer"
  96.              * Your new "layer" variable should equal one of the layers referenced below.
  97.              * (layer should be "foreground", "background", etc.)
  98.              *
  99.              * Graphical layers that only serve for, well, graphical purposes only require this code:
  100.              *  foregroundTiles.add(tilemap);
  101.              *
  102.              * Layers that must collide require this code:
  103.                 if (collidableTileLayers == null)
  104.                     collidableTileLayers = new Array<FlxTilemap>();
  105.                
  106.                 collisionTiles.add(tilemap);
  107.                
  108.                 collidableTileLayers.push(tilemap);
  109.              */
  110.            
  111.             if (tileLayerName == 'foreground')
  112.             {
  113.                 foregroundTiles.add(tilemap);
  114.             }
  115.             else if (tileLayerName == 'background')
  116.             {  
  117.                 backgroundTiles.add(tilemap);
  118.             }
  119.             else if (tileLayerName == 'collisions')
  120.             {
  121.                 /*
  122.                  * All collidable layers require the below code:
  123.                  */
  124.                 if (collidableTileLayers == null)
  125.                     collidableTileLayers = new Array<FlxTilemap>();
  126.                
  127.                 collisionTiles.add(tilemap);
  128.                
  129.                 collidableTileLayers.push(tilemap);
  130.             }
  131.         }
  132.     }
  133.    
  134.     public function loadObjects(state:PlayState)
  135.     {
  136.         for (group in objectGroups)
  137.         {
  138.             for (o in group.objects)
  139.             {
  140.                 loadObject(o, group, state);
  141.             }
  142.         }
  143.     }
  144.    
  145.     private function loadObject(o:TiledObject, g:TiledObjectGroup, state:PlayState)
  146.     {
  147.         var x:Int = o.x;
  148.         var y:Int = o.y;
  149.        
  150.         if (o.gid != -1)
  151.             y -= g.map.getGidOwner(o.gid).tileHeight;
  152.        
  153.         switch (o.type.toLowerCase())
  154.         {
  155.             /*
  156.              * This is for importing objects. The objects within Tiled require their "type" variable to equal something, for example "player"
  157.              *
  158.              * Example Object Loading:
  159.              *
  160.              * case "player":
  161.              *  _state.player.x = x;
  162.              *  _state.player.y = y;
  163.              *
  164.              * This would set the PlayState's player x and y variables to the object's x and y.
  165.              * (That should be obvious)
  166.              */
  167.            
  168.             case "null":
  169.                 //Don't do anything. You should delete this once you add actual objects.
  170.         }
  171.     }
  172.    
  173.     public function collideWithLevel(obj:FlxObject, ?notifyCallback:FlxObject->FlxObject->Void, ?processCallback:FlxObject->FlxObject->Bool):Bool
  174.     {
  175.         if (collidableTileLayers != null)
  176.         {
  177.             for (map in collidableTileLayers)
  178.             {
  179.                 return FlxG.overlap(map, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate);
  180.             }
  181.         }
  182.         return false;
  183.     }
  184. }
RAW Paste Data