forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetCollisionBetween.js
More file actions
69 lines (61 loc) · 2.49 KB
/
Copy pathSetCollisionBetween.js
File metadata and controls
69 lines (61 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var SetTileCollision = require('./SetTileCollision');
var CalculateFacesWithin = require('./CalculateFacesWithin');
var SetLayerCollisionIndex = require('./SetLayerCollisionIndex');
/**
* 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).
*
* @function Phaser.Tilemaps.Components.SetCollisionBetween
* @private
* @since 3.0.0
*
* @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.
* @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.
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {boolean} [updateLayer=true] - If true, updates the current tiles on the layer. Set to
* false if no tiles have been placed for significant performance boost.
*/
var SetCollisionBetween = function (start, stop, collides, recalculateFaces, layer, updateLayer)
{
if (collides === undefined) { collides = true; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
if (updateLayer === undefined) { updateLayer = true; }
if (start > stop) { return; }
// Update the array of colliding indexes
for (var index = start; index <= stop; index++)
{
SetLayerCollisionIndex(index, collides, layer);
}
// Update the tiles
if (updateLayer)
{
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);
}
}
}
}
}
if (recalculateFaces)
{
CalculateFacesWithin(0, 0, layer.width, layer.height, layer);
}
};
module.exports = SetCollisionBetween;