Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// hasAction.js
(function () {
  /*global adventurejs A*/
  "use strict";
  var p = adventurejs.Asset.prototype;
  /**
   * Check if this asset has a general verb action or reaction
   * for the specified event,
   * or if the asset has an action for the specific asset.
   * @memberOf adventurejs.Asset
   * @method adventurejs.Asset#hasAction
   * @param {string} hook_name
   * @param {string} asset1_name We use asset.name here instead of asset.id
   * to make life slightly easier for authors. Asset IDs are formed from
   * asset names, but generally we don't expect authors to be aware of IDs.
   * Hooks will only be defined by authors, so let them use asset.name
   * as their identifier. We do however make an effort to see if an id has been
   * passed instead of a name.
   * @param {string} asset2_name
   * @return {Boolean}
   */
  p.hasAction = function Asset_hasAction(hook_name, asset1_name, asset2_name) {
    if (!hook_name || !this[hook_name]) return;
    var asset1, asset2;
    if (asset1_name) {
      asset1 = asset1_name.class
        ? asset1_name
        : this.game.getAsset(asset1_name);
      asset1_name = asset1?.name || asset1_name;
    }
    if (asset2_name) {
      asset2 = asset2_name.class
        ? asset2_name
        : this.game.getAsset(asset2_name);
      asset2_name = asset2?.name || asset2_name;
    }

    // look for a generic hook
    let hook = this[hook_name];
    if ("function" === typeof hook) {
      return true;
    }

    // look for a specific hook
    if ("function" === typeof hook[asset1_name]) {
      return true;
    }

    if (
      hook[asset1.name] &&
      "function" === typeof hook[asset1.name][asset2.name]
    ) {
      return true;
    }

    return false;
  };
})();