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

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

	var p = adventurejs.Parser.prototype;

  /**
   * Exclude from a list of assets all assets but player's parent. 
   * Should only ever return one object.
   * @method adventurejs.Parser#selectPlayerParent
   * @memberOf adventurejs.Parser
   * @param {Array} list 
   * @returns {Array}
   */
   p.selectPlayerParent = function Parser_selectPlayerParent(list) 
  {
    if("string" === typeof list) list = [list];
		if( !Array.isArray( list ) ) 
    {
      this.game.log( "warn", "critical", "selectPlayerParent.js > received non-array", 'Parser' );
			return [];
		}
		var foundObjects = [];
		for(var i = 0; i < list.length; i++) 
    {
      var object = this.game.getAsset( list[i] );
      var player = this.game.getPlayer();
      var currentRoom = this.game.getCurrentRoom();

      if( player.isNested() 
      && object.id === player.getNestId() )
      {
        foundObjects.push( list[i] );
      }

      else if( false === player.isNested() 
      && object.id === currentRoom.id )
      {
        foundObjects.push( list[i] );
      }

      else if( player.isNested() 
      && object.id === currentRoom.id
      && player.getNestAsset() instanceof adventurejs.Floor )
      {
        foundObjects.push( currentRoom.id );
      }

      if( 0 < foundObjects.length ) break;

		}

		return foundObjects;
	}
  
}());