Pre-release
AdventureJS Docs Downloads Bundler Devlog Score: 0 Moves: 0
// getAllContents.js
(function () {
  /* global AdventureJS A */
  var p = AdventureJS.Assets.Entity.prototype;
  /**
   * Returns an array of all assets contained in this asset.
   * This is a shallow search.
   * @memberOf AdventureJS.Assets.Entity
   * @method AdventureJS.Assets.Entity#getAllContents
   * @param {Boolean} include_attached
   * @return {Array}
   */
  p.getAllContents = function Entity_getAllContents(include_attached = false) {
    var contents = [];

    for (var aspect in this.aspects) {
      if (aspect === "attached" && !include_attached) {
        // the old version skipped all contents in the attached aspect
        // continue;
        //
        // BUT
        //
        // we don't want to include the attached thing,
        // but we may want to include its children
        const attachedcontents = this.aspects[aspect].contents;
        for (let i = 0; i < attachedcontents.length; i++) {
          const child = this.game.getAsset(attachedcontents[i]);
          const nestedContents = child.getAllNestedContents(include_attached);
          contents = contents.concat(nestedContents);
        }
        // this.aspects.attached.contents
      } else if (Array.isArray(this.aspects[aspect].contents)) {
        contents = contents.concat(this.aspects[aspect].contents);
      }
    }

    return contents;
  };
})();