Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
//createClass.js
(function () {
  /* global adventurejs A */

  var p = adventurejs.Game.prototype;

  /**
   * <pre class="display"><code class="language-javascript">MyGame.createClass(
   *   class Foo extends adventurejs.Bar {
   *     constructor(name, game_name, context_id = "", id = "") {
   *       super(name, game_name, context_id, id);
   *       this.class = "Foo";
   *     }
   *   }
   * )</code></pre>
   * <p>
   * <strong>createClass()</strong> is a constructor function
   * available to authors for creating whole new classes of Assets.
   * The function takes one parameter: a class declaration function.
   * For example, to create a special class of Yggdrasil tree:
   * </p>
   * <pre class="display"><code class="language-javascript">MyGame.createClass({
   *  extends: "Tree",
   *  class: "Yggdrasil",
   * })</code></pre>
   * <p>
   * At runtime, <strong>createClass()</strong> plugs the new
   * class constructor directly into AdventureJS, where it can
   * be used to construct instances with the usual
   * Game.createAsset({}) method. As of this writing, createClass
   * is basically a passthrough without any internal error
   * checking, so consider yourself going off road. It's
   * recommended that you browse through the
   * <a href="/doc/AssetClasses__Index.html">Asset Reference</a>
   * to see how classes are laid out.
   * </p>
   * <p>
   * You can also find more info under
   * <a href="/doc/AdvancedScripting_CustomClasses.html">Custom Classes</a>.
   * </p>
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#createClass
   * @kind function
   * @param {Object} data A generic object with properties to copy into a new class definition.
   * @param {String} data.extends The name of a parent class to subclass.
   * @param {String} data.class The name of the new subclass.
   * @returns {Object}
   */
  p.createClass = function Game_createClass(data = {}) {
    const { extend, class: CustomClass, ...props } = data;
    const ExtendClass = adventurejs[extend];
    if (!ExtendClass) {
    }

    if (props.singular && props.plural) {
      if (!props.singularPluralPairs) {
        props.singularPluralPairs = [];
      }
      props.singularPluralPairs.push([props.singular, props.plural]);
    }

    const Custom = class CustomClass extends ExtendClass {
      constructor(...args) {
        super(...args);
        A.deepSet.call(this.game, props, this);
      }
    };

    Object.defineProperty(Custom, "name", { value: CustomClass });
    A[CustomClass] = Custom;
    if ("string" === typeof data.plural) {
      A[A.propercase(data.plural)] = A[CustomClass];
    }
    return Custom;
  };
})();