arcade-physics
Version:
Use Arcade Physics without Phaser.
107 lines • 3.51 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.Range = void 0;
const GetValue_1 = __importDefault(require("../object/GetValue"));
const Shuffle_1 = require("./Shuffle");
const BuildChunk = (a, b, qty) => {
const out = [];
for (let aIndex = 0; aIndex < a.length; aIndex++) {
for (let bIndex = 0; bIndex < b.length; bIndex++) {
for (let i = 0; i < qty; i++) {
out.push({ a: a[aIndex], b: b[bIndex] });
}
}
}
return out;
};
/**
* Creates an array populated with a range of values, based on the given arguments and configuration object.
*
* Range ([a,b,c], [1,2,3]) =
* a1, a2, a3, b1, b2, b3, c1, c2, c3
*
* Range ([a,b], [1,2,3], qty = 3) =
* a1, a1, a1, a2, a2, a2, a3, a3, a3, b1, b1, b1, b2, b2, b2, b3, b3, b3
*
* Range ([a,b,c], [1,2,3], repeat x1) =
* a1, a2, a3, b1, b2, b3, c1, c2, c3, a1, a2, a3, b1, b2, b3, c1, c2, c3
*
* Range ([a,b], [1,2], repeat -1 = endless, max = 14) =
* Maybe if max is set then repeat goes to -1 automatically?
* a1, a2, b1, b2, a1, a2, b1, b2, a1, a2, b1, b2, a1, a2 (capped at 14 elements)
*
* Range ([a], [1,2,3,4,5], random = true) =
* a4, a1, a5, a2, a3
*
* Range ([a, b], [1,2,3], random = true) =
* b3, a2, a1, b1, a3, b2
*
* Range ([a, b, c], [1,2,3], randomB = true) =
* a3, a1, a2, b2, b3, b1, c1, c3, c2
*
* Range ([a], [1,2,3,4,5], yoyo = true) =
* a1, a2, a3, a4, a5, a5, a4, a3, a2, a1
*
* Range ([a, b], [1,2,3], yoyo = true) =
* a1, a2, a3, b1, b2, b3, b3, b2, b1, a3, a2, a1
*
* @function Phaser.Utils.Array.Range
* @since 3.0.0
*
* @param {array} a - The first array of range elements.
* @param {array} b - The second array of range elements.
* @param {object} [options] - A range configuration object. Can contain: repeat, random, randomB, yoyo, max, qty.
*
* @return {array} An array of arranged elements.
*/
const Range = (a, b, options) => {
const max = (0, GetValue_1.default)(options, 'max', 0);
const qty = (0, GetValue_1.default)(options, 'qty', 1);
const random = (0, GetValue_1.default)(options, 'random', false);
const randomB = (0, GetValue_1.default)(options, 'randomB', false);
let repeat = (0, GetValue_1.default)(options, 'repeat', 0);
const yoyo = (0, GetValue_1.default)(options, 'yoyo', false);
let out = [];
if (randomB) {
(0, Shuffle_1.Shuffle)(b);
}
// Endless repeat, so limit by max
if (repeat === -1) {
if (max === 0) {
repeat = 0;
}
else {
// Work out how many repeats we need
let total = a.length * b.length * qty;
if (yoyo) {
total *= 2;
}
repeat = Math.ceil(max / total);
}
}
for (let i = 0; i <= repeat; i++) {
const chunk = BuildChunk(a, b, qty);
if (random) {
(0, Shuffle_1.Shuffle)(chunk);
}
out = out.concat(chunk);
if (yoyo) {
chunk.reverse();
out = out.concat(chunk);
}
}
if (max) {
out.splice(max);
}
return out;
};
exports.Range = Range;
//# sourceMappingURL=Range.js.map