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

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

	var p = adventurejs.Game.prototype;

  /**
   * <p>
   * Global assets are a bit (perhaps overly) complicated. 
   * They exist as singular objects in the game world, and 
   * they can be enabled/disabled and have their descriptions 
   * overridden per room and per zone. 
   * </p>
   * <p>
   * For example: <br/>
   * 
   * <li><code>MyGame.world.global_air</code> is an asset</li>
   * 
   * <li><code>MyGame.world.room.room_scenery.global_air</code>
   * is a simple object that can be used to determine whether 
   * a global asset is available in a given room. </li>
   * 
   * <li><code>MyGame.world.zone.zone_scenery.global_air</code>
   * is a simple object that can be used to determine whether 
   * a global asset is available in a given zone. </li>
   * 
	 * Though global asset is singular, its description can be 
	 * overridden by descriptions set in room_scenery and zone_scenery.
   * </p>
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#getGlobalAssetDescription
	 * @param {Object|String} asset
   * @returns {String} A description of the specified asset id.
   */
   p.getGlobalAssetDescription = function Game_getGlobalAssetDescription(asset, property )
  {
    //          defined  enabled  described
    //   room   _        _        _           all slots can be  
    //   zone   _        _        _           true, false, or
    // global   _        _        _           null/undefined

    // if defined is null we check next level
    // if defined is false at any stage we stop
    // if any level is enabled but not described, 
    //   try to get description from next level

    if("string" === typeof asset) asset = this.game.getAsset(asset);
    if( !(asset instanceof adventurejs.Asset ) ) return '';

    // property may be something other than 'description'
    // and while global asset may have multiple descriptions,
    // overrides likely only have one description 
    var defaultprop = 'description';
    if( "string" !== typeof property ) property = defaultprop;

    var currentRoom = this.game.getCurrentRoom();

		var defined_in_room;
		var enabled_in_room; // true, false, or null/undefined
		var described_in_room;

		var defined_in_zone;
		var enabled_in_zone; // true, false, or null/undefined
		var described_in_zone;

		var defined_in_game;
		var enabled_in_game; // true, false, or null/undefined
		var described_in_game;

    // get room settings
    defined_in_room = currentRoom.room_scenery[ asset.id ];
    if( "undefined" !== typeof defined_in_room )
    {
      enabled_in_room = defined_in_room.enabled; 
      described_in_room = defined_in_room[property] || defined_in_room[defaultprop];
    }

    // get zone settings
    if ( "undefined" !== typeof this.game.world[ currentRoom.zone ] ) 
    {
      defined_in_zone = this.game.world[ currentRoom.zone ].zone_scenery[ asset.id ];
    }
    if( defined_in_zone ) {
      enabled_in_zone = defined_in_zone.enabled;
      described_in_zone = defined_in_zone[property]||defined_in_zone[defaultprop];
    }

    // get global settings
    // no need to ask if undefined because we received the global asset
    enabled_in_game = asset.enabled;
    described_in_game = asset.descriptions[property]||asset.descriptions[defaultprop];

    //console.warn('enabled_in_room',enabled_in_room);
    //console.warn('enabled_in_zone',enabled_in_zone);
    //console.warn('enabled_in_game',enabled_in_game);

    // if room explicitly says no, exit
    if( false === enabled_in_room )
    {
      return '';
    }
    // if room explicitly says yes, use room or zone or global
    else if( true === enabled_in_room )
    {
      return( described_in_room || described_in_zone || described_in_game);
    }
    // if zone explicitly says no, exit
    else if( false === enabled_in_zone )
    {
      return '';
    }
    // if zone explicitly says yes, use zone or global
    else if( true === enabled_in_zone )
    {
      return( described_in_zone || described_in_game);
    }
    // if game explicitly says no, exit
    else if( false === enabled_in_game )
    {
      return '';
    }
    // if game explicitly says yes, use global
    else if( true === enabled_in_game )
    {
      return( described_in_game);
    }

    // ideally we never arrive here
		return '';
  }
  
}());