Removed repetative code.

This commit is contained in:
tarsupin 2018-07-14 20:09:36 -05:00 committed by GitHub
parent 00a38f744a
commit ad24a0b8ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -26,66 +26,45 @@ var CullTiles = function (layer, camera, outputArray)
if (outputArray === undefined) { outputArray = []; } if (outputArray === undefined) { outputArray = []; }
outputArray.length = 0; outputArray.length = 0;
var y = 0; var y = 0;
var x = 0; var x = 0;
var tile = null; var tile = null;
var tilemapLayer = layer.tilemapLayer;
var tileW = Math.floor(layer.tileWidth * tilemapLayer.scaleX); var tilemapLayer = layer.tilemapLayer;
var tileH = Math.floor(layer.tileHeight * tilemapLayer.scaleY);
var mapData = layer.data; var mapData = layer.data;
var mapWidth = layer.width; var mapWidth = layer.width;
var mapHeight = layer.height; var mapHeight = layer.height;
var tileW = Math.floor(layer.tileWidth * tilemapLayer.scaleX);
var tileH = Math.floor(layer.tileHeight * tilemapLayer.scaleY);
// Camera world view bounds, snapped for tile size // Camera world view bounds, snapped for tile size
var boundsLeft = SnapFloor(camera.worldView.x, tileW) - (tilemapLayer.cullPaddingX * tileW); var boundsLeft = SnapFloor(camera.worldView.x, tileW) - (tilemapLayer.cullPaddingX * tileW);
var boundsRight = SnapCeil(camera.worldView.right, tileW) + (tilemapLayer.cullPaddingX * tileW); var boundsRight = SnapCeil(camera.worldView.right, tileW) + (tilemapLayer.cullPaddingX * tileW);
var boundsTop = SnapFloor(camera.worldView.y, tileH) - (tilemapLayer.cullPaddingY * tileH); var boundsTop = SnapFloor(camera.worldView.y, tileH) - (tilemapLayer.cullPaddingY * tileH);
var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + (tilemapLayer.cullPaddingY * tileH); var boundsBottom = SnapCeil(camera.worldView.bottom, tileH) + (tilemapLayer.cullPaddingY * tileH);
var skipCull = tilemapLayer.skipCull;
var drawLeft = skipCull ? 0 : Math.max(0, boundsLeft / layer.tileWidth);
var drawRight = skipCull ? mapWidth : Math.min(mapWidth, boundsRight / layer.tileWidth);
var drawTop = skipCull ? 0 : Math.max(0, boundsTop / layer.tileHeight);
var drawBottom = skipCull ? mapHeight : Math.min(mapHeight, boundsBottom / layer.tileHeight);
// If skipping cull, loop through every tile in the map. for (y = drawTop; y < drawBottom; y++)
if(tilemapLayer.skipCull)
{ {
for (y = 0; y < mapHeight; y++) for (x = drawLeft; x < drawRight; x++)
{ {
for (x = 0; x < mapWidth; x++) tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{ {
tile = mapData[y][x]; continue;
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0)
{
continue;
}
outputArray.push(tile);
} }
}
}
else
{
var drawLeft = Math.max(0, boundsLeft / layer.tileWidth);
var drawRight = Math.min(mapWidth, boundsRight / layer.tileWidth);
var drawTop = Math.max(0, boundsTop / layer.tileHeight);
var drawBottom = Math.min(mapHeight, boundsBottom / layer.tileHeight);
for (y = drawTop; y < drawBottom; y++)
{
for (x = drawLeft; x < drawRight; x++)
{
tile = mapData[y][x];
if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0) outputArray.push(tile);
{
continue;
}
outputArray.push(tile);
}
} }
} }