Add filtering options to forEachTile, GetTilesWithin, GetTilesWithinWorldXY

This commit is contained in:
Michael Hadley 2017-11-25 18:03:21 -06:00
parent 998ae1bd40
commit c31147df19
13 changed files with 47 additions and 33 deletions

View file

@ -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)

View file

@ -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++)
{

View file

@ -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;

View file

@ -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;

View file

@ -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);
};

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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)

View file

@ -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)

View file

@ -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);

View file

@ -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])

View file

@ -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)

View file

@ -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)