Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// Platonic.js
(function () {
  /* global adventurejs A */
  /**
   * @augments adventurejs.Atom
   * @class adventurejs.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 BaseClasses
   * @summary Framework class used as an interface for dispensers.
   * @classdesc
   * <p>
   * <strong>Platonic</strong> is subclassed from the
   * foundational class {@link adventurejs.Atom|Atom},
   * and is used to make platonic assets, which are stand-ins
   * for one-from-many assets, such as one grape from a
   * bunch of grapes or one nut from a sack of nuts. Platonic
   * assets are never instantiated by authors - they're
   * created automatically any time an asset is set to dispense.
   * </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: "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,
        extant: true,
        validated: false,
        initialized: false,
      };

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

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

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

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

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

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

  adventurejs.Platonic = Platonic;
})();