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) {
   *       super(name, game_name);
   *       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({
   *  parent: "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/AssetReference__Overview.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.parent The name of a parent class to subclass.
   * @param {String} data.class The name of the new subclass.
   * @returns {Object}
   */
  p.createClass = function Game_createCustomClass(data = {}) {
    const { parent, class: CustomClass, ...props } = data;
    const ParentClass = adventurejs[parent];
    if (!ParentClass) {
    }

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

    Object.defineProperty(Custom, "name", { value: CustomClass });
    A[CustomClass] = Custom;
    return Custom;
  };
})();