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

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

  /**
   * @augments {adventurejs.Verb}
   * @class jumpFrom_to
   * @ajsnode game.dictionary.verbs.jumpFrom_to
   * @ajsconstruct MyGame.createVerb({ "name": "jumpFrom_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; jump from golden bough to silver spoon</span>
   * You jump from the golden bough to the silver spoon, enjoying
   * your life of privilege. Money falls into your deserving hands!
   * </pre>
   * <p>
   * <strong>Jump from</strong> one
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}
   * to another. Requires that the object to jump from has its
   * <a class="code" href="/doc/adventurejs.Tangible.html#property_can_jump_from">can.jump_from</a>
   * property set to true, and that the object to jump to has its
   * <a class="code" href="/doc/adventurejs.Tangible.html#property_can_jump_to">can.jump_to</a>
   * property set to true. May also requires that the source
   * Asset has the destination Asset id listed in its
   * <a class="code" href="/doc/adventurejs.Tangible.html#property_things_player_can_jump_to_from_this">things_player_can_jump_to_from_this</a>
   * property.
   * </p>
   */
  A.Preverbs.jumpFrom_to = {
    name: "jumpFrom_to",
    prettyname: "jump",
    synonyms: [],
    verb_prep_noun_prep_noun: ["jump from to"],

    player_must_be: {
      not_constrained: true,
      not_on_floor: true,
      not_nested_elsewhere: true,
      not_on_target: true,
    },

    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        player_parent: true,
      },
    },

    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        tangible: true,
        known: true,
        present: true,
        visible: true,
        not_player_parent: true,
        //reachable: true, // @TODO check can.jump_to and things_player_can_jump_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();

      // player not on thing they asked to jump from
      if (false === player.isNested() || nest_parent_id !== direct_object.id) {
        var msg = "$(We're) not on " + direct_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      // prevent jump from under/behind
      if (
        player.isNested() &&
        ("under" === nest_preposition ||
          "behind" === nest_preposition ||
          (false === nest_parent_object.quirks.in_means_on &&
            "in" === nest_preposition))
      ) {
        var msg =
          "$(We) can't jump from " +
          nest_preposition +
          " " +
          direct_object.articlename +
          ". ";
        this.handleFailure(msg);
        return null;
      }

      // can't jump to
      if (false === indirect_object.can.jump_to) {
        var msg = "$(We) can't jump to " + indirect_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      // can't jump from
      if (false === direct_object.can.jump_from) {
        var msg = "$(We) can't jump from " + direct_object.articlename + ". ";

        if (direct_object.can.swing_from) {
          msg +=
            "$(We) might be able to swing from " +
            direct_object.articlename +
            ". ";
        }

        this.handleFailure(msg);
        return null;
      }

      // no jumping between these objects
      if (
        player.isNested() &&
        -1 ===
          direct_object.things_player_can_jump_to_from_this.indexOf(
            indirect_object.id
          ) &&
        -1 ===
          direct_object.things_player_can_do_all_verbs_to_from_this.indexOf(
            indirect_object.id
          )
      ) {
        var msg =
          "$(We) can't jump from " +
          direct_object.articlename +
          " to " +
          indirect_object.articlename +
          ". ";

        if (
          direct_object.can.swing_from &&
          (-1 !==
            direct_object.things_player_can_swing_to_from_this.indexOf(
              indirect_object.id
            ) ||
            -1 !==
              direct_object.things_player_can_do_all_verbs_to_from_this.indexOf(
                indirect_object.id
              ))
        ) {
          msg +=
            "$(We) might be able to swing from " +
            direct_object.articlename +
            " to " +
            indirect_object.articlename +
            ". ";
        }

        this.handleFailure(msg);
        return null;
      }

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

      // is player close enough on y?
      if (player.position.y < range.min) {
        var msg =
          "$(We) can't jump up to " +
          indirect_object.articlename +
          " from your position on " +
          direct_object.articlename +
          ". ";
        if (msg) this.game.print(msg, input.output_class);
        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 (
        indirect_object instanceof adventurejs.Exit ||
        indirect_object instanceof adventurejs.Aperture
      ) {
        this.game.dictionary.doVerb(indirect_object.direction);
        return null;
      }

      // player is on floor
      // if( false === player.isNested()
      //   && player.isOnFloor()
      // ) {
      //   var msg = "$(We'll) have to get up off the floor first.";
      //   if(msg) this.game.print( msg, input.output_class );
      //   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 results;

      // If direct_object is an exit/aperture, we already shunted to "enter".

      /**
       * We check default_aspect_for_go_on
       * and default_posture_for_go_on for context.
       */

      var posture;
      if (indirect_object.can_stand_on) {
        posture = "stand";
      } else {
        posture = indirect_object.default_posture_for_go_on;
      }
      var preposition = indirect_object.default_aspect_for_go_on;

      var msg = "$(We) ";
      if (player.isOnFloor()) {
        msg += "get up and ";
      }
      msg += "jump to " + indirect_object.articlename + ". ";

      // DO THE THING!
      if (player.isNested()) {
        results = player.onUnnestThisFromThat(direct_object);
        if ("undefined" !== typeof results) return results;
      }
      results = player.onNestThisToThat(indirect_object, preposition);
      if ("undefined" !== typeof results) return results;

      player.posture = posture;

      this.handleSuccess(msg, direct_object);
      return true;
    },
  };
})(); // jumpFrom_to