Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.29 KB | None | 0 0
  1. import haxe.io.Path;
  2. import haxe.xml.Parser;
  3.  
  4. import openfl.Assets;
  5.  
  6. import flixel.FlxBasic;
  7. import flixel.FlxG;
  8. import flixel.FlxObject;
  9. import flixel.FlxSprite;
  10. import flixel.group.FlxGroup;
  11. import flixel.tile.FlxTilemap;
  12. import flixel.addons.editors.tiled.TiledMap;
  13. import flixel.addons.editors.tiled.TiledObject;
  14. import flixel.addons.editors.tiled.TiledObjectGroup;
  15. import flixel.addons.editors.tiled.TiledTileSet;
  16.  
  17.  
  18. class TestMap extends FlxGroup {
  19.     private inline static var PATH_LEVEL_TILESHEETS = "assets/map/";
  20.  
  21.     public var background_tiles:FlxGroup = new FlxGroup();
  22.     public var foreground_tiles:FlxGroup = new FlxGroup();
  23.     public var map:TiledMap;
  24.     private var collidable_tilemaps:Array<FlxTilemap> = new Array<FlxTilemap>();
  25.  
  26.     public function new(tiledLevel:String) {
  27.         super();
  28.         map = new TiledMap(PATH_LEVEL_TILESHEETS + tiledLevel + ".tmx");
  29.  
  30.         FlxG.camera.setBounds(0, 0, map.fullWidth, map.fullHeight, true);
  31.  
  32.         for (layer in map.layers) {
  33.             var tileSheetName:String = layer.properties.get("tileset");
  34.             if (tileSheetName == null) {
  35.                 throw "No property 'tileset' in your layer " + layer.name + ".";
  36.             }
  37.  
  38.             var tileset:TiledTileSet = map.tilesets[tileSheetName];
  39.  
  40.             if (tileset == null) {
  41.                 throw "Tileset '" + tileSheetName + " not found." +
  42.                     "Did you mispell the 'tilesheet' property in " + layer.name + "' layer ?";
  43.             }
  44.  
  45.             var image_path = PATH_LEVEL_TILESHEETS + tileset.imageSource;
  46.  
  47.             var tilemap:FlxTilemap = new FlxTilemap();
  48.             tilemap.widthInTiles = map.width;
  49.             tilemap.heightInTiles = map.height;
  50.             tilemap.loadMap(layer.tileArray, image_path, tileset.tileWidth, tileset.tileHeight, 0, tileset.firstGID, 1, 1);
  51.  
  52.             if (layer.properties.contains("nocollide")) {
  53.                 background_tiles.add(tilemap);
  54.             } else {
  55.                 foreground_tiles.add(tilemap);
  56.                 collidable_tilemaps.push(tilemap);
  57.             }
  58.         }
  59.         add(background_tiles);
  60.         add(foreground_tiles);
  61.     }
  62.  
  63.     public function loadObjects(state:PlayState) {
  64.         for (group in map.objectGroups) {
  65.             for (o in group.objects) {
  66.                 loadObject(o, group, state);
  67.             }
  68.         }
  69.     }
  70.  
  71.     public function loadObject(o:TiledObject, g:TiledObjectGroup, state:PlayState) {
  72.         var x:Int = o.x;
  73.         var y:Int = o.y;
  74.  
  75.         // objects in tiled are aligned bottom-left (top-left in flixel)
  76.         if (o.gid != -1)
  77.             y -= g.map.getGidOwner(o.gid).tileHeight;
  78.  
  79.  
  80.     }
  81.  
  82.     public function collideWithLevel(obj:FlxObject,
  83.                                     ?notifyCallback:FlxObject->FlxObject->Void,
  84.                                     ?processCallback:FlxObject->FlxObject->Bool):Bool {
  85.         for (tilemap in collidable_tilemaps) {
  86.             // IMPORTANT: Always collide the map with objects, not the other way around.
  87.             //            This prevents odd collision errors (collision separation code off by 1 px).
  88.             return FlxG.overlap(tilemap, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate);
  89.         }
  90.         return false;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement