Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// getAncestorId.js
(function () {
  /*global adventurejs A*/
  var p = adventurejs.Tangible.prototype;
  /**
   * Get the ID of this asset's topmost parent, excluding {@link adventurejs.Room|Room}.
   * @memberOf adventurejs.Tangible
   * @method adventurejs.Tangible#getAncestorId
   * @returns {String}
   */
  p.getAncestorId = function Tangible_getAncestorId() {
    var child = this;
    var parent_asset = this.getPlaceAsset();

    // child has no parent, so it's not accessible
    if (!parent_asset) {
      return "";
    }

    // child parent is room
    if (parent_asset instanceof adventurejs.Room) {
      return "";
    }

    while (parent_asset) {
      child = parent_asset;
      if (!parent_asset.getPlaceAsset()) {
        return "";
      }
      parent_asset = parent_asset.getPlaceAsset();
      if (parent_asset instanceof adventurejs.Room) {
        return child.id;
      }
    }
    return "";
  };
})();