From 095ac9eaf383b76050620c6446a76f9f5cc4c744 Mon Sep 17 00:00:00 2001 From: Richard McHale Date: Sun, 15 Jun 2014 12:14:19 +0100 Subject: [PATCH 1/4] Adding loading of polygon object --- com/haxepunk/tmx/TmxObject.hx | 88 +++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/com/haxepunk/tmx/TmxObject.hx b/com/haxepunk/tmx/TmxObject.hx index ca82890..623ef2a 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 flash.display.BitmapData; +import flash.geom.Matrix; +import flash.geom.Point; import haxe.xml.Fast; import com.haxepunk.masks.Hitbox; @@ -64,6 +71,36 @@ 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 Point(Std.parseFloat(pointStrings[0]), Std.parseFloat(pointStrings[1]))); + } + + shapeMask = new Polygon(maskPoints); + +#if debug + var graphicPoints:flash.Vector = new flash.Vector(); + + 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 +112,55 @@ class TmxObject #end } } + + function createPolygon(points:flash.Vector) + { + //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; } + } + + 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 From 9747610fb6df241b00ea51c15f7fdd143bae7014 Mon Sep 17 00:00:00 2001 From: ramchale Date: Sat, 21 Jun 2014 20:34:23 +0100 Subject: [PATCH 2/4] setting x and y (oops) --- com/haxepunk/tmx/TmxObject.hx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/com/haxepunk/tmx/TmxObject.hx b/com/haxepunk/tmx/TmxObject.hx index 623ef2a..f8a7247 100644 --- a/com/haxepunk/tmx/TmxObject.hx +++ b/com/haxepunk/tmx/TmxObject.hx @@ -87,6 +87,8 @@ class TmxObject } shapeMask = new Polygon(maskPoints); + shapeMask.x = x; + shapeMask.y = y; #if debug var graphicPoints:flash.Vector = new flash.Vector(); From 6a5071219ec80783e64479c1242f9126c8cc9ae7 Mon Sep 17 00:00:00 2001 From: ramchale Date: Sun, 22 Jun 2014 14:57:30 +0100 Subject: [PATCH 3/4] Fixing polygon drawing where 0,0 is not the origin --- com/haxepunk/tmx/TmxObject.hx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/com/haxepunk/tmx/TmxObject.hx b/com/haxepunk/tmx/TmxObject.hx index f8a7247..58ec515 100644 --- a/com/haxepunk/tmx/TmxObject.hx +++ b/com/haxepunk/tmx/TmxObject.hx @@ -130,6 +130,10 @@ class TmxObject 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 @@ -138,6 +142,7 @@ class TmxObject commands.push(2); //Line to } + HXP.sprite.graphics.clear(); HXP.sprite.graphics.beginFill(0xFFFFFF); From 1bd2edb6e60cbb907067914075a3c722e20ee9c4 Mon Sep 17 00:00:00 2001 From: Richard McHale Date: Mon, 11 Aug 2014 16:39:24 +0100 Subject: [PATCH 4/4] More generic and work with HaxePunk 2.5.3 --- com/haxepunk/tmx/TmxObject.hx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/com/haxepunk/tmx/TmxObject.hx b/com/haxepunk/tmx/TmxObject.hx index 58ec515..e976adc 100644 --- a/com/haxepunk/tmx/TmxObject.hx +++ b/com/haxepunk/tmx/TmxObject.hx @@ -8,9 +8,9 @@ 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 flash.geom.Point; import haxe.xml.Fast; import com.haxepunk.masks.Hitbox; @@ -78,12 +78,12 @@ class TmxObject var pointsString:String = polygon_node.att.points; var pairStrings:Array = pointsString.split(' '); - var maskPoints:Array = new Array(); + var maskPoints:Array = new Array(); for (pair in pairStrings) { var pointStrings:Array = pair.split(','); - maskPoints.push(new Point(Std.parseFloat(pointStrings[0]), Std.parseFloat(pointStrings[1]))); + maskPoints.push(new Vector(Std.parseFloat(pointStrings[0]), Std.parseFloat(pointStrings[1]))); } shapeMask = new Polygon(maskPoints); @@ -91,7 +91,7 @@ class TmxObject shapeMask.y = y; #if debug - var graphicPoints:flash.Vector = new flash.Vector(); + var graphicPoints:Array = new Array(); for (point in maskPoints) { @@ -115,7 +115,7 @@ class TmxObject } } - function createPolygon(points:flash.Vector) + function createPolygon(points:Array) { //Find limits for image var minX:Float = 0, maxX:Float = 0, minY:Float = 0, maxY:Float = 0;