Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// findClassInThis.js
(function () {
  /*global adventurejs A*/
  "use strict";
  var p = adventurejs.Tangible.prototype;
  /**
   * Find instances of specified class within this asset, for example
   * find all coins within a purse. Returns an array of asset IDs.
   * @memberOf adventurejs.Tangible
   * @method adventurejs.Tangible#findClassInThis
   * @param {String} instanceClass
   * @returns {Array}
   */
  p.findClassInThis = function Tangible_findClassInThis(
    instanceClass,
    params = {}
  ) {
    if ("string" !== typeof instanceClass) return [];

    // did we receive a valid class name?
    if (!adventurejs[instanceClass]) {
      // if not, since class names are propercased,
      // make an effort to propercase
      instanceClass = A.propercase(instanceClass);
    }
    if (!adventurejs[instanceClass]) return [];

    var contents = this.getAllNestedContents();
    var foundInstances = [];
    for (var i = 0; i < contents.length; i++) {
      var asset_id = contents[i];
      if (params.exclude?.includes(asset_id)) continue;
      var asset = this.game.getAsset(asset_id);
      if (asset && asset instanceof adventurejs[instanceClass]) {
        foundInstances.push(asset_id);
      }
    }
    return foundInstances;
  };
})();