Skip to content

Commit dfab17f

Browse files
committed
Tilemap helper methods for accessing objects from object layers
1 parent 7acd9b0 commit dfab17f

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

src/gameobjects/tilemap/Tilemap.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,37 @@ var Tilemap = new Class({
563563
return this;
564564
},
565565

566+
/**
567+
* For each object in the given object layer, run the given filter callback function. Any
568+
* objects that pass the filter test (i.e. where the callback returns true) will returned as a
569+
* new array. Similar to Array.prototype.Filter in vanilla JS.
570+
*
571+
* @param {ObjectLayer|string} [objectLayer] - The name of an object layer (from Tiled) or an
572+
* ObjectLayer instance.
573+
* @param {function} callback - The callback. Each object in the given area will be passed to
574+
* this callback as the first and only parameter.
575+
* @param {object} [context] - The context under which the callback should be run.
576+
* @return {object[]|null} An array of object that match the search, or null if the objectLayer
577+
* given was invalid.
578+
*/
579+
filterObjects: function (objectLayer, callback, context)
580+
{
581+
if (typeof objectLayer === 'string')
582+
{
583+
var name = objectLayer;
584+
585+
objectLayer = this.getObjectLayer(objectLayer);
586+
587+
if (!objectLayer)
588+
{
589+
console.warn('No object layer found with the name: ' + name);
590+
return null;
591+
}
592+
}
593+
594+
return objectLayer.objects.filter(callback, context);
595+
},
596+
566597
/**
567598
* See component documentation. If no layer specified, the map's current layer is used.
568599
*
@@ -587,6 +618,36 @@ var Tilemap = new Class({
587618
return TilemapComponents.FindByIndex(findIndex, skip, reverse, layer);
588619
},
589620

621+
/**
622+
* Find the first object in the given object layer that satisfies the provided testing function.
623+
* I.e. finds the first object for which `callback` returns true. Similar to
624+
* Array.prototype.find in vanilla JS.
625+
*
626+
* @param {ObjectLayer|string} [objectLayer] - The name of an object layer (from Tiled) or an
627+
* ObjectLayer instance.
628+
* @param {function} callback - The callback. Each object in the given area will be passed to
629+
* this callback as the first and only parameter.
630+
* @param {object} [context] - The context under which the callback should be run.
631+
* @return {object|null} An object that matches the search, or null if no object found
632+
*/
633+
findObject: function (objectLayer, callback, context)
634+
{
635+
if (typeof objectLayer === 'string')
636+
{
637+
var name = objectLayer;
638+
639+
objectLayer = this.getObjectLayer(objectLayer);
640+
641+
if (!objectLayer)
642+
{
643+
console.warn('No object layer found with the name: ' + name);
644+
return null;
645+
}
646+
}
647+
648+
return objectLayer.objects.find(callback, context) || null;
649+
},
650+
590651
/**
591652
* See component documentation. If no layer specified, the map's current layer is used.
592653
*

0 commit comments

Comments
 (0)