2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-16 02:06:07 +00:00
|
|
|
var GetTilesWithin = require('./GetTilesWithin');
|
2018-04-10 03:13:38 +00:00
|
|
|
var GetRandom = require('../../utils/array/GetRandom');
|
2017-11-16 02:06:07 +00:00
|
|
|
|
2017-11-27 13:33:30 +00:00
|
|
|
/**
|
2017-11-29 19:22:30 +00:00
|
|
|
* Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the
|
2018-02-08 01:08:59 +00:00
|
|
|
* specified layer. Each tile will receive a new index. If an array of indexes is passed in, then
|
2017-11-29 19:22:30 +00:00
|
|
|
* 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.
|
2017-11-27 13:33:30 +00:00
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.Randomize
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2020-11-23 10:48:24 +00:00
|
|
|
* @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.
|
2018-02-08 02:02:37 +00:00
|
|
|
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
|
2017-11-27 13:33:30 +00:00
|
|
|
*/
|
2017-11-29 19:22:30 +00:00
|
|
|
var Randomize = function (tileX, tileY, width, height, indexes, layer)
|
2017-11-16 02:06:07 +00:00
|
|
|
{
|
|
|
|
var i;
|
2020-11-26 12:55:40 +00:00
|
|
|
var tiles = GetTilesWithin(tileX, tileY, width, height, {}, layer);
|
2017-11-16 02:06:07 +00:00
|
|
|
|
2019-10-29 06:22:43 +00:00
|
|
|
// If no indices are given, then find all the unique indexes within the specified region
|
2020-11-23 10:48:24 +00:00
|
|
|
if (!indexes)
|
2017-11-16 02:06:07 +00:00
|
|
|
{
|
2017-11-29 19:22:30 +00:00
|
|
|
indexes = [];
|
2020-09-02 10:54:24 +00:00
|
|
|
|
2017-11-16 02:06:07 +00:00
|
|
|
for (i = 0; i < tiles.length; i++)
|
|
|
|
{
|
2017-11-29 19:22:30 +00:00
|
|
|
if (indexes.indexOf(tiles[i].index) === -1)
|
2017-11-16 02:06:07 +00:00
|
|
|
{
|
2017-11-29 19:22:30 +00:00
|
|
|
indexes.push(tiles[i].index);
|
2017-11-16 02:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < tiles.length; i++)
|
|
|
|
{
|
2018-04-10 03:13:38 +00:00
|
|
|
tiles[i].index = GetRandom(indexes);
|
2017-11-16 02:06:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-22 01:07:22 +00:00
|
|
|
module.exports = Randomize;
|