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

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

  /**
   * @augments {adventurejs.Verb}
   * @class swing_from_to
   * @ajsnode game.dictionary.verbs.swing_from_to
   * @ajsconstruct MyGame.createVerb({ "name": "swing_from_to", [...] });
   * @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 from flagpole to antenna</span>
   * Using the grapple, you swing from the flagpole to the antenna.
   * </pre>
   * <p>
   * <strong>Swing from</strong> one
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to another.
   * This verb is a catchall for the specific phrasing of
   * "swing from thing to thing", and has no success logic of its
   * own, but tries to redirect to
   * {@link swing_to_on} or
   * {@link swing_from_to_on}.
   * </p>
   * Figure out what player is swinging on then forward to swingFromToOn
   */
  A.Preverbs.swing_from_to = {
    name: "swing_from_to",
    prettyname: "swing from",
    synonyms: [],
    verb_prep_noun_prep_noun: ["swing from to"],

    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: [ 'from' ], /* @todo */
      noun_must_be: {
        player_parent: true,
      },
    },

    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      //accepts_preposition: true,
      //requires_preposition: true,
      //accepts_these_prepositions: [ 'to' ], /* @todo */
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        //reachable: true, // TODO check can.swing_to and things_player_can_swing_to_from_this
        not_player_parent: true,
      },
    },

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

      /**
       * There are only a couple of conditions under which a
       * player can swing, so let's see if either of those are true.
       * Player must either:
       * - be ON a thing they can swing on
       * - or HOLDING a thing they can swing on
       */

      var player_can_swing = false;

      // see if player is nested, ex: on a vine
      // and they're trying "swing from vine to x"
      if (player.isNested() && player.getNestAsset().can.swing_on_if_nested) {
        var onObject = A.clone.call(this.game, input.parsedNoun1);
        var toObject = A.clone.call(this.game, input.parsedNoun2);
        input.parsedNoun1 = A.clone.call(this.game, toObject);
        input.parsedNoun2 = A.clone.call(this.game, onObject);
        this.game.dictionary.doVerb("swing_to_on");
        return null;
      }

      // see if player is holding a rope that's supports swinging
      var swingobject;
      if (false === player_can_swing) {
        var holding = player.IOVgetConnections("hold");
        for (var i = 0; i < holding.length; i++) {
          swingobject = this.game.getAsset(holding[i]);
          if (
            swingobject.can.swing_on_if_holding ||
            (swingobject.can.swing_on_if_holding_and_supported &&
              swingobject.is.supported)
          ) {
            player_can_swing = true;
            break;
          }
        }
      }

      if (player_can_swing) {
        input.parsedNoun3 = new adventurejs.ParsedNoun(swingobject);
        this.game.dictionary.doVerb("swing_from_to_on");
        return null;
      }

      var msg = "$(We're) not holding anything $(we) can swing on. ";
      this.handleFailure(msg);
      return null;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var msg = "$(We) swing! ";
      if (msg) this.game.print(msg, input.output_class);
      return null;
    },
  };
})(); // swing_from_to