Skip to content

Commit 195ea19

Browse files
committed
Extra tilemap method findTile (matches Array.prototype.find)
1 parent a8fdcbc commit 195ea19

5 files changed

Lines changed: 64 additions & 0 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ var Tilemap = new Class({
589589
return TilemapComponents.FindByIndex(findIndex, skip, reverse, layer);
590590
},
591591

592+
/**
593+
* See component documentation. If no layer specified, the map's current layer is used.
594+
*
595+
* @return {Tile|null} Returns a Tiles, or null if the layer given was invalid.
596+
*/
597+
findTile: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
598+
{
599+
layer = this.getLayer(layer);
600+
if (layer === null) { return null; }
601+
return TilemapComponents.FindTile(callback, context, tileX, tileY, width, height, filteringOptions, layer);
602+
},
603+
592604
/**
593605
* See component documentation. If no layer specified, the map's current layer is used.
594606
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var GetTilesWithin = require('./GetTilesWithin');
2+
3+
/**
4+
* Find the first tile in the given rectangular area (in tile coordinates) of the layer that
5+
* satisfies the provided testing function. I.e. finds the first tile for which `callback` returns
6+
* true. Similar to Array.prototype.find in vanilla JS.
7+
*
8+
* @param {function} callback - The callback. Each tile in the given area will be passed to this
9+
* callback as the first and only parameter.
10+
* @param {object} [context] - The context under which the callback should be run.
11+
* @param {integer} [tileX=0] - [description]
12+
* @param {integer} [tileY=0] - [description]
13+
* @param {integer} [width=max width based on tileX] - [description]
14+
* @param {integer} [height=max height based on tileY] - [description]
15+
* @param {object} [filteringOptions] - Optional filters to apply when getting the tiles.
16+
* @param {boolean} [filteringOptions.isNotEmpty=false] - If true, only return tiles that don't have
17+
* -1 for an index.
18+
* @param {boolean} [filteringOptions.isColliding=false] - If true, only return tiles that collide
19+
* on at least one side.
20+
* @param {boolean} [filteringOptions.hasInterestingFace=false] - If true, only return tiles that
21+
* have at least one interesting face.
22+
* @param {LayerData} layer - [description]
23+
* @return {Tile|null} A Tile that matches the search, or null if no Tile found
24+
*/
25+
var FindTile = function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
26+
{
27+
var tiles = GetTilesWithin(tileX, tileY, width, height, filteringOptions, layer);
28+
return tiles.find(callback, context) || null;
29+
};
30+
31+
module.exports = FindTile;

v3/src/gameobjects/tilemap/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
CullTiles: require('./CullTiles'),
66
Fill: require('./Fill'),
77
FindByIndex: require('./FindByIndex'),
8+
FindTile: require('./FindTile'),
89
FilterTiles: require('./FilterTiles'),
910
ForEachTile: require('./ForEachTile'),
1011
GetTileAt: require('./GetTileAt'),

v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ var DynamicTilemapLayer = new Class({
181181
return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer);
182182
},
183183

184+
/**
185+
* See component documentation.
186+
*
187+
* @return {Tile|null}
188+
*/
189+
findTile: function (callback, context, tileX, tileY, width, height, filteringOptions)
190+
{
191+
return TilemapComponents.FindTile(callback, context, tileX, tileY, width, height, filteringOptions, this.layer);
192+
},
193+
184194
/**
185195
* See component documentation.
186196
*

v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ var StaticTilemapLayer = new Class({
310310
return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer);
311311
},
312312

313+
/**
314+
* See component documentation.
315+
*
316+
* @return {Tile|null}
317+
*/
318+
findTile: function (callback, context, tileX, tileY, width, height, filteringOptions)
319+
{
320+
return TilemapComponents.FindTile(callback, context, tileX, tileY, width, height, filteringOptions, this.layer);
321+
},
322+
313323
/**
314324
* See component documentation.
315325
*

0 commit comments

Comments
 (0)