forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCullTiles.js
More file actions
85 lines (69 loc) · 2.99 KB
/
Copy pathCullTiles.js
File metadata and controls
85 lines (69 loc) · 2.99 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
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var SnapFloor = require('../../math/snap/SnapFloor');
var SnapCeil = require('../../math/snap/SnapCeil');
/**
* Returns the tiles in the given layer that are within the camera's viewport. This is used
* internally.
*
* @function Phaser.Tilemaps.Components.CullTiles
* @private
* @since 3.0.0
*
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to run the cull check against.
* @param {array} [outputArray] - [description]
*
* @return {Phaser.Tilemaps.Tile[]} An array of Tile objects.
*/
var CullTiles = function (layer, camera, outputArray)
{
if (outputArray === undefined) { outputArray = []; }
outputArray.length = 0;
var tilemap = layer.tilemapLayer.tilemap;
var tilemapLayer = layer.tilemapLayer;
var mapData = layer.data;
var mapWidth = layer.width;
var mapHeight = layer.height;
// We need to use the tile sizes defined for the map as a whole, not the layer,
// in order to calculate the bounds correctly. As different sized tiles may be
// placed on the grid and we cannot trust layer.baseTileWidth to give us the true size.
var tileW = Math.floor(tilemap.tileWidth * tilemapLayer.scaleX);
var tileH = Math.floor(tilemap.tileHeight * tilemapLayer.scaleY);
var drawLeft = 0;
var drawRight = mapWidth;
var drawTop = 0;
var drawBottom = mapHeight;
if (!tilemapLayer.skipCull && tilemapLayer.scrollFactorX === 1 && tilemapLayer.scrollFactorY === 1)
{
// Camera world view bounds, snapped for scaled tile size
// Cull Padding values are given in tiles, not pixels
var boundsLeft = SnapFloor(camera.worldView.x - tilemapLayer.x, tileW, 0, true) - tilemapLayer.cullPaddingX;
var boundsRight = SnapCeil(camera.worldView.right - tilemapLayer.x, tileW, 0, true) + tilemapLayer.cullPaddingX;
var boundsTop = SnapFloor(camera.worldView.y - tilemapLayer.y, tileH, 0, true) - tilemapLayer.cullPaddingY;
var boundsBottom = SnapCeil(camera.worldView.bottom - tilemapLayer.y, tileH, 0, true) + tilemapLayer.cullPaddingY;
drawLeft = Math.max(0, boundsLeft);
drawRight = Math.min(mapWidth, boundsRight);
drawTop = Math.max(0, boundsTop);
drawBottom = Math.min(mapHeight, boundsBottom);
}
for (var y = drawTop; y < drawBottom; y++)
{
for (var x = drawLeft; x < drawRight; x++)
{
var tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
}
}
tilemapLayer.tilesDrawn = outputArray.length;
tilemapLayer.tilesTotal = mapWidth * mapHeight;
return outputArray;
};
module.exports = CullTiles;