Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 1
// isWithin.js
(function () {
  /*global adventurejs A*/
  var p = adventurejs.Tangible.prototype;

  /**
   * <strong>isWithin</strong> tests whether
   * one asset is in (any aspect of) another asset.
   * <pre class="display"><code class="language-javascript">if( MyGame.$('jewel').$isWithin('crown') ){ // do stuff }
   * </code></pre>
   * @memberOf adventurejs.Tangible
   * @method adventurejs.Tangible#isWithin
   * @param {Object|String} asset Can be string or object.
   * @returns {boolean}
   */
  p.isWithin = p.$isWithin = function Tangible_isWithin(asset) {
    if ("string" === typeof asset) asset = this.game.getAsset(asset);
    if (!asset || !asset.id || !asset.hasClass("Tangible")) return false;
    var nextlevel;

    // get parent of this
    if (this.place) nextlevel = this.getPlaceAsset();

    // this has no parent
    if (!nextlevel) return false;

    while (nextlevel) {
      if (nextlevel.id === asset.id) {
        return true;
      } else {
        if (!nextlevel.place) return false;
        nextlevel = nextlevel.getPlaceAsset();
        if (!nextlevel) return false;
      }
    }

    return false;
  };
})();