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}
|
|
|
|
*/
|
|
|
|
|
2018-01-23 15:05:37 +00:00
|
|
|
var SetTileCollision = require('./SetTileCollision');
|
|
|
|
var CalculateFacesWithin = require('./CalculateFacesWithin');
|
2018-02-07 17:10:01 +00:00
|
|
|
var HasValue = require('../../utils/object/HasValue');
|
2018-01-23 15:05:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets collision on the tiles within a layer by checking tile properties. If a tile has a property
|
|
|
|
* that matches the given properties object, its collision flag will be set. The `collides`
|
|
|
|
* parameter controls if collision will be enabled (true) or disabled (false). Passing in
|
|
|
|
* `{ collides: true }` would update the collision flag on any tiles with a "collides" property that
|
|
|
|
* has a value of true. Any tile that doesn't have "collides" set to true will be ignored. You can
|
|
|
|
* also use an array of values, e.g. `{ types: ["stone", "lava", "sand" ] }`. If a tile has a
|
|
|
|
* "types" property that matches any of those values, its collision flag will be updated.
|
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.SetCollisionByProperty
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-01-23 15:05:37 +00:00
|
|
|
* @param {object} properties - An object with tile properties and corresponding values that should
|
|
|
|
* be checked.
|
|
|
|
* @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.
|
2018-01-23 15:05:37 +00:00
|
|
|
*/
|
|
|
|
var SetCollisionByProperty = function (properties, collides, recalculateFaces, layer)
|
|
|
|
{
|
|
|
|
if (collides === undefined) { collides = true; }
|
|
|
|
if (recalculateFaces === undefined) { recalculateFaces = true; }
|
|
|
|
|
|
|
|
for (var ty = 0; ty < layer.height; ty++)
|
|
|
|
{
|
|
|
|
for (var tx = 0; tx < layer.width; tx++)
|
|
|
|
{
|
|
|
|
var tile = layer.data[ty][tx];
|
|
|
|
|
|
|
|
if (!tile) { continue; }
|
|
|
|
|
|
|
|
for (var property in properties)
|
|
|
|
{
|
|
|
|
if (!HasValue(tile.properties, property)) { continue; }
|
|
|
|
|
|
|
|
var values = properties[property];
|
|
|
|
if (!Array.isArray(values))
|
|
|
|
{
|
|
|
|
values = [ values ];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < values.length; i++)
|
|
|
|
{
|
|
|
|
if (tile.properties[property] === values[i])
|
|
|
|
{
|
|
|
|
SetTileCollision(tile, collides);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recalculateFaces) { CalculateFacesWithin(0, 0, layer.width, layer.height, layer); }
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SetCollisionByProperty;
|