From c31147df1924aacbe04320ffe6d2114203624a9f Mon Sep 17 00:00:00 2001 From: Michael Hadley Date: Sat, 25 Nov 2017 18:03:21 -0600 Subject: [PATCH] Add filtering options to forEachTile, GetTilesWithin, GetTilesWithinWorldXY --- v3/src/gameobjects/tilemap/Tilemap.js | 12 +++++------ .../components/CalculateFacesWithin.js | 2 +- v3/src/gameobjects/tilemap/components/Copy.js | 2 +- v3/src/gameobjects/tilemap/components/Fill.js | 2 +- .../tilemap/components/ForEachTile.js | 4 ++-- .../tilemap/components/GetTilesWithin.js | 20 +++++++++++++++++-- .../components/GetTilesWithinWorldXY.js | 6 ++---- .../tilemap/components/Randomize.js | 2 +- .../tilemap/components/ReplaceByIndex.js | 2 +- .../gameobjects/tilemap/components/Shuffle.js | 2 +- .../tilemap/components/SwapByIndex.js | 2 +- .../dynamiclayer/DynamicTilemapLayer.js | 12 +++++------ .../tilemap/staticlayer/StaticTilemapLayer.js | 12 +++++------ 13 files changed, 47 insertions(+), 33 deletions(-) diff --git a/v3/src/gameobjects/tilemap/Tilemap.js b/v3/src/gameobjects/tilemap/Tilemap.js index 5cb9dc456..7f976ae96 100644 --- a/v3/src/gameobjects/tilemap/Tilemap.js +++ b/v3/src/gameobjects/tilemap/Tilemap.js @@ -227,12 +227,12 @@ var Tilemap = new Class({ return TilemapComponents.FindByIndex(findIndex, skip, reverse, layer); }, - forEachTile: function (callback, context, tileX, tileY, width, height, layer) + forEachTile: function (callback, context, tileX, tileY, width, height, filteringOptions, layer) { layer = this.getLayer(layer); if (layer !== null) { - TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, layer); + TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, filteringOptions, layer); } return this; }, @@ -303,18 +303,18 @@ var Tilemap = new Class({ return TilemapComponents.GetTileAtWorldXY(worldX, worldY, nonNull, camera, layer); }, - getTilesWithin: function (tileX, tileY, width, height, layer) + getTilesWithin: function (tileX, tileY, width, height, filteringOptions, layer) { layer = this.getLayer(layer); if (layer === null) { return null; } - return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, layer); + return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, filteringOptions, layer); }, - getTilesWithinWorldXY: function (worldX, worldY, width, height, camera, layer) + getTilesWithinWorldXY: function (worldX, worldY, width, height, filteringOptions, camera, layer) { layer = this.getLayer(layer); if (layer === null) { return null; } - return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, camera, layer); + return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, filteringOptions, camera, layer); }, getTilesetIndex: function (name) diff --git a/v3/src/gameobjects/tilemap/components/CalculateFacesWithin.js b/v3/src/gameobjects/tilemap/components/CalculateFacesWithin.js index 9ec5fcd8a..5b8bd4f86 100644 --- a/v3/src/gameobjects/tilemap/components/CalculateFacesWithin.js +++ b/v3/src/gameobjects/tilemap/components/CalculateFacesWithin.js @@ -8,7 +8,7 @@ var CalculateFacesWithin = function (tileX, tileY, width, height, layer) var left = null; var right = null; - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer); for (var i = 0; i < tiles.length; i++) { diff --git a/v3/src/gameobjects/tilemap/components/Copy.js b/v3/src/gameobjects/tilemap/components/Copy.js index 931e9872d..213364b06 100644 --- a/v3/src/gameobjects/tilemap/components/Copy.js +++ b/v3/src/gameobjects/tilemap/components/Copy.js @@ -6,7 +6,7 @@ var Copy = function (srcTileX, srcTileY, width, height, destTileX, destTileY, la if (srcTileX === undefined || srcTileX < 0) { srcTileX = 0; } if (srcTileY === undefined || srcTileY < 0) { srcTileY = 0; } - var srcTiles = GetTilesWithin(srcTileX, srcTileY, width, height, layer); + var srcTiles = GetTilesWithin(srcTileX, srcTileY, width, height, null, layer); var offsetX = destTileX - srcTileX; var offsetY = destTileY - srcTileY; diff --git a/v3/src/gameobjects/tilemap/components/Fill.js b/v3/src/gameobjects/tilemap/components/Fill.js index 027084a98..451fdbb95 100644 --- a/v3/src/gameobjects/tilemap/components/Fill.js +++ b/v3/src/gameobjects/tilemap/components/Fill.js @@ -3,7 +3,7 @@ var GetTilesWithin = require('./GetTilesWithin'); // Fills indices, not other properties. Does not modify collisions. Matches v2 functionality. var Fill = function (index, tileX, tileY, width, height, layer) { - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer); for (var i = 0; i < tiles.length; i++) { tiles[i].index = index; diff --git a/v3/src/gameobjects/tilemap/components/ForEachTile.js b/v3/src/gameobjects/tilemap/components/ForEachTile.js index 09089af49..e5c1b7f00 100644 --- a/v3/src/gameobjects/tilemap/components/ForEachTile.js +++ b/v3/src/gameobjects/tilemap/components/ForEachTile.js @@ -1,8 +1,8 @@ var GetTilesWithin = require('./GetTilesWithin'); -var ForEachTile = function (callback, context, tileX, tileY, width, height, layer) +var ForEachTile = function (callback, context, tileX, tileY, width, height, filteringOptions, layer) { - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, filteringOptions, layer); tiles.forEach(callback, context); }; diff --git a/v3/src/gameobjects/tilemap/components/GetTilesWithin.js b/v3/src/gameobjects/tilemap/components/GetTilesWithin.js index 02aa40943..7f4266c68 100644 --- a/v3/src/gameobjects/tilemap/components/GetTilesWithin.js +++ b/v3/src/gameobjects/tilemap/components/GetTilesWithin.js @@ -1,13 +1,26 @@ -// TODO: add options for filtering by empty, collides, interestingFace + + +var GetFastValue = require('../../../utils/object/GetFastValue'); + // Get tiles within the rectangular area specified. Note: this clips the x, y, w & h to the map's // boundries. -var GetTilesWithin = function (tileX, tileY, width, height, layer) +// Options: +// { +// isNotEmpty: false, +// isColliding: false, +// hasInterestingFace: false +// } +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) { @@ -39,6 +52,9 @@ var GetTilesWithin = function (tileX, tileY, width, height, layer) 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); } } diff --git a/v3/src/gameobjects/tilemap/components/GetTilesWithinWorldXY.js b/v3/src/gameobjects/tilemap/components/GetTilesWithinWorldXY.js index e4e9af7c6..614611749 100644 --- a/v3/src/gameobjects/tilemap/components/GetTilesWithinWorldXY.js +++ b/v3/src/gameobjects/tilemap/components/GetTilesWithinWorldXY.js @@ -2,9 +2,7 @@ var GetTilesWithin = require('./GetTilesWithin'); var WorldToTileX = require('./WorldToTileX'); var WorldToTileY = require('./WorldToTileY'); -// TODO: add options for filtering by empty, collides, interestingFace -// { isNotEmpty, isColliding, hasInterestingFace } -var GetTilesWithinWorldXY = function (worldX, worldY, width, height, camera, layer) +var GetTilesWithinWorldXY = function (worldX, worldY, width, height, filteringOptions, camera, layer) { // Top left corner of the rect, rounded down to include partial tiles var xStart = WorldToTileX(worldX, true, camera, layer); @@ -14,7 +12,7 @@ var GetTilesWithinWorldXY = function (worldX, worldY, width, height, camera, lay var xEnd = Math.ceil(WorldToTileX(worldX + width, false, camera, layer)); var yEnd = Math.ceil(WorldToTileY(worldY + height, false, camera, layer)); - return GetTilesWithin(xStart, yStart, xEnd - xStart, yEnd - yStart, layer); + return GetTilesWithin(xStart, yStart, xEnd - xStart, yEnd - yStart, filteringOptions, layer); }; module.exports = GetTilesWithinWorldXY; diff --git a/v3/src/gameobjects/tilemap/components/Randomize.js b/v3/src/gameobjects/tilemap/components/Randomize.js index f3c30632d..3cf2ba131 100644 --- a/v3/src/gameobjects/tilemap/components/Randomize.js +++ b/v3/src/gameobjects/tilemap/components/Randomize.js @@ -5,7 +5,7 @@ var GetRandomElement = require('../../../utils/array/GetRandomElement'); var Randomize = function (tileX, tileY, width, height, indices, layer) { var i; - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer); // If no indicies are given, then find all the unique indices within the specified region if (indices === undefined) diff --git a/v3/src/gameobjects/tilemap/components/ReplaceByIndex.js b/v3/src/gameobjects/tilemap/components/ReplaceByIndex.js index 06bef3082..58a502c47 100644 --- a/v3/src/gameobjects/tilemap/components/ReplaceByIndex.js +++ b/v3/src/gameobjects/tilemap/components/ReplaceByIndex.js @@ -3,7 +3,7 @@ var GetTilesWithin = require('./GetTilesWithin'); // Replaces indices, not other properties. Does not modify collisions. Matches v2 functionality. var ReplaceByIndex = function (findIndex, newIndex, tileX, tileY, width, height, layer) { - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer); for (var i = 0; i < tiles.length; i++) { if (tiles[i] && tiles[i].index === findIndex) diff --git a/v3/src/gameobjects/tilemap/components/Shuffle.js b/v3/src/gameobjects/tilemap/components/Shuffle.js index 087121a8b..6d2dc42a7 100644 --- a/v3/src/gameobjects/tilemap/components/Shuffle.js +++ b/v3/src/gameobjects/tilemap/components/Shuffle.js @@ -4,7 +4,7 @@ var ShuffleArray = require('../../../utils/array/Shuffle'); // Shuffles indices, not other properties. Does not modify collisions. Matches v2 functionality. var Shuffle = function (tileX, tileY, width, height, layer) { - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer); var indices = tiles.map(function (tile) { return tile.index; }); ShuffleArray(indices); diff --git a/v3/src/gameobjects/tilemap/components/SwapByIndex.js b/v3/src/gameobjects/tilemap/components/SwapByIndex.js index b52d3771a..8c9d02589 100644 --- a/v3/src/gameobjects/tilemap/components/SwapByIndex.js +++ b/v3/src/gameobjects/tilemap/components/SwapByIndex.js @@ -3,7 +3,7 @@ var GetTilesWithin = require('./GetTilesWithin'); // Swaps indices, not other properties. Does not modify collisions. Matches v2 functionality. var SwapByIndex = function (indexA, indexB, tileX, tileY, width, height, layer) { - var tiles = GetTilesWithin(tileX, tileY, width, height, layer); + var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer); for (var i = 0; i < tiles.length; i++) { if (tiles[i]) diff --git a/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js b/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js index 39155de3f..31faef9e0 100644 --- a/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js +++ b/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js @@ -87,9 +87,9 @@ var DynamicTilemapLayer = new Class({ return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer); }, - forEachTile: function (callback, context, tileX, tileY, width, height) + forEachTile: function (callback, context, tileX, tileY, width, height, filteringOptions) { - TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer); + TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, filteringOptions, this.layer); return this; }, @@ -103,14 +103,14 @@ var DynamicTilemapLayer = new Class({ return TilemapComponents.GetTileAtWorldXY(worldX, worldY, nonNull, camera, this.layer); }, - getTilesWithin: function (tileX, tileY, width, height) + getTilesWithin: function (tileX, tileY, width, height, filteringOptions) { - return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, this.layer); + return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, filteringOptions, this.layer); }, - getTilesWithinWorldXY: function (worldX, worldY, width, height, camera) + getTilesWithinWorldXY: function (worldX, worldY, width, height, filteringOptions, camera) { - return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, camera, this.layer); + return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, filteringOptions, camera, this.layer); }, hasTileAt: function (tileX, tileY) diff --git a/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js b/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js index c90dd40fd..0e0567886 100644 --- a/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js +++ b/v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js @@ -228,9 +228,9 @@ var StaticTilemapLayer = new Class({ return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer); }, - forEachTile: function (callback, context, tileX, tileY, width, height) + forEachTile: function (callback, context, tileX, tileY, width, height, filteringOptions) { - TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer); + TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, filteringOptions, this.layer); return this; }, @@ -244,14 +244,14 @@ var StaticTilemapLayer = new Class({ return TilemapComponents.GetTileAtWorldXY(worldX, worldY, nonNull, camera, this.layer); }, - getTilesWithin: function (tileX, tileY, width, height) + getTilesWithin: function (tileX, tileY, width, height, filteringOptions) { - return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, this.layer); + return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, filteringOptions, this.layer); }, - getTilesWithinWorldXY: function (worldX, worldY, width, height, camera) + getTilesWithinWorldXY: function (worldX, worldY, width, height, filteringOptions, camera) { - return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, camera, this.layer); + return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, filteringOptions, camera, this.layer); }, hasTileAt: function (tileX, tileY)