Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import haxe.io.Path;
- import haxe.xml.Parser;
- import openfl.Assets;
- import flixel.FlxBasic;
- import flixel.FlxG;
- import flixel.FlxObject;
- import flixel.FlxSprite;
- import flixel.group.FlxGroup;
- import flixel.tile.FlxTilemap;
- import flixel.addons.editors.tiled.TiledMap;
- import flixel.addons.editors.tiled.TiledObject;
- import flixel.addons.editors.tiled.TiledObjectGroup;
- import flixel.addons.editors.tiled.TiledTileSet;
- class TestMap extends FlxGroup {
- private inline static var PATH_LEVEL_TILESHEETS = "assets/map/";
- public var background_tiles:FlxGroup = new FlxGroup();
- public var foreground_tiles:FlxGroup = new FlxGroup();
- public var map:TiledMap;
- private var collidable_tilemaps:Array<FlxTilemap> = new Array<FlxTilemap>();
- public function new(tiledLevel:String) {
- super();
- map = new TiledMap(PATH_LEVEL_TILESHEETS + tiledLevel + ".tmx");
- FlxG.camera.setBounds(0, 0, map.fullWidth, map.fullHeight, true);
- for (layer in map.layers) {
- var tileSheetName:String = layer.properties.get("tileset");
- if (tileSheetName == null) {
- throw "No property 'tileset' in your layer " + layer.name + ".";
- }
- var tileset:TiledTileSet = map.tilesets[tileSheetName];
- if (tileset == null) {
- throw "Tileset '" + tileSheetName + " not found." +
- "Did you mispell the 'tilesheet' property in " + layer.name + "' layer ?";
- }
- var image_path = PATH_LEVEL_TILESHEETS + tileset.imageSource;
- var tilemap:FlxTilemap = new FlxTilemap();
- tilemap.widthInTiles = map.width;
- tilemap.heightInTiles = map.height;
- tilemap.loadMap(layer.tileArray, image_path, tileset.tileWidth, tileset.tileHeight, 0, tileset.firstGID, 1, 1);
- if (layer.properties.contains("nocollide")) {
- background_tiles.add(tilemap);
- } else {
- foreground_tiles.add(tilemap);
- collidable_tilemaps.push(tilemap);
- }
- }
- add(background_tiles);
- add(foreground_tiles);
- }
- public function loadObjects(state:PlayState) {
- for (group in map.objectGroups) {
- for (o in group.objects) {
- loadObject(o, group, state);
- }
- }
- }
- public function loadObject(o:TiledObject, g:TiledObjectGroup, state:PlayState) {
- var x:Int = o.x;
- var y:Int = o.y;
- // objects in tiled are aligned bottom-left (top-left in flixel)
- if (o.gid != -1)
- y -= g.map.getGidOwner(o.gid).tileHeight;
- }
- public function collideWithLevel(obj:FlxObject,
- ?notifyCallback:FlxObject->FlxObject->Void,
- ?processCallback:FlxObject->FlxObject->Bool):Bool {
- for (tilemap in collidable_tilemaps) {
- // IMPORTANT: Always collide the map with objects, not the other way around.
- // This prevents odd collision errors (collision separation code off by 1 px).
- return FlxG.overlap(tilemap, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement