Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// canBePut.js
(function () {
  /* global AdventureJS A */
  var p = AdventureJS.Assets.Tangible.prototype;
  /**
   * Check whether this asset can be placed within
   * the specified aspect of the specified asset.
   * This takes into account the
   * <code>with_assets</code> and
   * <code>with_classes</code> properties.
   * @memberOf AdventureJS.Assets.Tangible
   * @method AdventureJS.Assets.Tangible#canBePut
   * @param {String} aspect
   * @param {Object} asset
   * @returns {Boolean}
   */
  p.canBePut = function Tangible_canBePut(aspect, asset) {
    if (!asset.hasAspectAt(aspect)) return false;
    if (!asset.getAspectAt(aspect).player_can_add_assets_to_contents)
      return false;

    var can = true;

    var with_classes = asset.aspects[aspect].with_classes;
    if (with_classes.length > 0) {
      can = false;
      for (var i = 0; i < with_classes.length; i++) {
        var Class = with_classes[i];
        // @TODO case check / lookup
        let c = AdventureJS[Class] || AdventureJS.Assets[Class];
        if (c && this instanceof c) {
          can = true;
          break;
        }
      }
    }

    var with_assets = asset.aspects[aspect].with_assets;
    if (with_assets.length > 0) {
      can = false;
      for (var i = 0; i < with_assets.length; i++) {
        if (this.id === with_assets[i]) {
          can = true;
          break;
        }
      }
    }

    return can;
  };
})();