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

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

  /**
   * @augments {adventurejs.Verb}
   * @class swing_to_on
   * @ajsnode game.dictionary.verbs.swing_to_on
   * @ajsconstruct MyGame.createVerb({ "name": "swing_to_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 to stalactite on root</span>
   * You swing to the stalactite on the hanging root.
   * </pre>
   * <p>
   * <strong>swing_to_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 to branch 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 to branch on vine"</li>
   * If player is nested on something other
   * than the swingable, the verb redirects to
   * {@link swing_from_to_on}.
   * </p>
   */
  A.Preverbs.swing_to_on = {
    name: "swing_to_on",
    prettyname: "swing",
    synonyms: [],
    verb_prep_noun_prep_noun: ["swing to 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: [ 'to' ], /* @todo */
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        //reachable: true,
        not_player_parent: true,
      },
    },

    phrase2: {
      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, // TODO check can.swing_to and things_player_can_swing_to_from_this
      },
    },

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

      var player = this.game.getPlayer();
      var nest_preposition = player.getNestPreposition();
      var nest_parent_id = player.getNestId();
      var nest_parent_object = player.getNestAsset();

      // is player nested and did player input object
      // for "on" that player is not on?
      // forward to swingfromtoon
      if (player.isNested() && nest_parent_id !== indirect_object.id) {
        input.parsedNoun3 = A.clone.call(this.game, input.parsedNoun2);
        input.parsedNoun2 = A.clone.call(this.game, input.parsedNoun1);
        input.parsedNoun1 = new adventurejs.ParsedNoun(nest_parent_object);
        this.game.dictionary.doVerb("swing_from_to_on");
        return null;
      }

      /**
       * 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:
       * - or HOLDING a thing they can swing on
       * - be ON a thing they can swing on
       */

      var player_can_swing = false;

      // is player is nested on swingable thing?
      if (
        player.isNested() &&
        nest_parent_id === indirect_object.id &&
        indirect_object.can.swing_on_if_nested
      ) {
        player_can_swing = true;
      }

      // is player holding a swingable thing?
      if (player.IOVisConnectedToAsset("hold", indirect_object)) {
        if (indirect_object.can.swing_on_if_holding) {
          player_can_swing = true;
        } else if (
          indirect_object.can.swing_on_if_holding_and_supported &&
          indirect_object.is.supported
        ) {
          player_can_swing = true;
        }
      }

      // 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) can't swing to " +
          direct_object.articlename +
          " on " +
          indirect_object.articlename +
          ". ";
        this.handleFailure(msg);
        return null;
      }

      // can't swing to
      if (false === direct_object.can.swing_to) {
        console.log(
          "swingto failed because " +
            direct_object.name +
            ".can.swing_to is false."
        );
        var msg = "$(We) can't swing to " + direct_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      // no swinging between these objects
      if (
        player.isNested() &&
        -1 ===
          nest_parent_object.things_player_can_swing_to_from_this.indexOf(
            direct_object.id
          ) &&
        -1 ===
          nest_parent_object.things_player_can_do_all_verbs_to_from_this.indexOf(
            direct_object.id
          )
      ) {
        var msg =
          "$(We) can't swing to " + direct_object.articlename + " from ";
        if (player.isNested()) {
          msg += " " + nest_parent_object.articlename;
        } else {
          msg += " here";
        }
        msg += ". ";

        if (
          nest_parent_object.can.jump_from &&
          (-1 !==
            nest_parent_object.things_player_can_jump_to_from_this.indexOf(
              direct_object.id
            ) ||
            -1 !==
              nest_parent_object.things_player_can_do_all_verbs_to_from_this.indexOf(
                direct_object.id
              ))
        ) {
          msg +=
            "$(We) might be able to jump from " +
            nest_parent_object.articlename +
            " to " +
            direct_object.articlename +
            ". ";
        }

        this.handleFailure(msg);
        return null;
      }

      // figure out y overlap
      var range = direct_object.getYRange();

      // is player close enough on y?
      if (player.position.y > range.max || player.position.y < range.min) {
        var msg =
          direct_object.Articlename +
          " is too far " +
          (player.position.y > range.max ? "below" : "above") +
          " your position on " +
          (player.isNested() ? nest_parent_object.articlename : "the floor") +
          ". ";
        this.handleFailure(msg);
        return null;
      }

      // TODO:
      // is object an exit? eg "jump to hatch"
      // for now just redirect to the exit
      // this might need to be revisited
      if (
        direct_object instanceof adventurejs.Exit ||
        direct_object instanceof adventurejs.Aperture
      ) {
        this.game.dictionary.doVerb(direct_object.direction);
        return null;
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var player = this.game.getPlayer();
      var nest_parent_id = player.getNestId();
      var posture = direct_object.default_posture_for_swing_to;
      var preposition = direct_object.default_aspect_for_swing_to;
      var results;

      // if player is nested, unnest
      if (player.isNested() && nest_parent_id !== direct_object.id) {
        results = player.onUnnestThisFromThat(direct_object);
        if ("undefined" !== typeof results) return results;
      }

      // if it isn't already nested, nest it
      if (nest_parent_id !== direct_object.id) {
        results = player.onNestThisToThat(direct_object, preposition);
        if ("undefined" !== typeof results) return results;
        player.posture = posture;
      }

      var msg =
        "$(We) swing to " +
        direct_object.articlename +
        " and land, " +
        player.getPostureGerund() +
        " " +
        preposition +
        " it. ";

      // must.let_go_after_swing
      if (
        player.IOVisConnectedToAsset("hold", indirect_object) &&
        indirect_object.must.let_go_after_swing
      ) {
        results = indirect_object.onReleaseThis(player);
      }
      msg += indirect_object.Articlename + " slips out of $(our) hands. ";

      if (direct_object.position.y !== player.position.y) {
        player.position.y = direct_object.position.y;
      }

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

      return true;
    },
  };
})(); // swing_to_on