Skip to content

Commit 4221b56

Browse files
authored
Merge pull request phaserjs#3153 from halgorithm/weighted-randomize-index-arrays
Weighted randomize index arrays
2 parents 15c45bd + 89e4289 commit 4221b56

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/gameobjects/tilemap/components/WeightedRandomize.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ var GetTilesWithin = require('./GetTilesWithin');
2020
* @param {integer} [width=max width based on tileX] - [description]
2121
* @param {integer} [height=max height based on tileY] - [description]
2222
* @param {object[]} [weightedIndexes] - An array of objects to randomly draw from during
23-
* randomization. They should be in the form: { index: 0, weight: 4 }.
23+
* randomization. They should be in the form: { index: 0, weight: 4 } or
24+
* { index: [0, 1], weight: 4 } if you wish to draw from multiple tile indexes.
2425
* @param {LayerData} layer - [description]
2526
*/
2627
var WeightedRandomize = function (tileX, tileY, width, height, weightedIndexes, layer)
@@ -48,7 +49,10 @@ var WeightedRandomize = function (tileX, tileY, width, height, weightedIndexes,
4849
sum += weightedIndexes[j].weight;
4950
if (rand <= sum)
5051
{
51-
randomIndex = weightedIndexes[j].index;
52+
var chosen = weightedIndexes[j].index;
53+
randomIndex = Array.isArray(chosen)
54+
? chosen[Math.floor(Math.random() * chosen.length)]
55+
: chosen;
5256
break;
5357
}
5458
}

0 commit comments

Comments
 (0)