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

  /**
   * If this asset dispenses anything, we need to:
   * - ensure that the dispensed class is marked as is_fungible: 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.Assets.Tangible
   * @method AdventureJS.Assets.Tangible#setDispensers
   */
  p.setDispensers = function Tangible_setDispensers() {
    for (const id in this.aspects) {
      const aspect = this.aspects[id];

      if (aspect.dispense_class) {
        const Class = A.FX.propercase(aspect.dispense_class);
        const clas = Class.toLowerCase();

        // is there an AdventureJS class that matches what this dispenser dispenses?
        let c = A[Class] || A.Assets[Class];
        if (c) {
          const platonic_id = `platonic_${clas}`;
          aspect.platonic_asset = platonic_id;

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

          c.is_fungible = true;

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

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

          // define a precursor for a platonic asset
          const precursor = {
            class: Class,
            id: platonic_id,
            name: Class.toLowerCase(),
            fungible: {
              dispense_class: 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;
            // Look through the deferred assets list for any existing
            // precursor that matches the one we just made.
            // We only need one precursor per platonic type, but if we
            // already defined one, it will still need a record of this
            // new dispenser.
            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.fungible.dispensers =
                  d_asset.fungible.dispensers.concat(
                    precursor.fungible.dispensers
                  );
                push = false;
              }
            }

            // add the precursor to the deferred assets queue to be
            // instantiated after at the end of the asset init cycle
            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
  };
})();