Skip to content

Commit a35d032

Browse files
committed
Math/Utils - various cleanup and deprecations/moves
- Put in the missing changes..
1 parent a6d2da2 commit a35d032

2 files changed

Lines changed: 72 additions & 103 deletions

File tree

src/math/Math.js

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ Phaser.Math = {
435435
* @param {number} min - The minimum value the array starts with.
436436
* @param {number} max - The maximum value the array contains.
437437
* @return {array} The array of number values.
438+
* @deprecated 2.1.4 - Specialized function not used internally.
438439
*/
439440
numberArray: function (min, max) {
440441

@@ -450,34 +451,35 @@ Phaser.Math = {
450451
},
451452

452453
/**
453-
* Creates an array of numbers (positive and/or negative) progressing from
454-
* `start` up to but not including `end`. If `start` is less than `stop` a
455-
* zero-length range is created unless a negative `step` is specified.
456-
*
457-
* @method Phaser.Math#numberArrayStep
458-
* @param {number} [start=0] - The start of the range.
459-
* @param {number} end - The end of the range.
460-
* @param {number} [step=1] - The value to increment or decrement by.
461-
* @returns {Array} Returns the new array of numbers.
462-
* @example
463-
* Phaser.Math.numberArrayStep(4);
464-
* // => [0, 1, 2, 3]
465-
*
466-
* Phaser.Math.numberArrayStep(1, 5);
467-
* // => [1, 2, 3, 4]
468-
*
469-
* Phaser.Math.numberArrayStep(0, 20, 5);
470-
* // => [0, 5, 10, 15]
471-
*
472-
* Phaser.Math.numberArrayStep(0, -4, -1);
473-
* // => [0, -1, -2, -3]
474-
*
475-
* Phaser.Math.numberArrayStep(1, 4, 0);
476-
* // => [1, 1, 1]
477-
*
478-
* Phaser.Math.numberArrayStep(0);
479-
* // => []
480-
*/
454+
* Creates an array of numbers (positive and/or negative) progressing from
455+
* `start` up to but not including `end`. If `start` is less than `stop` a
456+
* zero-length range is created unless a negative `step` is specified.
457+
*
458+
* @method Phaser.Math#numberArrayStep
459+
* @param {number} [start=0] - The start of the range.
460+
* @param {number} end - The end of the range.
461+
* @param {number} [step=1] - The value to increment or decrement by.
462+
* @returns {Array} Returns the new array of numbers.
463+
* @example
464+
* Phaser.Math.numberArrayStep(4);
465+
* // => [0, 1, 2, 3]
466+
*
467+
* Phaser.Math.numberArrayStep(1, 5);
468+
* // => [1, 2, 3, 4]
469+
*
470+
* Phaser.Math.numberArrayStep(0, 20, 5);
471+
* // => [0, 5, 10, 15]
472+
*
473+
* Phaser.Math.numberArrayStep(0, -4, -1);
474+
* // => [0, -1, -2, -3]
475+
*
476+
* Phaser.Math.numberArrayStep(1, 4, 0);
477+
* // => [1, 1, 1]
478+
*
479+
* Phaser.Math.numberArrayStep(0);
480+
* // => []
481+
* @deprecated 2.1.4 - Specialized function not used internally.
482+
*/
481483
numberArrayStep: function(start, end, step) {
482484

483485
start = +start || 0;
@@ -1017,7 +1019,7 @@ Phaser.Math = {
10171019
* @method Phaser.Math#floor
10181020
* @param {number} value - Any number.
10191021
* @return {integer} The rounded value of that number.
1020-
* @deprecated 2.1.4 - Use {@link Phaser.Math#truncate} or instead.
1022+
* @deprecated 2.1.4 - Use {@link Phaser.Math#truncate} or `Math.trunc` instead.
10211023
*/
10221024
floor: function (value) {
10231025
return Math.trunc(value);
@@ -1170,9 +1172,7 @@ Phaser.Math = {
11701172
* @deprecated 2.1.4 - Do the rounding locally.
11711173
*/
11721174
distanceRounded: function (x1, y1, x2, y2) {
1173-
11741175
return Math.round(Phaser.Math.distance(x1, y1, x2, y2));
1175-
11761176
},
11771177

11781178
/**
@@ -1185,9 +1185,7 @@ Phaser.Math = {
11851185
* @return {number}
11861186
*/
11871187
clamp: function (x, a, b) {
1188-
11891188
return ( x < a ) ? a : ( ( x > b ) ? b : x );
1190-
11911189
},
11921190

11931191
/**
@@ -1200,9 +1198,7 @@ Phaser.Math = {
12001198
* @return {number}
12011199
*/
12021200
clampBottom: function (x, a) {
1203-
12041201
return x < a ? a : x;
1205-
12061202
},
12071203

12081204
/**
@@ -1216,9 +1212,7 @@ Phaser.Math = {
12161212
* @see {@link Phaser.Math.fuzzyEqual}
12171213
*/
12181214
within: function (a, b, tolerance) {
1219-
12201215
return (Math.abs(a - b) <= tolerance);
1221-
12221216
},
12231217

12241218
/**
@@ -1233,9 +1227,7 @@ Phaser.Math = {
12331227
* @return {number}
12341228
*/
12351229
mapLinear: function (x, a1, a2, b1, b2) {
1236-
12371230
return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1238-
12391231
},
12401232

12411233
/**
@@ -1248,10 +1240,8 @@ Phaser.Math = {
12481240
* @return {number}
12491241
*/
12501242
smoothstep: function (x, min, max) {
1251-
12521243
x = Math.max(0, Math.min(1, (x - min) / (max - min)));
12531244
return x * x * (3 - 2 * x);
1254-
12551245
},
12561246

12571247
/**
@@ -1264,10 +1254,8 @@ Phaser.Math = {
12641254
* @return {number}
12651255
*/
12661256
smootherstep: function (x, min, max) {
1267-
12681257
x = Math.max(0, Math.min(1, (x - min) / (max - min)));
12691258
return x * x * x * (x * (x * 6 - 15) + 10);
1270-
12711259
},
12721260

12731261
/**
@@ -1280,9 +1268,7 @@ Phaser.Math = {
12801268
* @return {integer} An integer in {-1, 0, 1}
12811269
*/
12821270
sign: function (x) {
1283-
12841271
return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
1285-
12861272
},
12871273

12881274
/**
@@ -1311,40 +1297,31 @@ Phaser.Math = {
13111297
return (a - base) / b;
13121298
}
13131299

1314-
},
1315-
1316-
/**
1317-
* Convert degrees to radians.
1318-
*
1319-
* @method Phaser.Math#degToRad
1320-
* @param {number} degrees - Angle in degrees.
1321-
* @return {number} Angle in radians.
1322-
*/
1323-
degToRad: (function() {
1300+
}
13241301

1325-
var degreeToRadiansFactor = Math.PI / 180;
1326-
1327-
return function (degrees) {
1328-
return degrees * degreeToRadiansFactor;
1329-
};
1330-
1331-
}()),
1332-
1333-
/**
1334-
* Convert degrees to radians.
1335-
*
1336-
* @method Phaser.Math#radToDeg
1337-
* @param {number} radians - Angle in radians.
1338-
* @return {number} Angle in degrees
1339-
*/
1340-
radToDeg: (function() {
1341-
1342-
var radianToDegreesFactor = 180 / Math.PI;
1302+
};
13431303

1344-
return function (radians) {
1345-
return radians * radianToDegreesFactor;
1346-
};
1304+
var degreeToRadiansFactor = Math.PI / 180;
1305+
var radianToDegreesFactor = 180 / Math.PI;
13471306

1348-
}())
1307+
/**
1308+
* Convert degrees to radians.
1309+
*
1310+
* @method Phaser.Math#degToRad
1311+
* @param {number} degrees - Angle in degrees.
1312+
* @return {number} Angle in radians.
1313+
*/
1314+
Phaser.Math.degToRad = function degToRad (degrees) {
1315+
return degrees * degreeToRadiansFactor;
1316+
};
13491317

1318+
/**
1319+
* Convert degrees to radians.
1320+
*
1321+
* @method Phaser.Math#radToDeg
1322+
* @param {number} radians - Angle in radians.
1323+
* @return {number} Angle in degrees
1324+
*/
1325+
Phaser.Math.radToDeg = function radToDeg (radians) {
1326+
return radians * radianToDegreesFactor;
13501327
};

src/utils/ArrayUtils.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,14 @@ Phaser.ArrayUtils = {
2626
*/
2727
getRandomItem: function (objects, startIndex, length) {
2828

29-
if (typeof startIndex === 'undefined') { startIndex = 0; }
30-
if (typeof length === 'undefined') { length = 0; }
31-
3229
if (objects == null) { // undefined or null
3330
return null;
3431
}
3532

36-
var l = length;
37-
38-
if ((l === 0) || (l > objects.length - startIndex))
39-
{
40-
l = objects.length - startIndex;
41-
}
33+
if (typeof startIndex === 'undefined') { startIndex = 0; }
34+
if (typeof length === 'undefined') { length = objects.length; }
4235

43-
var randomIndex = startIndex + Math.floor(Math.random() * l);
36+
var randomIndex = startIndex + Math.floor(Math.random() * length);
4437
return objects[randomIndex] || null;
4538

4639
},
@@ -57,29 +50,24 @@ Phaser.ArrayUtils = {
5750
*/
5851
removeRandomItem: function (objects, startIndex, length) {
5952

60-
if (typeof startIndex === 'undefined') { startIndex = 0; }
61-
if (typeof length === 'undefined') { length = 0; }
62-
6353
if (objects == null) { // undefined or null
6454
return null;
6555
}
6656

67-
var l = length;
57+
if (typeof startIndex === 'undefined') { startIndex = 0; }
58+
if (typeof length === 'undefined') { length = objects.length; }
6859

69-
if ((l === 0) || (l > objects.length - startIndex))
60+
var randomIndex = startIndex + Math.floor(Math.random() * length);
61+
if (randomIndex < objects.length)
7062
{
71-
l = objects.length - startIndex;
63+
var removed = objects.splice(randomIndex, 1);
64+
return removed[0];
7265
}
73-
74-
if (l > 0)
66+
else
7567
{
76-
var idx = startIndex + Math.floor(Math.random() * l);
77-
var removed = objects.splice(idx, 1);
78-
return removed[0];
68+
return null;
7969
}
8070

81-
return null;
82-
8371
},
8472

8573
/**
@@ -179,12 +167,16 @@ Phaser.ArrayUtils = {
179167
*/
180168
findClosest: function (value, arr) {
181169

182-
if (value < arr[0]) {
170+
if (!arr.length)
171+
{
172+
return NaN;
173+
}
174+
else if (arr.length === 1 || value < arr[0])
175+
{
183176
return arr[0];
184177
}
185178

186179
var i = 1;
187-
188180
while (arr[i] < value) {
189181
i++;
190182
}
@@ -194,6 +186,6 @@ Phaser.ArrayUtils = {
194186

195187
return ((high - value) <= (value - low)) ? high : low;
196188

197-
},
189+
}
198190

199191
};

0 commit comments

Comments
 (0)