Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// callAction.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.
   * If either form
   * is found, pass it to getStringOrArrayOrFunction and
   * return the results. If the result returns false or null
   * it will terminate the calling verb's default behavior.
   * @memberOf adventurejs.Asset
   * @method adventurejs.Asset#callAction
   * @param {string} hook
   * @param {string} asset_name We use asset.name here instead of asset.id
   * in support of authors, because we're never asking them to deal in IDs,
   * only names. Hooks will only be defined by authors, so we 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, because Ivan.
   * @param {object} params Arbitrary parameter object.
   * @return {Boolean}
   */
  p.callAction = function Asset_callActions(
    hook_name,
    passed_asset_name,
    params = {}
  ) {
    params = params !== null && params !== undefined ? params : {};
    this.game.log(
      "log",
      "high",
      `${this.id}.callAction > ${hook_name}`,
      "Behavior"
    );
    if (!hook_name || !this[hook_name]) return;
    let input = this.game.getInput();
    let player, asset2, asset3;

    if (passed_asset_name) {
      // if an asset was passed, disregard assets retrieved from input
      asset2 = passed_asset_name.class
        ? passed_asset_name
        : this.game.getAsset(passed_asset_name);
    } else {
      asset2 = input.getAsset(2);
      asset3 = input.getAsset(3);
      if (!asset2) asset2 = this.game.getPlayer();
    }

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

    // see if hook is an object containing other properties,
    // where we expect to find asset names
    if ("function" === typeof hook[asset2.name]) {
      return hook[asset2.name].call(this, params);
    }

    if (
      hook[asset2.name] &&
      asset3 &&
      "function" === typeof hook[asset2.name][asset3.name]
    ) {
      return hook[asset2.name][asset3.name].call(this, params);
    }
    return;
  };
})();