Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// swing_on.js

/**
 */
(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @augments {adventurejs.Verb}
   * @class swing_on
   * @ajsnode game.dictionary.verbs.swing_on
   * @ajsconstruct MyGame.createVerb({ "name": "swing_on", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading DeprecatedVerbs
   * @summary Summary.
   * @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; swing on rope</span>
   * You swing on the rope.
   * </pre>
   * <p>
   * <strong>Swing on</strong> has two distinct interpretations:
   * <li>
   * Player is nested on a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} that swings,
   * such as hanging from a vine<br>
   * ex: swing on vine
   * </li>
   * <li>
   * Player is holding a Tangible Asset that can be swung on
   * (which needs to be tested)<br>
   * ex: "hold vine then swing on vine"
   * </li>
   * No special logic is provided on success.
   * Authors wanting to make use of it may need to use a method such
   * as verb hooks. See
   * <a href="/doc/Scripting_VerbPhases.html">Verb Phases</a>
   * to learn more.
   * </p>
   * <!--
   * swingOn doesn't change state so it never results in success.
   * Print faux-success if player is holding a supported rope.
   * Print faux-success if player is nested on swingable.
   * -->
   */
  A.Preverbs.swing_on = {
    name: "swing_on",
    prettyname: "swing on",
    synonyms: [],
    verb_prep_noun: ["swing on"],

    player_must_be: {
      not_on_floor: true,
      not_constrained: true,
      not_under: true,
      not_behind: true,
      not_nested_elsewhere: true,
    },

    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      //accepts_preposition: true,
      //requires_preposition: true,
      //accepts_these_prepositions: [ 'on' ], /* @todo */
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

    doTry: function () {
      var input = this.game.getInput();
      var direct_object = input.getAsset(1);
      var player = this.game.getPlayer();

      if (false === direct_object.can.swing_on) {
        var msg = "$(We) can't swing on " + direct_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      if (
        !player.IOVisConnectedToAsset("hold", direct_object) &&
        direct_object.id !== player.getNestId()
      ) {
        var msg = "$(We're) not holding " + direct_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      // player is nested on directobject
      if (
        direct_object.can.swing_on &&
        direct_object.id === player.getNestId()
      ) {
        var msg =
          "$(We) swing back and forth " +
          player.getNestPreposition() +
          " " +
          direct_object.articlename +
          ". ";
        this.handleFailure(msg);
        return null;
      }

      if (player.IOVisConnectedToAsset("hold", direct_object)) {
        var can_be_swung = false;
        var msg = "";
        if (direct_object.can.swing_on_if_holding) {
          can_be_swung = true;
        } else if (
          direct_object.can.swing_on_if_holding_and_supported &&
          direct_object.is.supported
        ) {
          can_be_swung = true;
        }
        if (can_be_swung) {
          msg =
            "$(We) swing on " +
            direct_object.articlename +
            " and then land back at your original position.";
        } else {
          msg = "$(We) can't swing on " + direct_object.articlename + ". ";
        }
        this.handleFailure(msg);
        return null;
      }

      return true;
    },

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

      // check if there's a custom msg
      this.handleSuccess(msg, direct_object);
      return true;
    },
  };
})(); // swing_on