diff --git a/com/haxepunk/tmx/TmxObject.hx b/com/haxepunk/tmx/TmxObject.hx index ca82890..e976adc 100644 --- a/com/haxepunk/tmx/TmxObject.hx +++ b/com/haxepunk/tmx/TmxObject.hx @@ -4,6 +4,13 @@ * For questions mail me at heardtheword@gmail.com ******************************************************************************/ package com.haxepunk.tmx; +import com.haxepunk.Graphic; +import com.haxepunk.graphics.atlas.Atlas; +import com.haxepunk.graphics.Image; +import com.haxepunk.masks.Polygon; +import com.haxepunk.math.Vector; +import flash.display.BitmapData; +import flash.geom.Matrix; import haxe.xml.Fast; import com.haxepunk.masks.Hitbox; @@ -64,6 +71,38 @@ class TmxObject debug_graphic = com.haxepunk.graphics.Image.createCircle(radius, 0xff0000, .6); debug_graphic.x = x; debug_graphic.y = y; +#end + }else if (source.hasNode.polygon) { + var polygon_node:Fast = source.node.polygon; + + var pointsString:String = polygon_node.att.points; + var pairStrings:Array = pointsString.split(' '); + + var maskPoints:Array = new Array(); + + for (pair in pairStrings) + { + var pointStrings:Array = pair.split(','); + maskPoints.push(new Vector(Std.parseFloat(pointStrings[0]), Std.parseFloat(pointStrings[1]))); + } + + shapeMask = new Polygon(maskPoints); + shapeMask.x = x; + shapeMask.y = y; + +#if debug + var graphicPoints:Array = new Array(); + + for (point in maskPoints) + { + graphicPoints.push(point.x); + graphicPoints.push(point.y); + } + + debug_graphic = createPolygon(graphicPoints); + + debug_graphic.x += x; + debug_graphic.y += y; #end }else{ // rect shapeMask = new com.haxepunk.masks.Hitbox(width, height, x, y); @@ -75,4 +114,60 @@ class TmxObject #end } } + + function createPolygon(points:Array) + { + //Find limits for image + var minX:Float = 0, maxX:Float = 0, minY:Float = 0, maxY:Float = 0; + for (i in 0 ... Std.int(points.length / 2)) + { + var x:Float = points[i * 2]; + var y:Float = points[(i * 2) + 1]; + + if (x < minX) { minX = x; } + if (x > maxX) { maxX = x; } + if (y < minY) { minY = y; } + if (y > maxY) { maxY = y; } + } + + //Make it join back to the first point, otherwise it'll default to 0,0 + points.push(points[0]); + points.push(points[1]); + + var commands:flash.Vector = new flash.Vector(); + + commands.push(1); //Move to + for (i in 0 ... Std.int(points.length / 2)) + { + commands.push(2); //Line to + } + + + HXP.sprite.graphics.clear(); + + HXP.sprite.graphics.beginFill(0xFFFFFF); + HXP.sprite.graphics.drawPath(commands, points); + + var data:BitmapData = HXP.createBitmap(Std.int(maxX - minX), Std.int(maxY - minY), true, 0); + var transform:Matrix = new Matrix( 1, 0, 0, 1, -minX, -minY ); + data.draw(HXP.sprite, transform); + + var image:Image; + if (HXP.renderMode == RenderMode.HARDWARE) + { + image = new Image(Atlas.loadImageAsRegion(data)); + } + else + { + image = new Image(data); + } + + image.color = 0xff0000; + image.alpha = 0.6; + + image.x = minX; + image.y = minY; + + return image; + } } \ No newline at end of file