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

  /**
   * If this asset dispenses anything, we need to:
   * - ensure that the dispensed class is marked as dispensedClass: true
   * - create a platonic asset for the dispensary
   * - save a record of each asset to the other
   * This runs during validation rather than initialization
   * to ensure that platonic assets are in place for initialization.
   * @memberOf adventurejs.Tangible
   * @method adventurejs.Tangible#setDispensers
   */
  p.setDispensers = function Tangible_setDispensers() {
    for (const id in this.aspects) {
      const aspect = this.aspects[id];

      if (aspect.dispense) {
        const Class = A.propercase(aspect.dispense);
        const clas = Class.toLowerCase();
        if (A[Class]) {
          const platonic_id = `platonic_${Class.toLowerCase()}`;
          aspect.platonic = platonic_id;

          this.game.platonic_dispensers[this.id] = {
            id: this.id,
            aspect: aspect.name,
            dispenser: Class,
            platonic: platonic_id,
          };

          A[Class].is_dispensary_class = true;

          // if ("undefined" === typeof this.game.potential_lookup[clas]) {
          //   this.game.potential_lookup[clas] = {};
          // }
          // this.game.potential_lookup[clas].type = "platonic";
          // this.game.potential_lookup[clas].class = Class;

          if ("undefined" === typeof this.game.platonic_classes[Class]) {
            this.game.platonic_classes[Class] = {
              class: Class,
              dispensers: [],
            };
          }
          this.game.platonic_classes[Class].dispensers.push({
            [aspect.name]: this.id,
          });

          const precursor = {
            class: Class,
            id: platonic_id,
            name: Class.toLowerCase(),
            dispensary: {
              dispenses: Class,
              dispensers: [{ [aspect.name]: this.id }],
            },
            is: { platonic: true },
          };

          if (this.game.game_state < this.game.game_states.PLAYING) {
            // use this in case of game startup
            let push = true;
            // check for duplicate precursors - we only need one
            // but we want it to have a record of any dispensers
            for (let i = 0; i < this.game.deferred_assets.length; i++) {
              const d_asset = this.game.deferred_assets[i];

              if (d_asset.class === precursor.class) {
                d_asset.dispensers = d_asset.dispensers.concat(
                  precursor.dispensers
                );
                push = false;
              }
            }
            if (push) this.game.deferred_assets.push(precursor);
          } else {
            // use this in case of dynamic creation
            let add = true;
            // check for existing assets - we only need one platonic
            // but we want it to have a record of any dispensers
            let platonic_asset;
            platonic_asset = this.game.world[`platonic_${Class.toLowerCase()}`];
            if (platonic_asset) {
              add = false;
            }
            if (add) {
              platonic_asset = this.game.addAsset(precursor);
            }
            if (platonic_asset) {
              this.dispenser.platonic = platonic_asset.id;
            }
          }
        } // if
      } // if
    } // for
  };
})();