2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
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 a range of tiles in a layer whose index is between the specified `start` and
|
|
|
|
* `stop` (inclusive). Calling this with a start value of 10 and a stop value of 14 would set
|
|
|
|
* collision for tiles 10, 11, 12, 13 and 14. 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.SetCollisionBetween
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2017-11-29 21:07:56 +00:00
|
|
|
* @param {integer} start - The first index of the tile to be set for collision.
|
|
|
|
* @param {integer} stop - The last index of the tile to be set for collision.
|
2017-11-27 13:33:30 +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.
|
2017-11-27 13:33:30 +00:00
|
|
|
*/
|
2017-11-21 02:06:49 +00:00
|
|
|
var SetCollisionBetween = function (start, stop, collides, recalculateFaces, layer)
|
|
|
|
{
|
|
|
|
if (collides === undefined) { collides = true; }
|
|
|
|
if (recalculateFaces === undefined) { recalculateFaces = true; }
|
|
|
|
|
|
|
|
if (start > stop) { return; }
|
|
|
|
|
2017-11-22 01:13:43 +00:00
|
|
|
// Update the array of colliding indexes
|
|
|
|
for (var index = start; index <= stop; index++)
|
|
|
|
{
|
|
|
|
SetLayerCollisionIndex(index, collides, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the tiles
|
2017-11-21 02:06:49 +00:00
|
|
|
for (var ty = 0; ty < layer.height; ty++)
|
|
|
|
{
|
|
|
|
for (var tx = 0; tx < layer.width; tx++)
|
|
|
|
{
|
|
|
|
var tile = layer.data[ty][tx];
|
|
|
|
if (tile)
|
|
|
|
{
|
|
|
|
if (tile.index >= start && tile.index <= stop)
|
|
|
|
{
|
|
|
|
SetTileCollision(tile, collides);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
if (recalculateFaces) { CalculateFacesWithin(0, 0, layer.width, layer.height, layer); }
|
2017-11-21 02:06:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SetCollisionBetween;
|