-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Randomize.js
52 lines (46 loc) · 1.98 KB
/
Randomize.js
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
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetTilesWithin = require('./GetTilesWithin');
var GetRandom = require('../../utils/array/GetRandom');
/**
* Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the
* specified layer. Each tile will receive a new index. If an array of indexes is passed in, then
* those will be used for randomly assigning new tile indexes. If an array is not provided, the
* indexes found within the region (excluding -1) will be used for randomly assigning new tile
* indexes. This method only modifies tile indexes and does not change collision information.
*
* @function Phaser.Tilemaps.Components.Randomize
* @since 3.0.0
*
* @param {number} tileX - The left most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} tileY - The top most tile index (in tile coordinates) to use as the origin of the area.
* @param {number} width - How many tiles wide from the `tileX` index the area will be.
* @param {number} height - How many tiles tall from the `tileY` index the area will be.
* @param {number[]} indexes - An array of indexes to randomly draw from during randomization.
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
*/
var Randomize = function (tileX, tileY, width, height, indexes, layer)
{
var i;
var tiles = GetTilesWithin(tileX, tileY, width, height, {}, layer);
// If no indices are given, then find all the unique indexes within the specified region
if (!indexes)
{
indexes = [];
for (i = 0; i < tiles.length; i++)
{
if (indexes.indexOf(tiles[i].index) === -1)
{
indexes.push(tiles[i].index);
}
}
}
for (i = 0; i < tiles.length; i++)
{
tiles[i].index = GetRandom(indexes);
}
};
module.exports = Randomize;