Skip to content

Commit ad24a0b

Browse files
authored
Removed repetative code.
1 parent 00a38f7 commit ad24a0b

1 file changed

Lines changed: 18 additions & 39 deletions

File tree

src/tilemaps/components/CullTiles.js

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,66 +26,45 @@ var CullTiles = function (layer, camera, outputArray)
2626
if (outputArray === undefined) { outputArray = []; }
2727

2828
outputArray.length = 0;
29-
29+
3030
var y = 0;
3131
var x = 0;
3232
var tile = null;
33-
34-
var tilemapLayer = layer.tilemapLayer;
3533

36-
var tileW = Math.floor(layer.tileWidth * tilemapLayer.scaleX);
37-
var tileH = Math.floor(layer.tileHeight * tilemapLayer.scaleY);
34+
var tilemapLayer = layer.tilemapLayer;
3835

3936
var mapData = layer.data;
4037
var mapWidth = layer.width;
4138
var mapHeight = layer.height;
4239

40+
var tileW = Math.floor(layer.tileWidth * tilemapLayer.scaleX);
41+
var tileH = Math.floor(layer.tileHeight * tilemapLayer.scaleY);
42+
4343
// Camera world view bounds, snapped for tile size
4444

4545
var boundsLeft = SnapFloor(camera.worldView.x, tileW) - (tilemapLayer.cullPaddingX * tileW);
4646
var boundsRight = SnapCeil(camera.worldView.right, tileW) + (tilemapLayer.cullPaddingX * tileW);
4747
var boundsTop = SnapFloor(camera.worldView.y, tileH) - (tilemapLayer.cullPaddingY * tileH);
4848
var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + (tilemapLayer.cullPaddingY * tileH);
49+
50+
var skipCull = tilemapLayer.skipCull;
51+
var drawLeft = skipCull ? 0 : Math.max(0, boundsLeft / layer.tileWidth);
52+
var drawRight = skipCull ? mapWidth : Math.min(mapWidth, boundsRight / layer.tileWidth);
53+
var drawTop = skipCull ? 0 : Math.max(0, boundsTop / layer.tileHeight);
54+
var drawBottom = skipCull ? mapHeight : Math.min(mapHeight, boundsBottom / layer.tileHeight);
4955

50-
// If skipping cull, loop through every tile in the map.
51-
52-
if(tilemapLayer.skipCull)
56+
for (y = drawTop; y < drawBottom; y++)
5357
{
54-
for (y = 0; y < mapHeight; y++)
58+
for (x = drawLeft; x < drawRight; x++)
5559
{
56-
for (x = 0; x < mapWidth; x++)
57-
{
58-
tile = mapData[y][x];
59-
60-
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
61-
{
62-
continue;
63-
}
60+
tile = mapData[y][x];
6461

65-
outputArray.push(tile);
66-
}
67-
}
68-
}
69-
else
70-
{
71-
var drawLeft = Math.max(0, boundsLeft / layer.tileWidth);
72-
var drawRight = Math.min(mapWidth, boundsRight / layer.tileWidth);
73-
var drawTop = Math.max(0, boundsTop / layer.tileHeight);
74-
var drawBottom = Math.min(mapHeight, boundsBottom / layer.tileHeight);
75-
76-
for (y = drawTop; y < drawBottom; y++)
77-
{
78-
for (x = drawLeft; x < drawRight; x++)
62+
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
7963
{
80-
tile = mapData[y][x];
81-
82-
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
83-
{
84-
continue;
85-
}
86-
87-
outputArray.push(tile);
64+
continue;
8865
}
66+
67+
outputArray.push(tile);
8968
}
9069
}
9170

0 commit comments

Comments
 (0)