Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// VerbSubscription.js
(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @ajspath adventurejs.Atom.VerbSubscription
   * @augments adventurejs.VerbSubscription
   * @class adventurejs.VerbSubscription
   * @ajsnavheading FrameworkReference
   * @param {String} game_name Name of top level game instance that is scoped to window.
   * @param {String} name Instance name.
   * @summary A standardized format for attaching verb subscriptions to assets.
   * @classdesc
   * <p>
   * <strong>VerbSubscription</strong> is in tryout vs
   * using unclassed objects.
   * </p>
   * <p>
   * <strong>VerbSubscription</strong> is a special class
   * used to create a standardized format for managing
   * verb subscriptions on assets at
   * asset.dov[verb] and
   * asset.iov[verb].
   * This is meant to provide a clean and orderly way to
   * specify what verbs are supported by individual assets
   * or asset classes.
   * <br><br>
   * VerbSubscriptions and their values are passed down the
   * inheritance chain. For example:
   * <code>asset.dov.examine</code>
   * is available on {@link adventurejs.Matter|Matter},
   * and inherited by
   * {@link adventurejs.Tangible|Tangible},
   * which means that all Tangible
   * subclasses can be examined.
   **/
  class VerbSubscription extends adventurejs.Atom {
    constructor(name, game_name, _this_id) {
      super(name, game_name);
      this.class = "VerbSubscription";

      if ("string" === typeof _this_id && _this_id) {
        this._this_id = _this_id;
      }

      /**
       * <strong>try_count: 0</strong> is used to track the number of times
       * the verb is attempted on this object.
       * @var {String} adventurejs.VerbSubscription#try_count
       */
      this.try_count = 0;

      /**
       * <strong>do_count: 0</strong> is used to track the number of times
       * the verb is successfully applied to this object.
       * @var {String} adventurejs.VerbSubscription#do_count
       */
      this.do_count = 0;

      /**
       * <strong>name: ''</strong> the name of the verb.
       * @var {String} adventurejs.VerbSubscription#name
       */
      this.name = "";

      /**
       * <strong>inherited_from: 'Asset'</strong> the name of the class this verb is inherited from. Since these properties aren't distinctly documented, this is offered as a convenience method for tracking down inherited verb settings.
       * @var {String} adventurejs.VerbSubscription#inherited_from
       */
      this.inherited_from = "Asset";

      /**
       * <strong>enabled: true</strong> allows changing the state of an asset's responsiveness to a given verb. If set false, a subscribed asset will not respond to the verb. This is useful for temporarily disabling verbs for specific assets, for example if you had a door that could not be unlocked until another action was completed.
       * @var {Boolean} adventurejs.VerbSubscription#enabled
       */
      this.enabled = true;

      /**
       * <strong>automatically: true</strong> allows for some verbs to be performed automatically if context calls for it, for example, when unlocking a door in order to pass through it. This takes precedence over global settings.
       * @var {Boolean} adventurejs.VerbSubscription#automatically
       */
      this.automatically = true;

      /**
       * <strong>automatically_after_use: true</strong> if automatically is set true, this sets it so that a verb can only be applied automatically after a player has already used it manually. This is to prevent automatic use of tools from breaking puzzles. For example, imagine one door with many keys but only one that works; if choosing the right key is part of the puzzle, this option prevents players from simply saying "unlock door" and having the right key automatically selected for them.
       * @var {Boolean} adventurejs.VerbSubscription#automatically_after_use
       */
      this.automatically_after_use = true;

      /**
       * <strong>once: false</strong> if true, asset can only be successfully used once with given verb, but is left in place after use.
       * @var {Boolean} adventurejs.VerbSubscription#once
       */
      this.once = false;

      /**
       * <strong>then_disable: false</strong> if true, asset can only be successfully used once with given verb, but is left in place after use.
       * @var {Boolean} adventurejs.VerbSubscription#then_disable
       */
      this.then_disable = false;

      /**
       * <strong>then_destroy: false</strong> allows author to specify that this asset should be destroyed after using. By default, then_destroy is set to a boolean. If true, asset is destroyed after a successful use regardless. then_destroy may provide a string or array or function subject to getStringOrArrayOrFunction().
       * @var {Boolean|*} adventurejs.VerbSubscription#then_destroy
       * @getSAF
       */
      this.then_destroy = false;

      /**
       * <strong>with_anything: true</strong> pertains only to indirect objects. If true, this asset can be used as an indirect object of this verb with any direct object.
       * @var {Boolean} adventurejs.VerbSubscription#with_anything
       */
      this.with_anything = false;

      /**
       * <strong>with_assets: []</strong> allows author to define a single asset that can interact with this one using the given verb, for example "unlock door with key" where key is the only asset that can unlock door.
       * @var {Array} adventurejs.VerbSubscription#with_assets
       */
      this.with_assets = [];
      // this.with;

      /**
       * <strong>with_classes: []</strong> allows author to define an entire class that can interact with this asset using the given verb, for example "unlock door with skeleton key" where any instance of the class SkeletonKey can unlock door.
       * @var {Array} adventurejs.VerbSubscription#with_classes
       */
      this.with_classes = [];
      // this.with_any;
      // this.with_all;

      /**
       * <strong>with_nothing: true</strong> pertains only to direct objects. If true, the specified verb can be applied to the direct object without the use of any indirect object.
       * @var {Boolean} adventurejs.VerbSubscription#with_nothing
       */
      this.with_nothing = false;

      /**
       * <strong>with_params: {}</strong> is an optional collection of verb-specific parameters that are copied from the verb that is being subscribed to. For example, plug includes with_params.connections for tracking which assets are plugged to which.
       * @var {Object} adventurejs.VerbSubscription#with_params
       */
      this.with_params = {};
      // this.params;
      // this.connections;
      // this.max_connections;

      /**
       * <strong>on_success: ''</strong> is an optional parameter. It is set as a string by default, but may provide a string or array or function to be served by getStringOrArrayOrFunction(). If it returns a string, that string will be appended to the verb's default success message.
       * @var {String|*} adventurejs.VerbSubscription#on_success
       * @getSAF
       */
      this.on_success = "";

      /**
       * <strong>on_first_success: ''</strong> is an optional parameter. It is set as a string by default, but may provide a string or array or function to be served by getStringOrArrayOrFunction(). If it returns a string, that string will be appended to the verb's default success message.
       * @var {String|*} adventurejs.VerbSubscription#on_first_success
       * @getSAF
       */
      this.on_first_success = "";

      /**
       * <strong>on_failure: ''</strong> is an optional parameter. It is set as a string by default, but may provide a string or array or function to be served by getStringOrArrayOrFunction(). If it returns a string, that string will be appended to the verb's default failure message.
       * @var {String|*} adventurejs.VerbSubscription#on_failure
       * @getSAF
       */
      this.on_failure = "";

      /**
       * <strong>on_first_failure: ''</strong> is an optional parameter. It is set as a string by default, but may provide a string or array or function to be served by getStringOrArrayOrFunction(). If it returns a string, that string will be appended to the verb's default failure message.
       * @var {String|*} adventurejs.VerbSubscription#on_first_failure
       * @getSAF
       */
      this.on_first_failure = "";

      this.doBeforeTry = null;
      this.doAfterTry = null;
      this.doBeforeSuccess = null;
      this.doAfterSuccess = null;

      return this;
    }

    get _this_id() {
      return this.__this_id;
    }
    set _this_id(id) {
      id = A.serialize(id);
      this.__this_id = id;
    }

    get _this() {
      return this.game.getAsset(this._this_id);
    }

    /**
     * Enable this verb on its parent asset.
     * @memberOf adventurejs.VerbSubscription
     * @method adventurejs.VerbSubscription#enable
     */
    enable() {
      this.enabled = true;
    }

    /**
     * Disable this verb on its parent asset.
     * @memberOf adventurejs.VerbSubscription
     * @method adventurejs.VerbSubscription#disable
     */
    disable() {
      this.enabled = false;
    }

    /**
     * Increment this verb subscription's do_count.
     * @memberOf adventurejs.VerbSubscription
     * @method adventurejs.VerbSubscription#incrementDoCount
     */
    incrementDoCount() {
      this.do_count++;
    }

    /**
     * Increment this verb subscription's try_count.
     * @memberOf adventurejs.VerbSubscription
     * @method adventurejs.VerbSubscription#incrementTryCount
     */
    incrementTryCount() {
      this.try_count++;
    }

    /**
     * Check whether this verb has been successfully applied.
     * @memberOf adventurejs.VerbSubscription
     * @var {Getter} adventurejs.VerbSubscription#did_do
     */
    get did_do() {
      return this.do_count > 0;
    }

    /**
     * Check whether this verb has been tried.
     * @memberOf adventurejs.VerbSubscription
     * @var {Getter} adventurejs.VerbSubscription#did_try
     */
    get did_try() {
      return this.try_count > 0;
    }
  }
  adventurejs.VerbSubscription = VerbSubscription;
})();