@@ -22,13 +22,39 @@ Phaser.Polygon = function (points) {
2222 */
2323 this . type = Phaser . POLYGON ;
2424
25+ // If points isn't an array, use arguments as the array
26+ if ( ! ( points instanceof Array ) )
27+ {
28+ points = Array . prototype . slice . call ( arguments ) ;
29+ }
30+
31+ // If this is a flat array of numbers, convert it to points
32+ if ( points [ 0 ] instanceof Phaser . Point )
33+ {
34+ var p = [ ] ;
35+
36+ for ( var i = 0 , il = points . length ; i < il ; i ++ )
37+ {
38+ p . push ( points [ i ] . x , points [ i ] . y ) ;
39+ }
40+
41+ points = p ;
42+ }
43+
44+ /**
45+ * @property {array } points - An array of Points that make up this Polygon.
46+ */
2547 this . points = points ;
48+
49+ /**
50+ * @property {boolean } closed - Is the Polygon closed or not?
51+ */
52+ this . closed = true ;
53+
2654} ;
2755
2856Phaser . Polygon . prototype = {
2957
30- type : null ,
31-
3258 /**
3359 * Creates a copy of the given Polygon.
3460 * This is a deep clone, the resulting copy contains new Phaser.Point objects
@@ -39,12 +65,7 @@ Phaser.Polygon.prototype = {
3965 */
4066 clone : function ( output ) {
4167
42- var points = [ ] ;
43-
44- for ( var i = 0 ; i < this . points . length ; i ++ )
45- {
46- points . push ( this . points [ i ] . clone ( ) ) ;
47- }
68+ var points = this . points . slice ( ) ;
4869
4970 if ( typeof output === "undefined" || output === null )
5071 {
@@ -72,12 +93,15 @@ Phaser.Polygon.prototype = {
7293 var inside = false ;
7394
7495 // use some raycasting to test hits https://github.com/substack/point-in-polygon/blob/master/index.js
75- for ( var i = 0 , j = this . points . length - 1 ; i < this . points . length ; j = i ++ )
96+
97+ var length = this . points . length / 2 ;
98+
99+ for ( var i = 0 , j = length - 1 ; i < length ; j = i ++ )
76100 {
77- var xi = this . points [ i ] . x ;
78- var yi = this . points [ i ] . y ;
79- var xj = this . points [ j ] . x ;
80- var yj = this . points [ j ] . y ;
101+ var xi = this . points [ i * 2 ] . x ;
102+ var yi = this . points [ i * 2 + 1 ] . y ;
103+ var xj = this . points [ j * 2 ] . x ;
104+ var yj = this . points [ j * 2 + 1 ] . y ;
81105
82106 var intersect = ( ( yi > y ) !== ( yj > y ) ) && ( x < ( xj - xi ) * ( y - yi ) / ( yj - yi ) + xi ) ;
83107
@@ -91,7 +115,33 @@ Phaser.Polygon.prototype = {
91115
92116 } ,
93117
94- setTo : function ( points ) {
118+ /**
119+ * Sets this Polygon to the given points.
120+ *
121+ * @method Phaser.Polygon#setTo
122+ * @param {Phaser.Point[]|number[] } points - The array of Points.
123+ * @return {boolean } True if the coordinates are within this polygon, otherwise false.
124+ */
125+ setTo : function ( points ) {
126+
127+ // If points isn't an array, use arguments as the array
128+ if ( ! ( points instanceof Array ) )
129+ {
130+ points = Array . prototype . slice . call ( arguments ) ;
131+ }
132+
133+ // If this is a flat array of numbers, convert it to points
134+ if ( points [ 0 ] instanceof Phaser . Point )
135+ {
136+ var p = [ ] ;
137+
138+ for ( var i = 0 , il = points . length ; i < il ; i ++ )
139+ {
140+ p . push ( points [ i ] . x , points [ i ] . y ) ;
141+ }
142+
143+ points = p ;
144+ }
95145
96146 this . points = points ;
97147
@@ -144,7 +194,7 @@ Object.defineProperty(Phaser.Polygon.prototype, 'points', {
144194/**
145195* Returns the area of the polygon.
146196*
147- * @name Phaser.Circle#right
197+ * @name Phaser.Polygon#area
148198* @readonly
149199*/
150200Object . defineProperty ( Phaser . Polygon . prototype , 'area' , {
@@ -193,4 +243,4 @@ Object.defineProperty(Phaser.Polygon.prototype, 'area', {
193243} ) ;
194244
195245// Because PIXI uses its own Polygon, we'll replace it with ours to avoid duplicating code or confusion.
196- PIXI . Polygon = Phaser . Polygon ;
246+ // PIXI.Polygon = Phaser.Polygon;
0 commit comments