Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding loading of polygon object #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions com/haxepunk/tmx/TmxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
* For questions mail me at [email protected]
******************************************************************************/
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;

Expand Down Expand Up @@ -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<String> = pointsString.split(' ');

var maskPoints:Array<Vector> = new Array<Vector>();

for (pair in pairStrings)
{
var pointStrings:Array<String> = 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<Float> = new Array<Float>();

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);
Expand All @@ -75,4 +114,60 @@ class TmxObject
#end
}
}

function createPolygon(points:Array<Float>)
{
//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<Int> = new flash.Vector<Int>();

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;
}
}