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

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

  /**
   * @augments {adventurejs.Verb}
   * @class swing_to
   * @ajsnode game.dictionary.verbs.swing_to
   * @ajsconstruct MyGame.createVerb({ "name": "swing_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 to Chrysler Building</span>
   * You swing to the Chrysler Building.
   * </pre>
   * <p>
   * <strong>Swing to</strong> is the simplest form of <strong>swing</strong>
   * and doesn't contain enough information to act on,
   * but depending on context, it will redirect to
   * {@link swing_to_on} or
   * {@link swing_from_to_on}.
   *
   * <li>
   * If player is nested on a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} that swings,
   * such as hanging from a vine, verb will redirect to
   * {@link swing_to_on}.
   * </li>
   *
   * <li>
   * If player is not nested on one Tangible Asset and holding
   * another that can be swung on, verb will also redirect to
   * {@link swing_to_on}.
   * </li>
   *
   * <li>
   * If player is nested on a Tangible Asset and holding
   * another Asset that can be swung on,
   * verb will redirect to
   * {@link swing_from_to_on}.
   * </li>
   *
   * </p>
   * Description of the verb.
   * IDEAL FORM: swing from x to y with z
   * swing to y
   * swing from x to y
   * Use cases:
   *
   * <li>player is on a thing that swings, ex: hanging from a vine</li>
   *
   * <li>player is holding a thing that swings and is also tied at one end
   * to a suspension, ex: "hold vine. swing to x"</li>
   */
  A.Preverbs.swing_to = {
    name: "swing_to",
    prettyname: "swing to",
    synonyms: [],
    verb_prep_noun: ["swing to"],
    verb_prep_prep_noun: ["swing over 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: [ 'to' ], /* @todo */
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        //reachable: true,
        not_player_parent: true,
      },
    },

    doTry: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();
      var nest_parent_id = player.getNestId();
      var nest_parent_object = player.getNestAsset();

      /**
       * 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
      if (player.isNested() && nest_parent_object.can.swing_on_if_nested) {
        input.parsedNoun2 = new adventurejs.ParsedNoun(nest_parent_object);
        this.game.dictionary.doVerb("swing_to_on");
        return null;
      }

      // see if player is holding/carrying a rope that supports swinging
      var swingobject;
      if (false === player_can_swing) {
        var holding = player
          .IOVgetConnections("hold")
          .concat(player.getContentsAt("in"));
        console.warn(holding);
        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 && false === player.isNested()) {
        input.parsedNoun2 = new adventurejs.ParsedNoun(swingobject);
        this.game.dictionary.doVerb("swing_to_on");
        return null;
      }

      if (player_can_swing && player.isNested()) {
        input.parsedNoun2 = A.clone.call(this.game, input.parsedNoun1);
        input.parsedNoun1 = new adventurejs.ParsedNoun(nest_parent_object);
        input.parsedNoun3 = new adventurejs.ParsedNoun(swingobject);
        this.game.dictionary.doVerb("swing_from_to_on");
        return null;
      }

      // can't swing at all
      if (false === player_can_swing) {
        console.log(
          "swingto failed because player is not nested on nor holding swingable."
        );
        var msg = "$(We're) not holding anything $(we) can swing on. ";
        this.handleFailure(msg);
        return null;
      }

      // we've shunted to swingToOn or errored - no need to proceed
    },

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