Skip to content

Commit e8ca3a8

Browse files
committed
Merge pull request phaserjs#1130 from beeglebug/feature/clone-consistency
Improved consistency of clone methods on geometry classes
2 parents cc69a4b + 8ef5c5e commit e8ca3a8

5 files changed

Lines changed: 61 additions & 45 deletions

File tree

src/geom/Circle.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,21 @@ Phaser.Circle.prototype = {
135135
/**
136136
* Returns a new Circle object with the same values for the x, y, width, and height properties as this Circle object.
137137
* @method Phaser.Circle#clone
138-
* @param {Phaser.Circle} out - Optional Circle object. If given the values will be set into the object, otherwise a brand new Circle object will be created and returned.
138+
* @param {Phaser.Circle} output - Optional Circle object. If given the values will be set into the object, otherwise a brand new Circle object will be created and returned.
139139
* @return {Phaser.Circle} The cloned Circle object.
140140
*/
141-
clone: function (out) {
141+
clone: function (output) {
142142

143-
if (typeof out === "undefined")
143+
if (typeof output === "undefined" || output === null)
144144
{
145-
out = new Phaser.Circle(this.x, this.y, this.diameter);
145+
output = new Phaser.Circle(this.x, this.y, this.diameter);
146146
}
147147
else
148148
{
149-
out.setTo(this.x, this.y, this.diameter);
149+
output.setTo(this.x, this.y, this.diameter);
150150
}
151151

152-
return out;
152+
return output;
153153

154154
},
155155

src/geom/Ellipse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,21 @@ Phaser.Ellipse.prototype = {
101101
/**
102102
* Returns a new Ellipse object with the same values for the x, y, width, and height properties as this Ellipse object.
103103
* @method Phaser.Ellipse#clone
104-
* @param {Phaser.Ellipse} out - Optional Ellipse object. If given the values will be set into the object, otherwise a brand new Ellipse object will be created and returned.
104+
* @param {Phaser.Ellipse} output - Optional Ellipse object. If given the values will be set into the object, otherwise a brand new Ellipse object will be created and returned.
105105
* @return {Phaser.Ellipse} The cloned Ellipse object.
106106
*/
107-
clone: function(out) {
107+
clone: function(output) {
108108

109-
if (typeof out === "undefined")
109+
if (typeof output === "undefined" || output === null)
110110
{
111-
out = new Phaser.Ellipse(this.x, this.y, this.width, this.height);
111+
output = new Phaser.Ellipse(this.x, this.y, this.width, this.height);
112112
}
113113
else
114114
{
115-
out.setTo(this.x, this.y, this.width, this.height);
115+
output.setTo(this.x, this.y, this.width, this.height);
116116
}
117117

118-
return out;
118+
return output;
119119

120120
},
121121

src/geom/Line.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ Phaser.Line.prototype = {
182182

183183
return results;
184184

185+
},
186+
187+
/**
188+
* Returns a new Line object with the same values for the start and end properties as this Line object.
189+
* @method Phaser.Line#clone
190+
* @param {Phaser.Line} output - Optional Line object. If given the values will be set into the object, otherwise a brand new Line object will be created and returned.
191+
* @return {Phaser.Line} The cloned Line object.
192+
*/
193+
clone: function (output) {
194+
195+
if (typeof output === "undefined" || output === null)
196+
{
197+
output = new Phaser.Line(this.start.x, this.start.y, this.end.x, this.end.y);
198+
}
199+
else
200+
{
201+
output.setTo(this.start.x, this.start.y, this.end.x, this.end.y);
202+
}
203+
204+
return output;
205+
185206
}
186207

187208
};

src/geom/Point.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Phaser.Point.prototype = {
222222
*/
223223
clone: function (output) {
224224

225-
if (typeof output === "undefined")
225+
if (typeof output === "undefined" || output === null)
226226
{
227227
output = new Phaser.Point(this.x, this.y);
228228
}

src/geom/Polygon.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,20 @@ Phaser.Polygon = function (points) {
2323
*/
2424
this.type = Phaser.POLYGON;
2525

26-
//if points isn't an array, use arguments as the array
27-
if (!(points instanceof Array))
28-
{
29-
points = Array.prototype.slice.call(arguments);
30-
}
31-
32-
//if this is a flat array of numbers, convert it to points
33-
if (typeof points[0] === 'number')
34-
{
35-
var p = [];
36-
37-
for (var i = 0, len = points.length; i < len; i += 2)
38-
{
39-
p.push(new Phaser.Point(points[i], points[i + 1]));
40-
}
41-
42-
points = p;
43-
}
44-
45-
/**
46-
* @property {array<Phaser.Point>|array<number>} points - The array of vertex Points.
47-
* @private
48-
*/
49-
this._points = points;
50-
26+
this.points = points;
5127
};
5228

5329
Phaser.Polygon.prototype = {
5430

5531
/**
56-
* Creates a clone of this polygon.
57-
*
58-
* @method Phaser.Polygon#clone
59-
* @return {Phaser.Polygon} A copy of the polygon.
60-
*/
61-
clone: function () {
32+
* Creates a copy of the given Polygon.
33+
* This is a deep clone, the resulting copy contains new Phaser.Point objects
34+
*
35+
* @method Phaser.Polygon#clone
36+
* @param {Phaser.Polygon} [output] Optional Polygon object. If given the values will be set into this object, otherwise a brand new Polygon object will be created and returned.
37+
* @return {Phaser.Polygon} The new Polygon object.
38+
*/
39+
clone: function (output) {
6240

6341
var points = [];
6442

@@ -67,7 +45,16 @@ Phaser.Polygon.prototype = {
6745
points.push(this.points[i].clone());
6846
}
6947

70-
return new Phaser.Polygon(points);
48+
if (typeof output === "undefined" || output === null)
49+
{
50+
output = new Phaser.Polygon(points);
51+
}
52+
else
53+
{
54+
output.setTo(points);
55+
}
56+
57+
return output;
7158

7259
},
7360

@@ -101,6 +88,14 @@ Phaser.Polygon.prototype = {
10188

10289
return inside;
10390

91+
},
92+
93+
setTo : function(points) {
94+
95+
this.points = points;
96+
97+
return this;
98+
10499
}
105100

106101
};

0 commit comments

Comments
 (0)