Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// canPlayerReachThatFromThis.js
(function () {
  /*global adventurejs A*/
  "use strict";
  var p = adventurejs.Tangible.prototype;
  /**
   * Check whether that asset can be reached
   * by player when nested in this asset.
   * @memberOf adventurejs.Tangible
   * @method adventurejs.Tangible#canPlayerReachThatFromThis
   * @param {Object} object
   * @returns {Boolean}
   */
  p.canPlayerReachThatFromThis = function Tangible_canPlayerReachThatFromThis(
    thatobject,
    thatprep
  ) {
    // was question asked without a valid object?
    if ("undefined" === typeof thatobject || !thatobject) {
      return false;
    }

    var player = this.game.getPlayer();

    // question is only relevant if player is nested within this asset
    if (this !== player.getNestAsset()) {
      return false;
    }

    var bool = false;
    var this_aspect = player.getNestPreposition();

    // Arrays can contain ID strings or objects which
    // ["uncomfortable_looking_bed", "table"]
    // [{desk: ["on"]}, {uncomfortable_looking_bed: ["on"]}]

    // this.things_player_can_reach_from_this is the simple version
    // of setting reachability.
    bool = A.isIdInMixedArray(
      thatobject.id,
      this.things_player_can_reach_from_this
    );
    if (bool) return bool; // don't need to look any deeper

    // the simple version of setting reachability from top of thing
    if (player.isNestedOnTop()) {
      bool = A.isIdInMixedArray(
        thatobject.id,
        this.things_player_can_reach_from_top_of_this
      );
      if (bool) return bool;
    }

    // the simple version of setting reachability from bottom of thing
    if (player.isNestedOnBottom()) {
      bool = A.isIdInMixedArray(
        thatobject.id,
        this.things_player_can_reach_from_bottom_of_this
      );
      if (bool) return bool;
    }

    // else we need to look deeper into the aspect player is nested in
    bool = this.aspects[this_aspect].canPlayerReachThatFromThisAspect(
      thatobject,
      thatprep
    );

    return bool;
  };
})();