Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// Platonic.js
(function () {
  /* global AdventureJS A */
  /**
   * @augments AdventureJS.Atom
   * @class AdventureJS.AssetHelpers.Platonic
   * @ajsconstruct MyGame.createAsset({ "class":"Platonic", "name":"foo" })
   * @ajsconstructedby AdventureJS.Game#createAsset
   * @param {String} game_name The name of the top level game object.
   * @param {String} name A name for the object, to be serialized and used as ID.
   * @ajsnavheading HelperClasses
   * @summary Engine class used as an interface for dispensers.
   * @classdesc
   * <p>
   * <strong>Platonic</strong> is subclassed from the
   * foundational class {@link AdventureJS.Atom|Atom}.
   * Platonic assets act as stand-ins for fungible classes;
   * when any class is declared fungible, a platonic asset
   * is automatically created for it. Fungibility means
   * one-of-many identical items, such as taking one grape
   * from a bunch of grapes or one nut from a sack of nuts.
   * </p>
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Pouch",
   *   name: "small pouch",
   *   place: { in: "Nomad" },
   *   synonyms: ["leather", "lace", "strip", "pistachio", "pistachios"],
   *   description: `A tiny pouch – really, hardly more than a scrap of leather – folded on itself and laced with a thin strip. The pouch contains a handful of pistachios. `,
   *   is: { closed: false },
   *   aspects: {
   *     in: {
   *       dispense_class: "Pistachio",
   *     },
   *   },
   * });
   *
   * MyGame.createClass({
   *   class: "Pistachio",
   *   extend: "Edible",
   *   singular: "pistachio",
   *   plural: "pistachios",
   *   description: `It's a pistachio. `,
   * });
   * </code></pre>
   *
   **/
  class Platonic extends AdventureJS.Atom {
    constructor(name, game_name, context_id = "", id = "") {
      super(name, game_name, context_id, id);
      this.class = "Platonic";
      this.is = {
        platonic: true,
        // fungible: true,
        extant: true,
        validated: false,
        initialized: false,
      };

      this.fungible = {
        dispensers: [],
        max_extant: -1,
        max_count: -1,
        dispense_class: "",
      };
      this.synonyms = [];
    }

    /**
     * @method AdventureJS.AssetHelpers.Platonic#validate
     * @memberOf AdventureJS.AssetHelpers.Platonic
     */
    validate(game) {
      return true;
    }

    /**
     * @method AdventureJS.AssetHelpers.Platonic#initialize
     * @memberOf AdventureJS.AssetHelpers.Platonic
     */
    initialize(game) {
      this.game.addAssetToLookup(this);
      return this;
    }

    hasDispensers() {
      return this.fungible?.dispensers?.length ? true : false;
    }

    getDispensers() {
      if (!this.fungible || !this.fungible.dispensers) return [];
      return this.fungible.dispensers;
    }

    getDispenserAssets() {
      if (!this.fungible || !this.fungible.dispensers) return [];
      const dispensers = [];
      for (const i in this.fungible.dispensers) {
        const [[key, value]] = Object.entries(this.fungible.dispensers[i]);
        dispensers.push(value);
      }
      return dispensers;
    }
  }

  AdventureJS.AssetHelpers.Platonic = Platonic;
})();