Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// registerParts.js
(function() {
	/*global adventurejs A*/ 
  "use strict";
  var p = adventurejs.Tangible.prototype;	
  /**
   * registerParts is a shortcut method for use with complex objects 
   * that have interlinked parts. For example, a sink of class Drainable 
   * can have a faucet, a drain, and multiple handles, each of which 
   * interact with the base object or other objects in the collection 
   * in different ways. Registering parts with the base object offers 
   * an opportunity to create object references and share variables.
   * @memberOf adventurejs.Tangible
	 * @method adventurejs.Tangible#registerParts
   */
   p.registerParts = function Tangible_registerParts()
  {
    if(this.parts.length>0)
    {
      //var msg = this.class + " " + this.id + " registerParts found parts";
      var msg = this.id + ".registerParts)() found parts:";
      this.game.log( "log", "high", msg , 'Tangible' );
    }
    for( var i = this.parts.length-1; i > -1; i-- )
    {
      var part_id = this.parts[i];
      var part = this.game.getAsset( this.parts[i] );
      this.parts.splice( i, 1 );
      if( "undefined" === typeof part )
      {
        //var msg = this.class + " " + this.name + " parts contains an invalid id: " + id;
        var msg = this.name + " parts contains an invalid id: " + part_id;
        this.game.log( "warn", "critical", msg , 'Tangible' );
        continue;
      }
      var registerableClassKeys = Object.keys( this.registerableClasses );
      for( var r = 0; r < registerableClassKeys.length; r++ )
      {
        var registerableClass = registerableClassKeys[r];
        if( true === part instanceof adventurejs[registerableClass] )
        {
          // pass in a reference to 'this' as well as the part to be registered
          // because we're going into anonymous functions that don't have scope
          //this.registerableClasses[ registerableClass ]( this, part );
          this.registerableClasses[ registerableClass ].call( this, part );
          //var msg = "-- " + this.class + " " + this.id + " register " + part.class + " " + part.id;
          var msg = "-- " + this.id + " register " + part.id;
          this.game.log( "log", "high", msg , 'Tangible' );
          continue;
        }
      }
    }
  }
}());