arcade-physics
Version:
Use Arcade Physics without Phaser.
73 lines • 2.15 kB
JavaScript
;
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberArrayStep = void 0;
const RoundAwayFromZero_1 = __importDefault(require("../../math/RoundAwayFromZero"));
/**
* Create an array of numbers (positive and/or negative) progressing from `start`
* up to but not including `end` by advancing by `step`.
*
* If `start` is less than `end` a zero-length range is created unless a negative `step` is specified.
*
* Certain values for `start` and `end` (eg. NaN/undefined/null) are currently coerced to 0;
* for forward compatibility make sure to pass in actual numbers.
*
* @example
* NumberArrayStep(4);
* // => [0, 1, 2, 3]
*
* NumberArrayStep(1, 5);
* // => [1, 2, 3, 4]
*
* NumberArrayStep(0, 20, 5);
* // => [0, 5, 10, 15]
*
* NumberArrayStep(0, -4, -1);
* // => [0, -1, -2, -3]
*
* NumberArrayStep(1, 4, 0);
* // => [1, 1, 1]
*
* NumberArrayStep(0);
* // => []
*
* @function Phaser.Utils.Array.NumberArrayStep
* @since 3.0.0
*
* @param {number} [start=0] - The start of the range.
* @param {number} [end=null] - The end of the range.
* @param {number} [step=1] - The value to increment or decrement by.
*
* @return {number[]} The array of number values.
*/
const NumberArrayStep = (start, end, step) => {
if (start === undefined) {
start = 0;
}
if (end === undefined) {
end = null;
}
if (step === undefined) {
step = 1;
}
if (end === null) {
end = start;
start = 0;
}
const result = [];
const total = Math.max((0, RoundAwayFromZero_1.default)((end - start) / (step || 1)), 0);
for (let i = 0; i < total; i++) {
result.push(start);
start += step;
}
return result;
};
exports.NumberArrayStep = NumberArrayStep;
//# sourceMappingURL=NumberArrayStep.js.map