forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.js
More file actions
132 lines (114 loc) · 3.13 KB
/
Copy pathRange.js
File metadata and controls
132 lines (114 loc) · 3.13 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetValue = require('../object/GetValue');
var Shuffle = require('./Shuffle');
var BuildChunk = function (a, b, qty)
{
var out = [];
for (var aIndex = 0; aIndex < a.length; aIndex++)
{
for (var bIndex = 0; bIndex < b.length; bIndex++)
{
for (var 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.
*/
var Range = function (a, b, options)
{
var max = GetValue(options, 'max', 0);
var qty = GetValue(options, 'qty', 1);
var random = GetValue(options, 'random', false);
var randomB = GetValue(options, 'randomB', false);
var repeat = GetValue(options, 'repeat', 0);
var yoyo = GetValue(options, 'yoyo', false);
var out = [];
if (randomB)
{
Shuffle(b);
}
// Endless repeat, so limit by max
if (repeat === -1)
{
if (max === 0)
{
repeat = 0;
}
else
{
// Work out how many repeats we need
var total = (a.length * b.length) * qty;
if (yoyo)
{
total *= 2;
}
repeat = Math.ceil(max / total);
}
}
for (var i = 0; i <= repeat; i++)
{
var chunk = BuildChunk(a, b, qty);
if (random)
{
Shuffle(chunk);
}
out = out.concat(chunk);
if (yoyo)
{
chunk.reverse();
out = out.concat(chunk);
}
}
if (max)
{
out.splice(max);
}
return out;
};
module.exports = Range;