phaser/src/tilemaps/components/Fill.js

48 lines
1.9 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');
var CalculateFacesWithin = require('./CalculateFacesWithin');
var SetTileCollision = require('./SetTileCollision');
/**
2017-11-26 15:33:19 +00:00
* Sets the tiles in the given rectangular area (in tile coordinates) of the layer with the
* specified index. Tiles will be set to collide if the given index is a colliding index.
2017-11-27 13:33:30 +00:00
* Collision information in the region will be recalculated.
*
2018-02-08 01:08:59 +00:00
* @function Phaser.Tilemaps.Components.Fill
* @since 3.0.0
*
2020-11-23 10:22:13 +00:00
* @param {number} index - The tile index to fill the area with.
* @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 {boolean} recalculateFaces - `true` if the faces data should be recalculated.
* @param {Phaser.Tilemaps.LayerData} layer - The tile layer to use. If not given the current layer is used.
*/
var Fill = function (index, tileX, tileY, width, height, recalculateFaces, layer)
{
var doesIndexCollide = (layer.collideIndexes.indexOf(index) !== -1);
var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer);
for (var i = 0; i < tiles.length; i++)
{
tiles[i].index = index;
SetTileCollision(tiles[i], doesIndexCollide);
}
if (recalculateFaces)
{
// Recalculate the faces within the area and neighboring tiles
CalculateFacesWithin(tileX - 1, tileY - 1, width + 2, height + 2, layer);
}
};
module.exports = Fill;