forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetAdvancedValue.js
More file actions
62 lines (56 loc) · 1.18 KB
/
Copy pathGetAdvancedValue.js
File metadata and controls
62 lines (56 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var MATH = require('../../math/const');
var GetValue = require('./GetValue');
// 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] }
// }
var GetAdvancedValue = function (source, key, defaultValue)
{
var value = GetValue(source, key, null);
if (value === null)
{
return defaultValue;
}
else if (Array.isArray(value))
{
return MATH.RND.pick(value);
}
else if (typeof value === 'object')
{
if (value.hasOwnProperty('randInt'))
{
return MATH.RND.integerInRange(value.randInt[0], value.randInt[1]);
}
else if (value.hasOwnProperty('randFloat'))
{
return MATH.RND.realInRange(value.randFloat[0], value.randFloat[1]);
}
}
else if (typeof value === 'function')
{
return value(key);
}
return value;
};
module.exports = GetAdvancedValue;