77*
88* Phaser - http://phaser.io
99*
10- * v2.1.3 "Ravinda" - Built: Wed Oct 22 2014 22:48:37
10+ * v2.1.3 "Ravinda" - Built: Wed Oct 22 2014 23:49:48
1111*
1212* By Richard Davey http://www.photonstorm.com @photonstorm
1313*
@@ -153,6 +153,83 @@ PIXI.sayHello = function (type)
153153 PIXI.dontSayHello = true;
154154};
155155
156+ /**
157+ * @author Adrien Brault <adrien.brault@gmail.com>
158+ */
159+
160+ /**
161+ * @class Polygon
162+ * @constructor
163+ * @param points* {Array<Point>|Array<Number>|Point...|Number...} This can be an array of Points that form the polygon,
164+ * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be
165+ * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the
166+ * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are
167+ * Numbers.
168+ */
169+ PIXI.Polygon = function(points)
170+ {
171+ //if points isn't an array, use arguments as the array
172+ if(!(points instanceof Array))points = Array.prototype.slice.call(arguments);
173+
174+ //if this is a flat array of numbers, convert it to points
175+ if(points[0] instanceof PIXI.Point)
176+ {
177+ var p = [];
178+ for(var i = 0, il = points.length; i < il; i++)
179+ {
180+ p.push(points[i].x, points[i].y);
181+ }
182+
183+ points = p;
184+ }
185+
186+ this.closed = true;
187+ this.points = points;
188+ };
189+
190+ /**
191+ * Creates a clone of this polygon
192+ *
193+ * @method clone
194+ * @return {Polygon} a copy of the polygon
195+ */
196+ PIXI.Polygon.prototype.clone = function()
197+ {
198+ var points = this.points.slice();
199+ return new PIXI.Polygon(points);
200+ };
201+
202+ /**
203+ * Checks whether the x and y coordinates passed to this function are contained within this polygon
204+ *
205+ * @method contains
206+ * @param x {Number} The X coordinate of the point to test
207+ * @param y {Number} The Y coordinate of the point to test
208+ * @return {Boolean} Whether the x/y coordinates are within this polygon
209+ */
210+ PIXI.Polygon.prototype.contains = function(x, y)
211+ {
212+ var inside = false;
213+
214+ // use some raycasting to test hits
215+ // https://github.com/substack/point-in-polygon/blob/master/index.js
216+ var length = this.points.length / 2;
217+
218+ for(var i = 0, j = length - 1; i < length; j = i++)
219+ {
220+ var xi = this.points[i * 2], yi = this.points[i * 2 + 1],
221+ xj = this.points[j * 2], yj = this.points[j * 2 + 1],
222+ intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
223+
224+ if(intersect) inside = !inside;
225+ }
226+
227+ return inside;
228+ };
229+
230+ // constructor
231+ PIXI.Polygon.prototype.constructor = PIXI.Polygon;
232+
156233/**
157234 * @author Mat Groves http://matgroves.com/ @Doormat23
158235 */
@@ -11477,7 +11554,7 @@ PIXI.AbstractFilter.prototype.apply = function(frameBuffer)
1147711554*
1147811555* Phaser - http://phaser.io
1147911556*
11480- * v2.1.3 "Ravinda" - Built: Wed Oct 22 2014 22:48:37
11557+ * v2.1.3 "Ravinda" - Built: Wed Oct 22 2014 23:49:48
1148111558*
1148211559* By Richard Davey http://www.photonstorm.com @photonstorm
1148311560*
@@ -15272,13 +15349,39 @@ Phaser.Polygon = function (points) {
1527215349 */
1527315350 this.type = Phaser.POLYGON;
1527415351
15352+ // If points isn't an array, use arguments as the array
15353+ if (!(points instanceof Array))
15354+ {
15355+ points = Array.prototype.slice.call(arguments);
15356+ }
15357+
15358+ // If this is a flat array of numbers, convert it to points
15359+ if (points[0] instanceof Phaser.Point)
15360+ {
15361+ var p = [];
15362+
15363+ for (var i = 0, il = points.length; i < il; i++)
15364+ {
15365+ p.push(points[i].x, points[i].y);
15366+ }
15367+
15368+ points = p;
15369+ }
15370+
15371+ /**
15372+ * @property {array} points - An array of Points that make up this Polygon.
15373+ */
1527515374 this.points = points;
15375+
15376+ /**
15377+ * @property {boolean} closed - Is the Polygon closed or not?
15378+ */
15379+ this.closed = true;
15380+
1527615381};
1527715382
1527815383Phaser.Polygon.prototype = {
1527915384
15280- type: null,
15281-
1528215385 /**
1528315386 * Creates a copy of the given Polygon.
1528415387 * This is a deep clone, the resulting copy contains new Phaser.Point objects
@@ -15289,12 +15392,7 @@ Phaser.Polygon.prototype = {
1528915392 */
1529015393 clone: function (output) {
1529115394
15292- var points = [];
15293-
15294- for (var i=0; i < this.points.length; i++)
15295- {
15296- points.push(this.points[i].clone());
15297- }
15395+ var points = this.points.slice();
1529815396
1529915397 if (typeof output === "undefined" || output === null)
1530015398 {
@@ -15322,12 +15420,15 @@ Phaser.Polygon.prototype = {
1532215420 var inside = false;
1532315421
1532415422 // use some raycasting to test hits https://github.com/substack/point-in-polygon/blob/master/index.js
15325- for (var i = 0, j = this.points.length - 1; i < this.points.length; j = i++)
15423+
15424+ var length = this.points.length / 2;
15425+
15426+ for (var i = 0, j = length - 1; i < length; j = i++)
1532615427 {
15327- var xi = this.points[i].x;
15328- var yi = this.points[i].y;
15329- var xj = this.points[j].x;
15330- var yj = this.points[j].y;
15428+ var xi = this.points[i * 2 ].x;
15429+ var yi = this.points[i * 2 + 1 ].y;
15430+ var xj = this.points[j * 2 ].x;
15431+ var yj = this.points[j * 2 + 1 ].y;
1533115432
1533215433 var intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
1533315434
@@ -15341,7 +15442,33 @@ Phaser.Polygon.prototype = {
1534115442
1534215443 },
1534315444
15344- setTo : function(points) {
15445+ /**
15446+ * Sets this Polygon to the given points.
15447+ *
15448+ * @method Phaser.Polygon#setTo
15449+ * @param {Phaser.Point[]|number[]} points - The array of Points.
15450+ * @return {boolean} True if the coordinates are within this polygon, otherwise false.
15451+ */
15452+ setTo: function (points) {
15453+
15454+ // If points isn't an array, use arguments as the array
15455+ if (!(points instanceof Array))
15456+ {
15457+ points = Array.prototype.slice.call(arguments);
15458+ }
15459+
15460+ // If this is a flat array of numbers, convert it to points
15461+ if (points[0] instanceof Phaser.Point)
15462+ {
15463+ var p = [];
15464+
15465+ for (var i = 0, il = points.length; i < il; i++)
15466+ {
15467+ p.push(points[i].x, points[i].y);
15468+ }
15469+
15470+ points = p;
15471+ }
1534515472
1534615473 this.points = points;
1534715474
@@ -15394,7 +15521,7 @@ Object.defineProperty(Phaser.Polygon.prototype, 'points', {
1539415521/**
1539515522* Returns the area of the polygon.
1539615523*
15397- * @name Phaser.Circle#right
15524+ * @name Phaser.Polygon#area
1539815525* @readonly
1539915526*/
1540015527Object.defineProperty(Phaser.Polygon.prototype, 'area', {
@@ -15443,7 +15570,7 @@ Object.defineProperty(Phaser.Polygon.prototype, 'area', {
1544315570});
1544415571
1544515572// Because PIXI uses its own Polygon, we'll replace it with ours to avoid duplicating code or confusion.
15446- PIXI.Polygon = Phaser.Polygon;
15573+ // PIXI.Polygon = Phaser.Polygon;
1544715574
1544815575/**
1544915576 * @author Mat Groves http://matgroves.com/ @Doormat23
@@ -40312,24 +40439,6 @@ Phaser.Graphics.prototype.destroy = function(destroyChildren) {
4031240439
4031340440};
4031440441
40315- /*
40316- * Draws a {Phaser.Polygon} or a {PIXI.Polygon} filled
40317- *
40318- * @method Phaser.Graphics.prototype.drawPolygon
40319- */
40320- Phaser.Graphics.prototype.drawPolygon = function (poly) {
40321-
40322- this.moveTo(poly.points[0].x, poly.points[0].y);
40323-
40324- for (var i = 1; i < poly.points.length; i += 1)
40325- {
40326- this.lineTo(poly.points[i].x, poly.points[i].y);
40327- }
40328-
40329- this.lineTo(poly.points[0].x, poly.points[0].y);
40330-
40331- };
40332-
4033340442/*
4033440443* Draws a single {Phaser.Polygon} triangle from a {Phaser.Point} array
4033540444*
0 commit comments