arcade-physics
Version:
Use Arcade Physics without Phaser.
76 lines • 2.45 kB
JavaScript
;
/* eslint-disable no-prototype-builtins */
/**
* @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 });
const Random_js_1 = __importDefault(require("../../math/Random.js"));
const GetValue_1 = __importDefault(require("./GetValue"));
/**
* Retrieves a value from an object. Allows for more advanced selection options, including:
*
* Allowed types:
*
* Implicit
* {
* x: 4
* }
*
* From function
* {
* x: function ()
* }
*
* Randomly pick one element from the array
* {
* x: [a, b, c, d, e, f]
* }
*
* Random integer between min and max:
* {
* x: { randInt: [min, max] }
* }
*
* Random float between min and max:
* {
* x: { randFloat: [min, max] }
* }
*
*
* @function Phaser.Utils.Objects.GetAdvancedValue
* @since 3.0.0
*
* @param {object} source - The object to retrieve the value from.
* @param {string} key - The name of the property to retrieve from the object. If a property is nested, the names of its preceding properties should be separated by a dot (`.`) - `banner.hideBanner` would return the value of the `hideBanner` property from the object stored in the `banner` property of the `source` object.
* @param {*} defaultValue - The value to return if the `key` isn't found in the `source` object.
*
* @return {*} The value of the requested key.
*/
const GetAdvancedValue = (source, key, defaultValue) => {
const value = (0, GetValue_1.default)(source, key, null);
if (value === null) {
return defaultValue;
}
else if (Array.isArray(value)) {
return Random_js_1.default.pick(value);
}
else if (typeof value === 'object') {
if (value.hasOwnProperty('randInt')) {
return Random_js_1.default.integerInRange(value.randInt[0], value.randInt[1]);
}
else if (value.hasOwnProperty('randFloat')) {
return Random_js_1.default.realInRange(value.randFloat[0], value.randFloat[1]);
}
}
else if (typeof value === 'function') {
return value(key);
}
return value;
};
exports.default = GetAdvancedValue;
//# sourceMappingURL=GetAdvancedValue.js.map