Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// wear.js

(function () {
  /*global adventurejs A*/

  /**
   * @augments {adventurejs.Verb}
   * @class wear
   * @ajsnode game.dictionary.verbs.wear
   * @ajsconstruct MyGame.createVerb({ "name": "wear", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading LocomotionVerbs
   * @summary Verb meaning wear, as in "wear chain mail".
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc
   * <pre class="display border outline">
   * <span class="input">&gt; wear knapsack</span>
   * You wear the knapsack. Inside are some comic books, a Superman
   * Pez dispenser, your flashlight, some twine, a Swiss Army knife,
   * a bottle of water, and a tin of field rations.
   * All the basics needed for your big adventure.
   * </pre>
   * <p>
   * <strong>Wear</strong> requires that the
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to be worn has
   * asset.dov.wear.enabled set to true.
   * Worn objects will have their
   * <a class="code" href="/doc/adventurejs.Tangible.html#property_is_worn">is.worn</a>
   * set to true, and may be handled differently from other inventory by
   * some verbs.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.wear = {
    name: "wear",
    prettyname: "wear",
    past_tense: "wore",
    synonyms: ["wear"],
    gerund: "wearing",

    /**
     * @ajsverbstructures
     * @memberof wear
     */
    accepts_structures: ["verb noun", "verb noun preposition noun"],

    /**
     * @memberof wear
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     takeable: true,
     *     in_inventory: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        dov: true,
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
        takeable: true,
        in_inventory: true,
      },
    },

    /**
     * @memberof wear
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     takeable: true,
     *     in_inventory: true,
     *   },
     *   accepts_preposition: true,
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        iov: true,
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
        takeable: true,
        in_inventory: true,
      },
      accepts_preposition: true,
    },

    /**
     * @memberof wear
     * @ajsverbparams
     * with_params: {},
     */
    with_params: {},

    doTry: function () {
      var input = this.game.getInput();
      var verb_phrase = input.verb_phrase;
      var player = this.game.getPlayer();
      var direct_object = input.getAsset(1);
      var parent = direct_object.getPlaceAsset();
      var msg = "";

      if (!direct_object.isDOV("wear")) {
        this.game.debug(
          `D1497 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.enabled is false `
        );
        msg += `$(We) can't ${this.name} ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      if (direct_object.is.worn) {
        this.game.debug(
          `D1498 | ${this.name}.js | ${direct_object.id}.is.worn `
        );
        msg += `$(We're) already ${this.gerund} ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      if (direct_object.is.zipped || direct_object.is.buttoned) {
        let state = direct_object.is.zipped ? "zipped" : "buttoned";
        let verb = direct_object.is.zipped ? "unzip" : "unbutton";
        this.game.debug(
          `D1892 | ${this.name}.js | ${direct_object.id}.is.${state} is true `
        );
        msg += `$(We'll) need to ${verb} ${direct_object.articlename} first. `;
        this.handleFailure(msg);
        return null;
      }

      // @TODO wear hat on head

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var verb_phrase = input.verb_phrase;
      var closedAnscestors = [];
      var direct_object = input.getAsset(1);
      var player = this.game.getPlayer();
      var parent = direct_object.getPlaceAsset();
      var tookFromParent;
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var results;
      var msg = "";

      // alternately, if we've decided it's available,
      // it doesn't matter if it's in inventory
      if (parent.id !== player.id) {
        // open any containers if necessary
        // we only do this for items nested in inventory
        if (
          direct_object.isWithin(player) &&
          direct_object.areAnscestorsClosed()
        ) {
          closedAnscestors = direct_object.getClosedAnscestors();
          for (var i = 0; i < closedAnscestors.length; i++) {
            this.game.getAsset(closedAnscestors[i]).is.closed = false;
            closedAnscestors[i] = this.game.getAsset(closedAnscestors[i]).name;
          }
        }

        // remove direct_object from its parent
        var results = parent.onRemoveThatFromThis(direct_object);
        if ("undefined" !== typeof results) return results;

        // set direct_object's new location to player
        results = player.onMoveThatToThis(direct_object, "in");
        if ("undefined" !== typeof results) return results;

        tookFromParent = true;
      }

      if (closedAnscestors.length > 0) {
        msg += "$(We) open the ";
        for (var i = 0; i < closedAnscestors.length; i++) {
          if (closedAnscestors.length > 2 && i < closedAnscestors.length - 2) {
            msg += ", ";
          }
          if (
            closedAnscestors.length > 1 &&
            i === closedAnscestors.length - 1
          ) {
            msg += " and the ";
          }
          msg += closedAnscestors[i];
        }
        msg += ". ";
      }
      if (tookFromParent) {
        msg += "$(We) take " + direct_object.articlename;
        if (false === parent instanceof adventurejs.Room) {
          msg += " from " + parent.articlename;
        }
        msg += " and put it on. ";
      } else {
        msg += "$(We) wear " + direct_object.articlename + ". ";
      }

      // apply state changes
      direct_object.is.worn = true;

      // print output
      return this.handleSuccess(msg, direct_object);
    },
  };
})(); // wear