2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 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-21 02:06:49 +00:00
|
|
|
var SetTileCollision = require('./SetTileCollision');
|
2017-11-22 01:18:34 +00:00
|
|
|
var CalculateFacesWithin = require('./CalculateFacesWithin');
|
2017-11-22 01:13:43 +00:00
|
|
|
var SetLayerCollisionIndex = require('./SetLayerCollisionIndex');
|
2017-11-21 02:06:49 +00:00
|
|
|
|
2017-11-27 13:33:30 +00:00
|
|
|
/**
|
|
|
|
* Sets collision on the given tile or tiles within a layer by index. You can pass in either a
|
|
|
|
* single numeric index or an array of indexes: [2, 3, 15, 20]. The `collides` parameter controls if
|
|
|
|
* collision will be enabled (true) or disabled (false).
|
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.SetCollision
|
2018-04-16 14:25:22 +00:00
|
|
|
* @private
|
2018-02-08 01:08:59 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-20 15:11:33 +00:00
|
|
|
* @param {(integer|array)} indexes - Either a single tile index, or an array of tile indexes.
|
2018-09-28 13:32:36 +00:00
|
|
|
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
|
|
|
|
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate the tile faces after the update.
|
2018-02-08 02:02:37 +00:00
|
|
|
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
|
2019-10-02 14:15:18 +00:00
|
|
|
* @param {boolean} updateLayer - If true, updates the current tiles on the layer. Set to
|
2018-07-19 15:59:35 +00:00
|
|
|
* false if no tiles have been placed for significant performance boost.
|
2017-11-27 13:33:30 +00:00
|
|
|
*/
|
2018-07-19 15:59:35 +00:00
|
|
|
var SetCollision = function (indexes, collides, recalculateFaces, layer, updateLayer)
|
2017-11-21 02:06:49 +00:00
|
|
|
{
|
|
|
|
if (collides === undefined) { collides = true; }
|
|
|
|
if (recalculateFaces === undefined) { recalculateFaces = true; }
|
|
|
|
if (!Array.isArray(indexes)) { indexes = [ indexes ]; }
|
2018-07-19 15:59:35 +00:00
|
|
|
if (updateLayer === undefined) { updateLayer = true; }
|
2017-11-21 02:06:49 +00:00
|
|
|
|
2017-11-22 01:13:43 +00:00
|
|
|
// Update the array of colliding indexes
|
|
|
|
for (var i = 0; i < indexes.length; i++)
|
|
|
|
{
|
|
|
|
SetLayerCollisionIndex(indexes[i], collides, layer);
|
|
|
|
}
|
2018-07-19 15:59:35 +00:00
|
|
|
|
2017-11-22 01:13:43 +00:00
|
|
|
// Update the tiles
|
2019-10-02 14:15:18 +00:00
|
|
|
if (updateLayer)
|
2017-11-21 02:06:49 +00:00
|
|
|
{
|
2018-07-19 15:59:35 +00:00
|
|
|
for (var ty = 0; ty < layer.height; ty++)
|
2017-11-21 02:06:49 +00:00
|
|
|
{
|
2018-07-19 15:59:35 +00:00
|
|
|
for (var tx = 0; tx < layer.width; tx++)
|
2017-11-21 02:06:49 +00:00
|
|
|
{
|
2018-07-19 15:59:35 +00:00
|
|
|
var tile = layer.data[ty][tx];
|
|
|
|
|
|
|
|
if (tile && indexes.indexOf(tile.index) !== -1)
|
|
|
|
{
|
|
|
|
SetTileCollision(tile, collides);
|
|
|
|
}
|
2017-11-21 02:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:32:36 +00:00
|
|
|
if (recalculateFaces)
|
|
|
|
{
|
|
|
|
CalculateFacesWithin(0, 0, layer.width, layer.height, layer);
|
|
|
|
}
|
2017-11-21 02:06:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SetCollision;
|