phaser/src/tilemaps/components/SwapByIndex.js

46 lines
1.6 KiB
JavaScript
Raw Normal View History

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
*/
var GetTilesWithin = require('./GetTilesWithin');
2017-11-27 13:33:30 +00:00
/**
* Scans the given rectangular area (given in tile coordinates) for tiles with an index matching
* `indexA` and swaps then with `indexB`. This only modifies the index and does not change collision
* information.
*
2018-02-08 01:08:59 +00:00
* @function Phaser.Tilemaps.Components.SwapByIndex
* @since 3.0.0
*
2020-11-23 10:22:13 +00:00
* @param {number} tileA - First tile index.
* @param {number} tileB - Second tile index.
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.
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
*/
var SwapByIndex = function (indexA, indexB, tileX, tileY, width, height, layer)
{
var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer);
for (var i = 0; i < tiles.length; i++)
{
if (tiles[i])
{
if (tiles[i].index === indexA)
{
tiles[i].index = indexB;
}
else if (tiles[i].index === indexB)
{
tiles[i].index = indexA;
}
}
}
};
module.exports = SwapByIndex;