forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetTilesWithin.js
More file actions
73 lines (65 loc) · 2.48 KB
/
Copy pathGetTilesWithin.js
File metadata and controls
73 lines (65 loc) · 2.48 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
70
71
72
73
var GetFastValue = require('../../../utils/object/GetFastValue');
/**
* Gets the tiles in the given rectangular area (in tile coordinates) of the layer.
*
* @param {integer} [tileX=0] - [description]
* @param {integer} [tileY=0] - [description]
* @param {integer} [width=max width based on tileX] - [description]
* @param {integer} [height=max height based on tileY] - [description]
* @param {object} [filteringOptions] - Optional filters to apply when getting the tiles.
* @param {boolean} [filteringOptions.isNotEmpty=false] - If true, only return tiles that don't have
* -1 for an index.
* @param {boolean} [filteringOptions.isColliding=false] - If true, only return tiles that collide on
* at least one side.
* @param {boolean} [filteringOptions.hasInterestingFace=false] - If true, only return tiles that
* have at least one interesting face.
* @param {LayerData} layer - [description]
* @return {Tile[]} Array of Tile objects.
*/
var GetTilesWithin = function (tileX, tileY, width, height, filteringOptions, layer)
{
if (tileX === undefined) { tileX = 0; }
if (tileY === undefined) { tileY = 0; }
if (width === undefined) { width = layer.width; }
if (height === undefined) { height = layer.height; }
var isNotEmpty = GetFastValue(filteringOptions, 'isNotEmpty', false);
var isColliding = GetFastValue(filteringOptions, 'isColliding', false);
var hasInterestingFace = GetFastValue(filteringOptions, 'hasInterestingFace', false);
// Clip x, y to top left of map, while shrinking width/height to match.
if (tileX < 0)
{
width += tileX;
tileX = 0;
}
if (tileY < 0)
{
height += tileY;
tileY = 0;
}
// Clip width and height to bottom right of map.
if (tileX + width > layer.width)
{
width = Math.max(layer.width - tileX, 0);
}
if (tileY + height > layer.height)
{
height = Math.max(layer.height - tileY, 0);
}
var results = [];
for (var ty = tileY; ty < tileY + height; ty++)
{
for (var tx = tileX; tx < tileX + width; tx++)
{
var tile = layer.data[ty][tx];
if (tile !== null)
{
if (isNotEmpty && tile.index === -1) { continue; }
if (isColliding && !tile.collides) { continue; }
if (hasInterestingFace && !tile.hasInterestingFace) { continue; }
results.push(tile);
}
}
}
return results;
};
module.exports = GetTilesWithin;